Most of us have probably encountered the bug of when you add another statement inside an 'if' and notice that it prints regardless of the condition of the branch, only to find out sooner or later in frustration that the braces around the 'if' condition was missing.
Example:
if (condition) {
statement;
}
Compared to:
if (condition)
statement;
Please note that I wouldn't want a discussion on what coding standard is better, as we all have our opinion about that and it's very much dependent on context. Instead, I would be interested in the advantages from a programming language point of view and from these advantages, why it was decided not to be strictly enforced. Does it make for a simpler grammar? Compiler implementation?
What is the advantage of leaving it optional, rather than strictly enforcing it? What does the standard say about it?
Braces improve the uniformity and readability of code. More important, when inserting an additional statement into a body containing only a single statement, it is easy to forget to add braces because the indentation gives strong (but misleading) guidance to the structure.
In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.
Yes it is not necessary to use curly braces after conditions and loops (functions always need them) IF and only if there is just one statement following. In this case it automatically uses the next statement.
The C Standard says that a if (expression)
must be followed by a statement. It does not require the statement to be a compound statement.
In the "Spirit of C" (see C Rationale document), one of the phrase is:
Trust the programmer.
MISRA-C on the other hand has a required rule for if
and loops that the statement has to be a compound statement.
Some coding styles allow a single statement for if
only if the statement is put in the same line as the if
. For example:
if (condition) statement;
Note that C and C++ are not only the languages to have the {}
be optional in if
. For example in Java it is also optional, but in Perl it is required.
Because:
that would be excessive meddling — the programmer chooses coding style, not the language;
it would further complicate the grammar; easier and more robust to just say that an if (condition)
is followed by a statement
and let the recursion handle everything.
Personally I find the distrust of this construct to be largely without merit for any vaguely competent programmer; I've not once encountered this "bug" where I've forgotten how to write C++ just because I chose to omit a brace pair. If you come across it, I suggest you treat it as a cue to pay more attention to what you're doing, rather than masking it with "tricks" and "style guides".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With