summaryrefslogtreecommitdiffstats
path: root/docs/UXP Coding Style.md
blob: c18e5a277cc2ce938f22d0385e1f3aa89f117fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# UXP Coding Style Guide
While our source tree is currently in a number of different Coding Styles and the general rule applies to adhere to style of surrounding code when you are making changes, it is our goal to unify the style of our source tree and making it adhere to a single style.
This document describes the preferred style for new source code files and the preferred style to convert existing files to.

This style guide will not apply to 3rd party libraries that are imported verbatim to our code (e.g. NSS, NSPR, SQLite, various media libs, etc.).

Our own managed and maintained code should adhere to this guide where possible or feasible. It departs from the Mozilla Coding Style in some ways, but considering Mozilla has abandoned their code style in favor of Google code style, we are now defining our own preferred style.

**Important**: if you touch a file to make it adhere to this code style, do not mix code changes with formatting changes. Always make formatting changes throughout a file a single, separate commit that does not touch code functionality!

## General formatting rules
The following formatting rules apply to all code:
- Always use spaces for indentation, never use tabs!
- Put a space between a keyword and parenthesis, e.g. `if (`. Do _not_ put a space between a function/type-assignment name and its parenthesis, e.g. `function(somevar)` or `int32_t(somevar)`.
- Put a space between variables and operators, e.g. `a == b`.
- Put a space after a comma or semicolon in variable lists, e.g. `function(a, b, c)` or `for (i = 1; i < 10; i++)`.
- Indentation of scopes is 2 spaces.
- Indentation of long lines is variable-aligned or expression-aligned (see "long line wrapping")
- Conditional defines are always placed on column 1. This is also true for nested defines.
- Maximum line length is 120 characters. This departs from the often-used 80-character limit which is rooted in the archaic use of 80-column text terminals and should no longer apply in this day. Github, our chief web VCS, has no issues dealing with 120 character lines either.
- Variables passed to functions are passed on one line unless the line would exceed the maximum length, in which case variables will be passed 1-per-line, expression-aligned (see "long line wrapping").
- Avoid trailing commas.
- Comment blocks are line-quoted if they appear within functions.
- Comment blocks are block-quoted if they appear outside of functions (e.g. as function definition headers).

## C and C++
Applies to `*.c`, `*.cc`, `*.cpp` and `*.h`. Potentially also `*.mm` for Mac code.
### General formatting guidelines
- Place function return types, including modifiers like `static` on the same line as the function signature.
  ```C++
  static bool somefunction(int var1, char* var2) {
    ...
  }
  ```
- When using templates, do not add spaces between the template name and its object.
  ```C++
  nsCOMPtr<nsISomeInterfaceType>
  ```
- Use braces, even for single-command lines. Avoid placing executed code on the expression line or using single-line indentation to indicate the scope. Always brace executed code.

  WRONG:
  ```C++
  if (a == b) do_something();
  if (a == b)
    do_something();
  if (a == b) { do_something(); }
  ```
  CORRECT:
  ```C++
  if (a == b) {
    do_something();
  }
  ```
- Pointer types: When declaring pointer types, place the `*` with the pointer type. When referencing pointer types, place `*` or `&` with the variable. Do not use `*` by itself with whitespace. Some existing modules still use the `int *myvar1;` style, these should be converted. Until conversion is done, follow existing, surrounding code style or convert it when you touch a file.

  WRONG:
  ```C++
  int * myvar;
  int *myvar1, *myvar2;
  int* myvar1, *myvar2;
  int* myvar1, myvar2; //myvar2 isn't a pointer!
  ```
  CORRECT:
  ```C++
  char* somePointer;
  int* myvar1;
  int* myvar2;
  function a(char* somePointer) [...]
  myString = *somePointer;
  myPointer = &myString;
  ```

