JavaScript:SpiderMonkey:Coding Style: Difference between revisions

Jump to navigation Jump to search
Language about bracing multi-line stuff inexplicably doesn't track long-standing practice -- fix it to
(Reflect change from |T *t| to |T* t|. Also remove the edge cases that are obvious with the new style.)
(Language about bracing multi-line stuff inexplicably doesn't track long-standing practice -- fix it to)
Line 67: Line 67:
  if(condition)            /* bad */
  if(condition)            /* bad */
  if (condition)            /* OK */
  if (condition)            /* OK */
* In a conditional, if the consequent (and, if present, the alternate) is a single statement, no braces are used.
* In a conditional, if the condition and consequent (and, if present, any number of <code>else if</code>/<code>else</code> alternatives and associated conditions) occupy single lines, no braces are used.
  if (today == "Tuesday")
  if (today == "Tuesday")
     puts("I don't have my wallet on me.");
     puts("I don't have my wallet on me.");
  else
  else
     puts("I would gladly pay you on Tuesday for a hamburger today.");
     puts("I would gladly pay you on Tuesday for a hamburger today.");
However, if ''either'' the consequent or alternate is a block of multiple statements, braces are used on both.
However, if ''any'' condition occupies multiple lines, or if a consequent or alternative occupies multiple lines (regardless whether it's a single statement or multiple statements), every consequent and alternative is braced.
  if (canSwingFromWeb) {
  if (canSwingFromWeb) {
     p->swingFromWeb();
     p->swingFromWeb();
  } else {
  } else {
     JS_ASSERT(p->isSpiderPig());
     MOZ_ASSERT(p->isSpiderPig());
     p->doWhateverSpiderPigDoes();
     p->doWhateverSpiderPigDoes();
}
if (apingOtherMonkey) {
    MOZ_ASSERT(monkey->wantsAttention(),
                "why else would he be aping another monkey?");
}
if (hungryForBananas && // and a comment makes the line long enough for a linebreak
    wantsAttentionInTheWorstWay)
{
    p->eatBananasAndPreen();
  }
  }
* Conditions with multi-line tests should put the brace on the new line to provide a visual separation between the condition and the body.
* Conditions with multi-line tests should put the brace on the new line to provide a visual separation between the condition and the body.
Confirmed users
446

edits

Navigation menu