diff --git a/mfbt/STYLE b/mfbt/STYLE index fe2bcd811e8..2d94c3ae282 100644 --- a/mfbt/STYLE +++ b/mfbt/STYLE @@ -42,6 +42,23 @@ A corollary: don't mix declaration types by declaring a T and a T* (or a T**, 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. @@ -347,3 +364,20 @@ interprets that argument. 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