style_guide.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/theming.rst">Edit me on GitHub</a></div>
  3. Software Style Guide
  4. ====================
  5. Most of the style guide recommendations here come from Douglas Crockford's book
  6. `JavaScript, the good parts <http://shop.oreilly.com/product/9780596517748.do>`_
  7. Tabs or spaces?
  8. ---------------
  9. We always indent 4 spaces.
  10. Underscores or camelCase?
  11. -------------------------
  12. We use camelCase for function names and underscores for variables names.
  13. For example:
  14. .. code-block:: javascript
  15. function thisIsAFunction () {
  16. let this_is_a_variable;
  17. ...
  18. }
  19. const versus let
  20. ----------------
  21. Try to use `const` whenever possible. If a variable won't be reassigned, use
  22. `const`, otherwise use `let`.
  23. Spaces around operators
  24. -----------------------
  25. In general, spaces are put around operators, such as the equals ``=`` or plus ``+`` signs.
  26. For example:
  27. .. code-block:: javascript
  28. if (sublocale != locale) {
  29. // do something
  30. }
  31. An exception is when they appear inside for-loop expressions, for example:
  32. .. code-block:: javascript
  33. for (i=0; i<msgs_length; i++) {
  34. // do something
  35. }
  36. Generally though, rather err on the side of adding spaces, since they make the
  37. code much more readable.
  38. destructuring
  39. -------------
  40. When assigning to a variable via destructuring, add spaces between the curly
  41. brackets.
  42. For example:
  43. .. code-block:: javascript
  44. const { foo } = bar;
  45. Global constants are written in ALL_CAPS
  46. ----------------------------------------
  47. Global identifiers that denote constant values should be written in
  48. all capital letters, with underscores between words.
  49. For example:
  50. .. code-block:: javascript
  51. const SECONDS_IN_HOUR = 3600;
  52. function update () {
  53. const timeout = 20;
  54. let seconds_since_message = 0;
  55. // other stuff here
  56. }
  57. Function declaration and invocation
  58. -----------------------------------
  59. When declaring a function, the function name and the brackets after it are separated
  60. with a space. Like so:
  61. .. code-block:: javascript
  62. function update (model) {
  63. model.foo = 'bar';
  64. }
  65. When calling the same function, the brackets are written without a space in
  66. between:
  67. .. code-block:: javascript
  68. update(model);
  69. This is to make a more explicit visual distinction between method declarations
  70. and method invocations.
  71. Checking for equality
  72. ---------------------
  73. JavaScript has a strict ``===`` and less strict ``==`` equality operator. The
  74. stricter equality operator also does type checking. To avoid subtle bugs when
  75. doing comparisons, always use the strict equality check.
  76. Curly brackets
  77. --------------
  78. Curly brackets must appear on the same lines as the ``if`` and ``else`` keywords.
  79. The closing curly bracket appears on its own line.
  80. For example:
  81. .. code-block:: javascript
  82. if (locales[locale]) {
  83. return locales[locale];
  84. } else {
  85. sublocale = locale.split("-")[0];
  86. if (sublocale != locale && locales[sublocale]) {
  87. return locales[sublocale];
  88. }
  89. }
  90. Always enclose blocks in curly brackets
  91. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. When writing a block such as an ``if`` or ``while`` statement, always use
  93. curly brackets around that block of code. Even when not strictly required by
  94. the compiler (for example if its only one line inside the ``if`` statement).
  95. For example, like this:
  96. .. code-block:: javascript
  97. if (condition === true) {
  98. this.updateRoomsList();
  99. }
  100. somethingElse();
  101. and NOT like this:
  102. .. code-block:: javascript
  103. if (converse.auto_list_rooms)
  104. this.updateRoomsList();
  105. somethingElse();
  106. This is to aid in readability and to avoid subtle bugs where certain lines are
  107. wrongly assumed to be executed within a block.