### Flow control
Flow control expressions should follow the following guidelines:
- Scopes have their opening braces on the expression line
- Scopes have their closing braces on a separate line, indent-aligned with the flow control expression
- Any alternative flow control paths are generally started with an expression on the closing brace line
- Case statements are indented by 2 on a new line with the case expression on its own line.
- Flow control default scopes are always placed at the bottom.
#### if..else
`if..else` statements example:
```C++
if (something == somethingelse) {
  true_case_code here;
} else {
  false_case_code here;
}

if (case1) {
  case1_code here;
} else if (case2) {
  case2_code here;
} else {
  other_case_code here;
}

if (case1) {
  case1_code here;
} else {
  if (case2) {
    case2_code here;
  }
  case2_and_other_code here;
}
```
#### for
`for` loop example:
```C++
for (i = 1; i < max; i++) {
  loop_code here;
}
```
#### while
`while` loop example:
```C++
while (something == true) {
  loop_code here;
}
```
#### switch..case
`switch..case` flow control example:
```C++
switch (variable) {
  case value1:
    // Comment describing 1
    code_for_1;
    code_for_1;
    break;
  case value2:
    code_for_2;
    code_for_2;
    // fallthrough
  case value3:
  case value4:
    code_for_3_and_4;
    break;
  default:
    code_for_default;
    code_for_default;
}
```
### Classes
Classes have some special formatting rules applied to them:
- Classes follow the following sequence: CTOR, DTOR, methods, variables
- Class CTOR variable initializers (in lieu of assignments in the CTOR body) are listed one-per-line with a leading comma (or colon for the first).
- `public`, `private` and `protected` keywords are not indented and are at the same indentation level as the class identifier statement.

Example class definition:
```C++
class FocusClassExample : public Runnable {
public:
  FocusClassExample(nsISupports* aTarget,
                    EventMessage aEventMessage,
                    nsPresContext* aContext,
                    nsPIDOMWindowOuter* aOriginalFocusedWindow,
                    nsIContent* aOriginalFocusedContent,
                    EventTarget* aRelatedTarget)
    : mTarget(aTarget)
    , mContext(aContext)
    , mEventMessage(aEventMessage)
    , mOriginalFocusedWindow(aOriginalFocusedWindow)
    , mOriginalFocusedContent(aOriginalFocusedContent)
    , mRelatedTarget(aRelatedTarget) {
    CTOR_initializer_code here
  }
  
  ~FocusClassExample {
    DTOR_code here
  }

  NS_IMETHOD Run() override {
    nsCOMPtr<nsIContent> originalWindowFocus = mOriginalFocusedWindow ?        
                                               mOriginalFocusedWindow->GetFocusedNode() :
                                               nullptr;
    // Blink does not check that focus is the same after blur, but WebKit does.
    // Opt to follow Blink's behavior (see bug 687787).
    if (mEventMessage == eFocusOut || originalWindowFocus == mOriginalFocusedContent) {
      InternalFocusEvent event(true, mEventMessage);
      event.mFlags.mBubbles = true;
      event.mFlags.mCancelable = false;
      event.mRelatedTarget = mRelatedTarget;
      return EventDispatcher::Dispatch(mTarget, mContext, &event);
    }
    return NS_OK;
  }

  nsCOMPtr<nsISupports> mTarget;
  RefPtr<nsPresContext> mContext;
private:
  EventMessage mEventMessage;
  nsCOMPtr<nsPIDOMWindowOuter> mOriginalFocusedWindow;
  nsCOMPtr<nsIContent> mOriginalFocusedContent;
  nsCOMPtr<EventTarget> mRelatedTarget;
};
```
### Long line wrapping
If statements on a single line become overly long, they should be split into multiple lines:
- Binary operators (including ternary) must be left on their original lines if the line break happens around the operator. The second line should start in the same column as the start of the expression in the first line.
- Lists of variables (e.g. when calling or declaring a function) should be split so variables are listed one-per-line, where second and subsequent lines start in the same column as the first variable listed, even if more than one variable would fit on the line segment until the wrapping column.

WRONG:
```C++
somelongnamespace::somelongfunction(var1, var2, var3,
                                    var4, var5);
somelongnamespace::somelongfunction(
  var1, var2, var3, var4, var5);
if (somelongvariable == somelongothervariable
  || somelongvariable2 == somelongothervariable2) {
somelongvariable = somelongexpression ? somevalue1
  : somevalue2;
```
CORRECT:
```C++
somelongnamespace::somelongfunction(var1,
                                    var2,
                                    var3,
                                    var4,
                                    var5);
if (somelongvariable == somelongothervariable ||
    somelongvariable2 == somelongothervariable2) {
somelongvariable = somelongexpression ?
                   somevalue1 :
                   somevalue2;
```
 
## JavaScript
Applies to `*.js` and `*.jsm`.
### General formatting guidelines
- Place function signatures on the same line as js object identifiers, and do not use named function syntax in that case:
  ```JavaScript
  var myObject = {
    myIdentifier: function(var1, var2) {
      ...
    },
    myIdentifier2: function() {
      ...
    }
  }
  ```
- Variable initializers should be placed on the variable declaration line if needed.
  ```JavaScript
  var fwdCommand = document.getElementById("Browser:Forward");
  ```
  ```JavaScript
  var myObject = {
    myVariable: null,
    myString: "Something"
  }
  ```
