style_guide.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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".
  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. To
  64. avoid subtle bugs when doing comparisons, always use the strict equality check.
  65. Curly brackets
  66. --------------
  67. Curly brackets come on the same lines as the ``if`` and ``else`` keywords.
  68. For example:
  69. .. code-block:: javascript
  70. if (locales[locale]) {
  71. return locales[locale];
  72. } else {
  73. sublocale = locale.split("-")[0];
  74. if (sublocale != locale && locales[sublocale]) {
  75. return locales[sublocale];
  76. }
  77. }
  78. Always enclose blocks in curly brackets
  79. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80. When writing an a block such as an ``if`` or ``while`` statement, always use
  81. curly brackets around the block of code. Either when not strictly required by
  82. the compiler.
  83. For example, like this:
  84. .. code-block:: javascript
  85. if (condition === true) {
  86. this.updateRoomsList();
  87. }
  88. somethingElse();
  89. and NOT like this:
  90. .. code-block:: javascript
  91. if (converse.auto_list_rooms)
  92. this.updateRoomsList();
  93. somethingElse();
  94. This is to aid in readability and to avoid subtle bugs where certain lines are
  95. wrongly assumed to be executed within a block.