2016-11-02 15:31:58 +01:00
|
|
|
.. raw:: html
|
|
|
|
|
2022-06-08 11:10:04 +02:00
|
|
|
<div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/style_guide.rst">Edit me on GitHub</a></div>
|
2016-11-02 15:31:58 +01:00
|
|
|
|
2015-06-16 00:18:15 +02:00
|
|
|
Software Style Guide
|
|
|
|
====================
|
|
|
|
|
|
|
|
Most of the style guide recommendations here come from Douglas Crockford's book
|
2017-09-03 22:12:17 +02:00
|
|
|
`JavaScript, the good parts <http://shop.oreilly.com/product/9780596517748.do>`_
|
2015-06-16 00:18:15 +02:00
|
|
|
|
|
|
|
Tabs or spaces?
|
|
|
|
---------------
|
|
|
|
|
2022-03-29 22:55:48 +02:00
|
|
|
We always indent 4 spaces.
|
2015-06-16 00:18:15 +02:00
|
|
|
|
|
|
|
Underscores or camelCase?
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
We use camelCase for function names and underscores for variables names.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
.. code-block:: javascript
|
2015-06-16 00:18:15 +02:00
|
|
|
|
|
|
|
function thisIsAFunction () {
|
2019-07-20 21:55:42 +02:00
|
|
|
let this_is_a_variable;
|
2015-06-16 00:18:15 +02:00
|
|
|
...
|
|
|
|
}
|
|
|
|
|
2022-03-29 22:55:48 +02:00
|
|
|
const versus let
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Try to use `const` whenever possible. If a variable won't be reassigned, use
|
|
|
|
`const`, otherwise use `let`.
|
|
|
|
|
2015-06-19 17:54:49 +02:00
|
|
|
Spaces around operators
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
In general, spaces are put around operators, such as the equals ``=`` or plus ``+`` signs.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
.. code-block:: javascript
|
2015-06-19 17:54:49 +02:00
|
|
|
|
|
|
|
if (sublocale != locale) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
|
|
|
|
An exception is when they appear inside for-loop expressions, for example:
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
.. code-block:: javascript
|
2015-06-19 17:54:49 +02:00
|
|
|
|
|
|
|
for (i=0; i<msgs_length; i++) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
|
|
|
|
Generally though, rather err on the side of adding spaces, since they make the
|
|
|
|
code much more readable.
|
|
|
|
|
2022-03-29 22:55:48 +02:00
|
|
|
destructuring
|
|
|
|
-------------
|
|
|
|
|
|
|
|
When assigning to a variable via destructuring, add spaces between the curly
|
|
|
|
brackets.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
const { foo } = bar;
|
|
|
|
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
Global constants are written in ALL_CAPS
|
|
|
|
----------------------------------------
|
2015-06-16 00:18:15 +02:00
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
Global identifiers that denote constant values should be written in
|
2015-06-16 00:18:15 +02:00
|
|
|
all capital letters, with underscores between words.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
const SECONDS_IN_HOUR = 3600;
|
2015-06-16 00:18:15 +02:00
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
function update () {
|
|
|
|
const timeout = 20;
|
|
|
|
let seconds_since_message = 0;
|
|
|
|
// other stuff here
|
|
|
|
}
|
2015-06-16 00:18:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
Function declaration and invocation
|
|
|
|
-----------------------------------
|
|
|
|
|
|
|
|
When declaring a function, the function name and the brackets after it are separated
|
|
|
|
with a space. Like so:
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
.. code-block:: javascript
|
2015-06-16 00:18:15 +02:00
|
|
|
|
|
|
|
function update (model) {
|
|
|
|
model.foo = 'bar';
|
|
|
|
}
|
|
|
|
|
|
|
|
When calling the same function, the brackets are written without a space in
|
|
|
|
between:
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
.. code-block:: javascript
|
2015-06-16 00:18:15 +02:00
|
|
|
|
|
|
|
update(model);
|
|
|
|
|
|
|
|
This is to make a more explicit visual distinction between method declarations
|
|
|
|
and method invocations.
|
|
|
|
|
|
|
|
Checking for equality
|
|
|
|
---------------------
|
|
|
|
|
2017-09-03 22:12:17 +02:00
|
|
|
JavaScript has a strict ``===`` and less strict ``==`` equality operator. The
|
2015-10-24 19:18:56 +02:00
|
|
|
stricter equality operator also does type checking. To avoid subtle bugs when
|
|
|
|
doing comparisons, always use the strict equality check.
|
2015-06-16 00:18:15 +02:00
|
|
|
|
2015-06-19 17:54:49 +02:00
|
|
|
Curly brackets
|
|
|
|
--------------
|
|
|
|
|
2015-10-24 19:18:56 +02:00
|
|
|
Curly brackets must appear on the same lines as the ``if`` and ``else`` keywords.
|
|
|
|
The closing curly bracket appears on its own line.
|
2015-06-19 17:54:49 +02:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
.. code-block:: javascript
|
2015-06-19 17:54:49 +02:00
|
|
|
|
|
|
|
if (locales[locale]) {
|
|
|
|
return locales[locale];
|
|
|
|
} else {
|
|
|
|
sublocale = locale.split("-")[0];
|
|
|
|
if (sublocale != locale && locales[sublocale]) {
|
|
|
|
return locales[sublocale];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 00:18:15 +02:00
|
|
|
Always enclose blocks in curly brackets
|
2015-06-19 17:54:49 +02:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-06-16 00:18:15 +02:00
|
|
|
|
2019-04-17 20:40:30 +02:00
|
|
|
When writing a block such as an ``if`` or ``while`` statement, always use
|
2015-10-24 19:18:56 +02:00
|
|
|
curly brackets around that block of code. Even when not strictly required by
|
|
|
|
the compiler (for example if its only one line inside the ``if`` statement).
|
2015-06-16 00:18:15 +02:00
|
|
|
|
|
|
|
For example, like this:
|
|
|
|
|
2019-07-20 21:55:42 +02:00
|
|
|
.. code-block:: javascript
|
2015-06-16 00:18:15 +02:00
|
|
|
|
|
|
|
if (condition === true) {
|
|
|
|
this.updateRoomsList();
|
|
|
|
}
|
|
|
|
somethingElse();
|
|
|
|
|
|
|
|
and NOT like this:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
|
|
|
if (converse.auto_list_rooms)
|
|
|
|
this.updateRoomsList();
|
|
|
|
somethingElse();
|
|
|
|
|
|
|
|
This is to aid in readability and to avoid subtle bugs where certain lines are
|
|
|
|
wrongly assumed to be executed within a block.
|