JavaScript:SpiderMonkey:Coding Style: Difference between revisions

Jump to navigation Jump to search
Line 228: Line 228:


= C++ =
= C++ =
== Conditionals ==
The following style is used for bracing conditionals:
If the consequent (and, if present, the alternate) is a single statement, no braces are used.
if (today == "Tuesday")
    puts("I don't have my wallet on me.");
else
    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.
if (canSwingFromWeb) {
    p->swingFromWeb();
} else {
    JS_ASSERT(p->isSpiderPig());
    p->doWhateverSpiderPigDoes();
}
Conditions with multi-line tests should put the brace on the new line to provide a visual separation between the condition and the body.
  types::TypeSet *types = frame.extra(lhs).types;
  if (JSOp(*PC) == JSOP_SETPROP && id == types::MakeTypeId(cx, id) &&
      types && !types->unknownObject() &&
      types->getObjectCount() == 1 &&
      types->getTypeObject(0) != NULL &&
      !types->getTypeObject(0)->unknownProperties())
  {
      JS_ASSERT(usePropCache);
      types::TypeObject *object = types->getTypeObject(0);
      types::TypeSet *propertyTypes = object->getProperty(cx, id, false);
      ...
  } else {
      ...
  }
However, if there is already a visual separation between the condition and the body, putting the { on a new line isn't necessary:
  if (forHead->pn_kid1 && NewSrcNote2(cx, cg, SRC_DECL,
                                      (forHead->pn_kid1->isOp(JSOP_DEFVAR))
                                      ? SRC_DECL_VAR
                                      : SRC_DECL_LET) < 0) {
      return false;
  }


== Namespaces ==
== Namespaces ==
Confirmed users
1,345

edits

Navigation menu