Confirmed users, Bureaucrats and Sysops emeriti
419
edits
No edit summary |
|||
Line 22: | Line 22: | ||
if (condition) // OK | if (condition) // OK | ||
== Indentation and | == Indentation and Bracing == | ||
* Function arguments that overflow the first line of the call expression should be aligned to underhang the first argument (to start in overflow lines in the column after the opening parenthesis). | * Function arguments that overflow the first line of the call expression should be aligned to underhang the first argument (to start in overflow lines in the column after the opening parenthesis). | ||
JS_SetContext(rt, // bad | JS_SetContext(rt, // bad | ||
Line 28: | Line 28: | ||
JS_SetContext(rt, // OK | JS_SetContext(rt, // OK | ||
cx); | cx); | ||
* An opening | * An opening brace is placed on the same line as the preceding statement. | ||
if (condition) // bad | if (condition) // bad | ||
{ | { | ||
Line 49: | Line 49: | ||
== Program Flow == | == Program Flow == | ||
* Keep indentation at a minimum. | * Keep indentation at a minimum. | ||
** Prefer return statements | ** Prefer return statements to cast out abnormal cases, instead of nesting "if/else" statements and indenting the common cases. | ||
void MyFunction(int n) { | void MyFunction(int n) { | ||
if (n) { // bad | if (n) { // bad | ||
Line 72: | Line 72: | ||
} | } | ||
DoThat(); | DoThat(); | ||
** Avoid similar arbitrary patterns and non-sequiturs: | |||
if (condition) { // bad | |||
DoThis(); | |||
DoThat(); | |||
} else { | |||
CleanUp(); | |||
return; | |||
} | |||
DoTheOther(); | |||
if (!condition) { // OK | |||
CleanUp(); | |||
return; | |||
} | |||
DoThis(); | |||
DoThat(); | |||
DoTheOther(); | |||
== Comments == | == Comments == | ||
* Always use C style comments. Never use C++ style comments. | * Always use C style comments. Never use C++ style comments. | ||
* Terminate a comment with a period. | * Terminate a comment with a period (so try to make comments be complete sentences). | ||
/* This is a good comment. */ | /* This is a good comment. */ | ||
* Align multiline comments with any indentation, and start every line with an asterisk. Asterisks stack in the same column. Precede the multiline comment with one empty line unless the prior line ends in a left brace. The first line of the comment contains only leading space followed by <code>/*</code>. Multiline comments should also be bracketed. | * Align multiline comments with any indentation, and start every line with an asterisk. Asterisks stack in the same column. Precede the multiline comment with one empty line unless the prior line ends in a left brace. The first line of the comment contains only leading space followed by <code>/*</code>. Multiline comments should also be bracketed. |