JavaScript:SpiderMonkey:C Coding Style: Difference between revisions

Jump to navigation Jump to search
Line 23: Line 23:


== Indentation and Bracketing ==
== Indentation and Bracketing ==
* Function arguments should be lined up aligned with 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
               cx);
               cx);
  JS_SetContext(rt,        // OK
  JS_SetContext(rt,        // OK
               cx);
               cx);
* An opening bracket is placed on the same line as the preceding statement.
* An opening curly brace is placed on the same line as the preceding statement.
  if(condition)             // bad
  if (condition)           // bad
  {
  {
  }
  }
  if(condition) {           // OK
  if (condition) {         // OK
  }
  }
* Do not use multiple statements in one line. Indent a follow-up statement in the next line.
* Do not put compound statements in one line. Indent the controlled statement on the next line, for clarity and break-pointing.
  if(condition) break;     // bad
  if (condition) break;     // bad
  if(condition)             // OK
  if (condition)           // OK
     break;
     break;
* If a statement covers multiple lines, use brackets.
* If a statement or condition covers multiple lines, use braces for all the controlled statements (even if the else part fits on one line, brace it too).
  if(condition)             // bad
  if (condition)           // bad
     CallThisMethod(argument1,
     CallThisMethod(argument1,
                     argument2);
                     argument2);
  if(condition) {           // OK
  if (condition) {         // OK
     CallThisMethod(argument1,
     CallThisMethod(argument1,
                     argument2);
                     argument2);
Confirmed users, Bureaucrats and Sysops emeriti
419

edits

Navigation menu