style_guide.rst 3.9 KB

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