- Avoid placing executed code on the expression line. Preferably brace executed code.

  WRONG:
  ```JavaScript
  if (a == b) do_something();
  if (a == b) { do_something(); }
  ```
  DISCOURAGED:
  ```JavaScript
  if (a == b)
    do_something();
  ```
  CORRECT:
  ```JavaScript
  if (a == b) {
    do_something();
  }
  ```
- Format in-lined variable functions as if you would format normal functions with the exception of closure: break with the opening brace, followed by code, and closing brace on its own line, followed immediately by any other variables passed in-line.
  ```JavaScript
  appMenuButton.addEventListener("mousedown", function(event) {
      if (event.button == 0) {
        appMenuOpening = new Date();
      }
    }, false);
  ```
- Use a single space between `{` `}` braces in empty js objects.

### Flow control
Flow control expressions should follow the following guidelines:
- Scopes have their opening braces on the expression line
- Scopes have their closing braces on a separate line, indent-aligned with the flow control expression
- Any alternative flow control paths are generally started with an expression on the closing brace line. The logic behind this is that you should keep the flow control level visually the same for the executed code blocks with no breaks (a closing brace on its own line means "done").
- Case statements are indented by 2 on a new line with the case expression on its own line.
- Flow control default scopes are always placed at the bottom.

#### if..else
`if..else` statements example:
```JavaScript
if (something == somethingelse) {
  true_case_code here;
} else {
  false_case_code here;
}

if (case1) {
  case1_code here;
} else if (case2) {
  case2_code here;
} else {
  other_case_code here;
}

if (case1) {
  case1_code here;
} else {
  if (case2) {
    case2_code here;
  }
  case2_and_other_code here;
}
```
#### for
`for` loop example:
```JavaScript
for (let i = 1; i < max; i++) {
  loop_code here;
}

for (let i = 0;; i++) {
   do_something_with(i);
   if (i > max) break;
   ...
}
```
#### while
`while` loop example:
```JavaScript
while (something == true) {
  loop_code here;
}

do {
  execute_me_at_least_once();
  ...
} while (something == true);
```
#### switch..case
`switch..case` flow control example:
```JavaScript
switch (variable) {
  case value1:
    // Comment describing 1
    code_for_1;
    code_for_1;
    break;
  case value2:
    code_for_2;
    code_for_2;
    // fallthrough
  case value3:
  case value4:
    code_for_3_and_4;
    break;
  default:
    code_for_default;
    code_for_default;
}
```
#### try..catch
- When using `try..catch` blocks, the use of optional catch binding is discouraged. Please always include the error variable.

`try..catch` flow control examples:
```JavaScript
try {
  do_something();
} catch(e) {
  handle_error(e);
} finally {
  always();
}

try {
  do_something();
} catch(e) {
}

try {
  do_something();
} finally {
  always();
}
```
DISCOURAGED
```JavaScript
try {
  do_something();
} catch {
  // No error processing
  do_something_else();
}

// No closing brace on its own line
try {
  do_something();
} catch(ex) { }
```

### Long line wrapping
If statements on a single line become overly long, they should be split into multiple lines:
- Binary operators (including ternary) must be left on their original lines if the line break happens around the operator. The second line should start in the same column as the start of the expression in the first line.
- Lists of variables (e.g. when calling or declaring a function) should be split at the wrapping column.
- Long OOP calls should be split at the period with the period on the start of the new line, indented to the column of the first object, filling to the wrapping column where possible.

WRONG:
```JavaScript
somelongobjectname.somelongfunction(
  var1, var2, var3, var4, var5);

if (somelongvariable == somelongothervariable
  || somelongvariable2 == somelongothervariable2) {

somelongvariable = somelongexpression ? somevalue1
  : somevalue2;

Cu.import("resource:///modules/DownloadsCommon.jsm", {}).
          DownloadsCommon.initializeAllDataLinks();
```
CORRECT:
```JavaScript
somelongobjectname.somelongfunction(var1, var2, var3,
                                    var4, var5);

if (somelongvariable == somelongothervariable ||
    somelongvariable2 == somelongothervariable2) {

somelongvariable = somelongexpression ?
                   somevalue1 :
                   somevalue2;

Cu.import("resource:///modules/DownloadsCommon.jsm", {})
  .DownloadsCommon.initializeAllDataLinks();

let sessionStartup = Cc["@mozilla.org/browser/sessionstartup;1"]
                       .getService(Ci.nsISessionStartup);
```

## XUL and other XML-derivatives 
Applies to `*.xul`, `*.html`, `*.xhtml`.
## IDL
Applies to `*.idl`, `*.xpidl` and `*.webidl`.