Skip to content

Commit 48ca60e

Browse files
kazutakahirataaokblast
authored andcommitted
[Docs] Clarify the brace policy for if/else/loop statements (llvm#164570)
Without this patch, the five-paragraph section is unclear about exactly when to use and not to use braces. Specifically, the first paragraph suggests omitting braces, and then subsequent paragraphs carve out exceptions. At the end, it's unclear what situations remain for omitting braces. This patch overhauls the text for readability. Specifically, it first describes when to omit braces and then lists cases where we should retain braces.
1 parent 6dce5e7 commit 48ca60e

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

llvm/docs/CodingStandards.rst

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,29 +1692,29 @@ faraway places in the file to tell that the function is local:
16921692
Don't Use Braces on Simple Single-Statement Bodies of if/else/loop Statements
16931693
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16941694

1695-
When writing the body of an ``if``, ``else``, or for/while loop statement, we
1696-
prefer to omit the braces to avoid unnecessary line noise. However, braces
1697-
should be used in cases where the omission of braces harms the readability and
1698-
maintainability of the code.
1695+
When writing the body of an ``if``, ``else``, or ``for``/``while`` loop
1696+
statement, we aim to reduce unnecessary line noise.
16991697

1700-
We consider that readability is harmed when omitting the brace in the presence
1701-
of a single statement that is accompanied by a comment (assuming the comment
1702-
can't be hoisted above the ``if`` or loop statement, see below).
1698+
**Omit braces when:**
17031699

1704-
Similarly, braces should be used when a single-statement body is complex enough
1705-
that it becomes difficult to see where the block containing the following
1706-
statement began. An ``if``/``else`` chain or a loop is considered a single
1707-
statement for this rule, and this rule applies recursively.
1700+
* The body consists of a single **simple** statement.
1701+
* The single statement is not preceded by a comment.
1702+
(Hoist comments above the control statement if you can.)
1703+
* An ``else`` clause, if present, also meets the above criteria (single
1704+
simple statement, no associated comments).
17081705

1709-
This list is not exhaustive. For example, readability is also harmed if an
1710-
``if``/``else`` chain does not use braced bodies for either all or none of its
1711-
members, or has complex conditionals, deep nesting, etc. The examples below
1712-
intend to provide some guidelines.
1706+
**Use braces in all other cases, including:**
17131707

1714-
Maintainability is harmed if the body of an ``if`` ends with a (directly or
1715-
indirectly) nested ``if`` statement with no ``else``. Braces on the outer ``if``
1716-
would help to avoid running into a "dangling else" situation.
1708+
* Multi-statement bodies
1709+
* Single-statement bodies with non-hoistable comments
1710+
* Complex single-statement bodies (e.g., deep nesting, complex nested
1711+
loops)
1712+
* Inconsistent bracing within ``if``/``else if``/``else`` chains (if one
1713+
block requires braces, all must)
1714+
* ``if`` statements ending with a nested ``if`` lacking an ``else`` (to
1715+
prevent "dangling else")
17171716

1717+
The examples below provide guidelines for these cases:
17181718

17191719
.. code-block:: c++
17201720

0 commit comments

Comments
 (0)