style_guide.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. .. note:: Converse.js doesn't yet use any of the new `ES2015
  6. <https://babeljs.io/docs/learn-es2015/>`_ features, because we don't
  7. rely on a transpiler and still support older browsers.
  8. Most of the style guide recommendations here come from Douglas Crockford's book
  9. `JavaScript, the good parts <http://shop.oreilly.com/product/9780596517748.do>`_
  10. This style guide is fairly opinionated. Some of these opinions perhaps don't
  11. conform to your expectations on how JavaScript code should look like.
  12. I apologize for that. However, for the sake of sanity, consistency and having
  13. code that is pleasing to the eye, please stick to these guidelines.
  14. Tabs or spaces?
  15. ---------------
  16. We always indent 4 spaces. Proper indentation is very important for readability.
  17. Underscores or camelCase?
  18. -------------------------
  19. We use camelCase for function names and underscores for variables names.
  20. For example:
  21. .. code-block:: javascript
  22. function thisIsAFunction () {
  23. var this_is_a_variable;
  24. ...
  25. }
  26. Spaces around operators
  27. -----------------------
  28. In general, spaces are put around operators, such as the equals ``=`` or plus ``+`` signs.
  29. For example:
  30. .. code-block:: javascript
  31. if (sublocale != locale) {
  32. // do something
  33. }
  34. An exception is when they appear inside for-loop expressions, for example:
  35. .. code-block:: javascript
  36. for (i=0; i<msgs_length; i++) {
  37. // do something
  38. }
  39. Generally though, rather err on the side of adding spaces, since they make the
  40. code much more readable.
  41. Constants are written in ALL_CAPS
  42. ---------------------------------
  43. Identifiers that denote constant values should be written in
  44. all capital letters, with underscores between words.
  45. For example:
  46. .. code-block:: javascript
  47. var SECONDS_IN_HOUR = 3600;
  48. var seconds_since_message = 0;
  49. Function declaration and invocation
  50. -----------------------------------
  51. When declaring a function, the function name and the brackets after it are separated
  52. with a space. Like so:
  53. .. code-block:: javascript
  54. function update (model) {
  55. model.foo = 'bar';
  56. }
  57. When calling the same function, the brackets are written without a space in
  58. between:
  59. .. code-block:: javascript
  60. update(model);
  61. This is to make a more explicit visual distinction between method declarations
  62. and method invocations.
  63. Checking for equality
  64. ---------------------
  65. JavaScript has a strict ``===`` and less strict ``==`` equality operator. The
  66. stricter equality operator also does type checking. To avoid subtle bugs when
  67. doing comparisons, always use the strict equality check.
  68. Curly brackets
  69. --------------
  70. Curly brackets must appear on the same lines as the ``if`` and ``else`` keywords.
  71. The closing curly bracket appears on its own line.
  72. For example:
  73. .. code-block:: javascript
  74. if (locales[locale]) {
  75. return locales[locale];
  76. } else {
  77. sublocale = locale.split("-")[0];
  78. if (sublocale != locale && locales[sublocale]) {
  79. return locales[sublocale];
  80. }
  81. }
  82. Always enclose blocks in curly brackets
  83. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  84. When writing an a block such as an ``if`` or ``while`` statement, always use
  85. curly brackets around that block of code. Even when not strictly required by
  86. the compiler (for example if its only one line inside the ``if`` statement).
  87. For example, like this:
  88. .. code-block:: javascript
  89. if (condition === true) {
  90. this.updateRoomsList();
  91. }
  92. somethingElse();
  93. and NOT like this:
  94. .. code-block:: javascript
  95. if (converse.auto_list_rooms)
  96. this.updateRoomsList();
  97. somethingElse();
  98. This is to aid in readability and to avoid subtle bugs where certain lines are
  99. wrongly assumed to be executed within a block.