Document ternary expression style and the else-after-return prohibition. No bug, rs=luke over IRC, DONTBUILD because this is modifying docs but touching no code
authorJeff Walden <jwalden@mit.edu>
Thu, 29 Mar 2012 16:03:32 -0700
changeset 94412 160407805dd27d590c4cf2d056d6fe44fd769281
parent 94411 197cccd053e9175750ebd9b02fcd3c6854c193ca
child 94413 38b752214eaa5d4aaa019e737c468ef22eb4f3ad
push id160
push userlsblakk@mozilla.com
push dateFri, 13 Jul 2012 18:18:57 +0000
treeherdermozilla-release@228ba1a111fc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Document ternary expression style and the else-after-return prohibition. No bug, rs=luke over IRC, DONTBUILD because this is modifying docs but touching no code
mfbt/STYLE
--- a/mfbt/STYLE
+++ b/mfbt/STYLE
@@ -37,16 +37,33 @@ goes for references -- & goes by the typ
     int& i = *p;
   }
 
 A corollary: don't mix declaration types by declaring a T and a T* (or a T**,
 &c.) in the same declaration.
 
   T* foo, bar; // BAD
 
+== Expressions ==
+
+Ternary expressions (a ? b : c) should use only one line if sufficiently short.
+Longer ternary expressions should use multiple lines.  The condition,
+consequent, and alternative should each be on separate lines (each part
+overflowing to additional lines as necessary), and the ? and : should be aligned
+with the start of the condition:
+
+  size_t
+  BinaryTree::height()
+  {
+    return isLeaf()
+           ? 0
+           : 1 + std::max(left()->height(),
+                          right()->height());
+  }
+
 == Bracing ==
 
 Don't brace single statements.
 
   if (y == 7)
     return 3;
   for (size_t i = 0; i < 5; i++)
     frob(i);
@@ -342,8 +359,25 @@ interprets that argument.
   enum Enumerability {
     Enumerable,
     NonEnumerable
   };
   bool
   DefineProperty(JSObject* obj, const char* name, Value v, Enumerability e);
 
 Use NULL for the null pointer constant.
+
+If a consequent in an if-statement ends with a return, don't specify an else.
+The else would be redundant with the return, and not using it avoids excess
+indentation.  If you feel the if-else alternation is important as a way to
+think about the choice being made, consider a ternary expression instead.
+
+  // BAD
+  if (f())
+    return 2;
+  else
+    return 5;
+  // GOOD
+  if (f())
+    return 2;
+  return 5;
+  // GOOD
+  return f() ? 2 : 5