@@ -669,15 +669,15 @@ copy.
669
669
.. code-block :: c++
670
670
671
671
// Typically there's no reason to copy.
672
- for (const auto &Val : Container) { observe(Val); }
673
- for (auto &Val : Container) { Val.change(); }
672
+ for (const auto &Val : Container) observe(Val);
673
+ for (auto &Val : Container) Val.change();
674
674
675
675
// Remove the reference if you really want a new copy.
676
676
for (auto Val : Container) { Val.change(); saveSomewhere(Val); }
677
677
678
678
// Copy pointers, but make it clear that they're pointers.
679
- for (const auto *Ptr : Container) { observe(*Ptr); }
680
- for (auto *Ptr : Container) { Ptr->change(); }
679
+ for (const auto *Ptr : Container) observe(*Ptr);
680
+ for (auto *Ptr : Container) Ptr->change();
681
681
682
682
Beware of non-determinism due to ordering of pointers
683
683
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -884,7 +884,7 @@ It is much preferred to format the code like this:
884
884
.. code-block :: c++
885
885
886
886
Value *doSomething(Instruction *I) {
887
- // Terminators never need 'something' done to them because ...
887
+ // Terminators never need 'something' done to them because ...
888
888
if (I->isTerminator())
889
889
return 0;
890
890
@@ -896,7 +896,7 @@ It is much preferred to format the code like this:
896
896
// This is really just here for example.
897
897
if (!doOtherThing(I))
898
898
return 0;
899
-
899
+
900
900
... some long code ....
901
901
}
902
902
@@ -1000,7 +1000,7 @@ Or better yet (in this case) as:
1000
1000
Type = Context.getsigjmp_bufType();
1001
1001
else
1002
1002
Type = Context.getjmp_bufType();
1003
-
1003
+
1004
1004
if (Type.isNull()) {
1005
1005
Error = Signed ? ASTContext::GE_Missing_sigjmp_buf :
1006
1006
ASTContext::GE_Missing_jmp_buf;
@@ -1010,7 +1010,7 @@ Or better yet (in this case) as:
1010
1010
1011
1011
The idea is to reduce indentation and the amount of code you have to keep track
1012
1012
of when reading the code.
1013
-
1013
+
1014
1014
Turn Predicate Loops into Predicate Functions
1015
1015
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1016
1016
@@ -1081,7 +1081,7 @@ In general, names should be in camel case (e.g. ``TextFileReader`` and
1081
1081
* **Variable names ** should be nouns (as they represent state). The name should
1082
1082
be camel case, and start with an upper case letter (e.g. ``Leader `` or
1083
1083
``Boats ``).
1084
-
1084
+
1085
1085
* **Function names ** should be verb phrases (as they represent actions), and
1086
1086
command-like function should be imperative. The name should be camel case,
1087
1087
and start with a lower case letter (e.g. ``openFile() `` or ``isFoo() ``).
@@ -1091,7 +1091,7 @@ In general, names should be in camel case (e.g. ``TextFileReader`` and
1091
1091
discriminator for a union, or an indicator of a subclass. When an enum is
1092
1092
used for something like this, it should have a ``Kind `` suffix
1093
1093
(e.g. ``ValueKind ``).
1094
-
1094
+
1095
1095
* **Enumerators ** (e.g. ``enum { Foo, Bar } ``) and **public member variables **
1096
1096
should start with an upper-case letter, just like types. Unless the
1097
1097
enumerators are defined in their own small namespace or inside a class,
@@ -1107,7 +1107,7 @@ In general, names should be in camel case (e.g. ``TextFileReader`` and
1107
1107
MaxSize = 42,
1108
1108
Density = 12
1109
1109
};
1110
-
1110
+
1111
1111
As an exception, classes that mimic STL classes can have member names in STL's
1112
1112
style of lower-case words separated by underscores (e.g. ``begin() ``,
1113
1113
``push_back() ``, and ``empty() ``). Classes that provide multiple
@@ -1359,7 +1359,7 @@ prefer it.
1359
1359
The use of ``#include <iostream> `` in library files is hereby **forbidden **,
1360
1360
because many common implementations transparently inject a `static constructor `_
1361
1361
into every translation unit that includes it.
1362
-
1362
+
1363
1363
Note that using the other stream headers (``<sstream> `` for example) is not
1364
1364
problematic in this regard --- just ``<iostream> ``. However, ``raw_ostream ``
1365
1365
provides various APIs that are better performing for almost every use than
@@ -1491,7 +1491,7 @@ being closed by a ``}``. For example:
1491
1491
public:
1492
1492
explicit Grokable() { ... }
1493
1493
virtual ~Grokable() = 0;
1494
-
1494
+
1495
1495
...
1496
1496
1497
1497
};
@@ -1540,8 +1540,8 @@ as possible, and only use them for class declarations. For example:
1540
1540
};
1541
1541
} // end anonymous namespace
1542
1542
1543
- static void runHelper() {
1544
- ...
1543
+ static void runHelper() {
1544
+ ...
1545
1545
}
1546
1546
1547
1547
bool StringSort::operator<(const char *RHS) const {
@@ -1569,6 +1569,53 @@ you have no immediate way to tell if this function is local to the file. In
1569
1569
contrast, when the function is marked static, you don't need to cross-reference
1570
1570
faraway places in the file to tell that the function is local.
1571
1571
1572
+ Don't Use Braces on Simple Single-Statement Bodies of if/else/loop Statements
1573
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1574
+
1575
+ When writing the body of an ``if ``, ``else ``, or loop statement, omit the braces to
1576
+ avoid unnecessary line noise. However, braces should be used in cases where the
1577
+ omission of braces harm the readability and maintainability of the code.
1578
+
1579
+ Readability is harmed when a single statement is accompanied by a comment that loses
1580
+ its meaning if hoisted above the ``if `` or loop statement. Similarly, braces should
1581
+ be used when single-statement body is complex enough that it becomes difficult to see
1582
+ where the block containing the following statement began. An ``if ``/``else `` chain or
1583
+ a loop is considered a single statement for this rule, and this rule applies recursively.
1584
+ This list is not exhaustive, for example, readability is also harmed if an
1585
+ ``if ``/``else `` chain starts using braced bodies partway through and does not continue
1586
+ on with braced bodies.
1587
+
1588
+ Maintainability is harmed if the body of an ``if `` ends with a (directly or indirectly)
1589
+ nested ``if `` statement with no ``else ``. Braces on the outer ``if `` would help to avoid
1590
+ running into a "dangling else" situation.
1591
+
1592
+
1593
+ Note that comments should only be hoisted for loops and
1594
+ ``if ``, and not in ``else if `` or ``else ``, where it would be unclear whether the comment
1595
+ belonged to the preceeding condition, or the ``else ``.
1596
+
1597
+ .. code-block :: c++
1598
+
1599
+ // Omit the braces, since the body is simple and clearly associated with the if.
1600
+ if (isa<FunctionDecl>(D))
1601
+ handleFunctionDecl(D);
1602
+ else if (isa<VarDecl>(D))
1603
+ handleVarDecl(D);
1604
+ else {
1605
+ // In this else case, it is necessary that we explain the situation with this
1606
+ // surprisingly long comment, so it would be unclear without the braces whether
1607
+ // the following statement is in the scope of the else.
1608
+ handleOtherDecl(D);
1609
+ }
1610
+
1611
+ // This should also omit braces. The for loop contains only a single statement,
1612
+ // so it shouldn't have braces. The if also only contains a single statement (the
1613
+ // for loop), so it also should omit braces.
1614
+ if (isa<FunctionDecl>(D))
1615
+ for(auto *A : D.attrs())
1616
+ handleAttr(A);
1617
+
1618
+
1572
1619
See Also
1573
1620
========
1574
1621
0 commit comments