style_guide.rst 3.7 KB

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