JavaScript:SpiderMonkey:C Coding Style: Difference between revisions

Jump to navigation Jump to search
m
C-style comments, per this document ;-)
m (C-style comments, per this document ;-))
Line 6: Line 6:
* Function return types are on a separate line preceding the function name.
* Function return types are on a separate line preceding the function name.
* Function braces go on the line following the function name.
* Function braces go on the line following the function name.
  void DoThis()          // bad
  void DoThis()          /* bad */
  {
  {
     ...
     ...
  }
  }
  void
  void
  DoThis()                // OK
  DoThis()                /* OK */
  {
  {
     ...
     ...
Line 37: Line 37:
* Comment /* FALL THROUGH */ in place of missing break when intentionally falling through from one case-controlled statement sequence into another, or into the default statements.
* Comment /* FALL THROUGH */ in place of missing break when intentionally falling through from one case-controlled statement sequence into another, or into the default statements.
* Do not use spaces between a function name and its arguments list,                                                or between an array name and the square bracket. Also, do no use spaces after a bracket. Use a space after a comma to separate arguments.
* Do not use spaces between a function name and its arguments list,                                                or between an array name and the square bracket. Also, do no use spaces after a bracket. Use a space after a comma to separate arguments.
  JS_SetContext ( rt, cx ); // bad
  JS_SetContext ( rt, cx ); /* bad */
  JS_SetContext(rt, cx);    // OK
  JS_SetContext(rt, cx);    /* OK */
* Use a space between a C keyword and parentheses.
* Use a space between a C keyword and parentheses.
  if(condition)            // bad
  if(condition)            /* bad */
  if (condition)            // OK
  if (condition)            /* OK */


== Indentation and Bracing ==
== 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 */
               cx);
               cx);
  JS_SetContext(rt,        // OK
  JS_SetContext(rt,        /* OK */
               cx);
               cx);
* An opening brace is placed on the same line as the preceding statement.
* An opening 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 put compound statements in one line. Indent the controlled statement on the next line, for clarity and break-pointing.
* 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 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 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);
Line 73: Line 73:
  MyFunction(int n)
  MyFunction(int n)
  {
  {
     if (n) {              // bad
     if (n) {              /* bad */
         ...
         ...
     }
     }
Line 81: Line 81:
  {
  {
     if(!n)
     if(!n)
         return;          // OK
         return;          /* OK */
     ...
     ...
  }
  }


* If an "if" statement controls a "then" clause ending in a return statement, do not use "else" after return.
* If an "if" statement controls a "then" clause ending in a return statement, do not use "else" after return.
  if (condition) {          // bad
  if (condition) {          /* bad */
     DoThis();
     DoThis();
     return;
     return;
Line 92: Line 92:
  else
  else
     DoThat();
     DoThat();
  if (condition) {          // OK
  if (condition) {          /* OK */
     DoThis();
     DoThis();
     return;
     return;
Line 99: Line 99:


* Avoid similar arbitrary patterns and non-sequiturs:
* Avoid similar arbitrary patterns and non-sequiturs:
  if (condition) {          // bad
  if (condition) {          /* bad */
     DoThis();
     DoThis();
     DoThat();
     DoThat();
Line 107: Line 107:
  }
  }
  DoTheOther();
  DoTheOther();
  if (!condition) {        // OK
  if (!condition) {        /* OK */
     CleanUp();
     CleanUp();
     return;
     return;
Confirmed users
446

edits

Navigation menu