style_guide.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Proper indentation is important for readability.
  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. Spaces around operators
  20. -----------------------
  21. In general, spaces are put around operators, such as the equals ``=`` or plus ``+`` signs.
  22. For example:
  23. .. code-block:: javascript
  24. if (sublocale != locale) {
  25. // do something
  26. }
  27. An exception is when they appear inside for-loop expressions, for example:
  28. .. code-block:: javascript
  29. for (i=0; i<msgs_length; i++) {
  30. // do something
  31. }
  32. Generally though, rather err on the side of adding spaces, since they make the
  33. code much more readable.
  34. Global constants are written in ALL_CAPS
  35. ----------------------------------------
  36. Global identifiers that denote constant values should be written in
  37. all capital letters, with underscores between words.
  38. For example:
  39. .. code-block:: javascript
  40. const SECONDS_IN_HOUR = 3600;
  41. function update () {
  42. const timeout = 20;
  43. let seconds_since_message = 0;
  44. // other stuff here
  45. }
  46. Function declaration and invocation
  47. -----------------------------------
  48. When declaring a function, the function name and the brackets after it are separated
  49. with a space. Like so:
  50. .. code-block:: javascript
  51. function update (model) {
  52. model.foo = 'bar';
  53. }
  54. When calling the same function, the brackets are written without a space in
  55. between:
  56. .. code-block:: javascript
  57. update(model);
  58. This is to make a more explicit visual distinction between method declarations
  59. and method invocations.
  60. Checking for equality
  61. ---------------------
  62. JavaScript has a strict ``===`` and less strict ``==`` equality operator. The
  63. stricter equality operator also does type checking. To avoid subtle bugs when
  64. doing comparisons, always use the strict equality check.
  65. Curly brackets
  66. --------------
  67. Curly brackets must appear on the same lines as the ``if`` and ``else`` keywords.
  68. The closing curly bracket appears on its own line.
  69. For example:
  70. .. code-block:: javascript
  71. if (locales[locale]) {
  72. return locales[locale];
  73. } else {
  74. sublocale = locale.split("-")[0];
  75. if (sublocale != locale && locales[sublocale]) {
  76. return locales[sublocale];
  77. }
  78. }
  79. Always enclose blocks in curly brackets
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. When writing a block such as an ``if`` or ``while`` statement, always use
  82. curly brackets around that block of code. Even when not strictly required by
  83. the compiler (for example if its only one line inside the ``if`` statement).
  84. For example, like this:
  85. .. code-block:: javascript
  86. if (condition === true) {
  87. this.updateRoomsList();
  88. }
  89. somethingElse();
  90. and NOT like this:
  91. .. code-block:: javascript
  92. if (converse.auto_list_rooms)
  93. this.updateRoomsList();
  94. somethingElse();
  95. This is to aid in readability and to avoid subtle bugs where certain lines are
  96. wrongly assumed to be executed within a block.