JavaScript:SpiderMonkey:Coding Style: Difference between revisions

Jump to navigation Jump to search
Update braces style per bug 1488698
(link to directories)
(Update braces style per bug 1488698)
Line 71: Line 71:
  if(condition)            /* bad */
  if(condition)            /* bad */
  if (condition)            /* OK */
  if (condition)            /* OK */
* 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.
* Always use braces for conditionals, even if the condition and consequent occupy single lines.
  if (today == "Tuesday")
 
     puts("I don't have my wallet on me.");
  if (!foo) {
  else
     return false;
    puts("I would gladly pay you on Tuesday for a hamburger today.");
  }
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();
Line 122: Line 122:
     doStuff();
     doStuff();
  }
  }
  for (int i = 0; i < 5; i++) /* OK */
  for (int i = 0; i < 5; i++) { /* OK */
     doStuff();
     doStuff();
}
  for (size_t ind = JSObject::JSSLOT_DATE_COMPONENTS_START;
  for (size_t ind = JSObject::JSSLOT_DATE_COMPONENTS_START;
       ind < JSObject::DATE_FIXED_RESERVED_SLOTS; ind++) {  /* bad */
       ind < JSObject::DATE_FIXED_RESERVED_SLOTS; ind++) {  /* bad */
Line 148: Line 149:
  MyFunction(int n)
  MyFunction(int n)
  {
  {
     if (!n)
     if (!n) {
         return;          /* OK */
         return;          /* OK */
    }
     ...
     ...
  }
  }
Line 186: Line 188:
* Avoid using && or || to mix deciding-whether-to-do-something with error-checking.
* Avoid using && or || to mix deciding-whether-to-do-something with error-checking.


  if (obj->hasProblems() && !obj->rectify())     /* bad */
  if (obj->hasProblems() && !obj->rectify()) /* bad */
     return false;
     return false;
}
   
   
  if (obj->hasProblems()) {                      /* OK */
  if (obj->hasProblems()) {                      /* OK */
     if (!obj->rectify())
     if (!obj->rectify()) {
         return false;
         return false;
    }
  }
  }


Confirmed users
33

edits

Navigation menu