Sujet : Re: how to make a macro work as a single line if stmt without braces
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 21. Sep 2024, 22:54:52
Autres entêtes
Organisation : None to speak of
Message-ID : <87frpsr7tf.fsf@nosuchdomain.example.com>
References : 1 2 3
User-Agent : Gnus/5.13 (Gnus v5.13)
Andrey Tarasevich <
andreytarasevich@hotmail.com> writes:
On 09/21/24 1:47 AM, David Brown wrote:
You should get in the habit of being consistent with braces, and
being generous with them. Your future self with thank you.
if (failed) {
WARN("failed because...");
} else {
ok++;
}
>
Nope. There's absolutely no reason to overuse braces.
Plenty of C programmers, myself included, disagree with this.
One reason to "overuse" braces is that you can easily add another
statement. If you write:
if (failed)
WARN("failed because...");
else
ok++;
and later decide you need two statements in the else clause, you then
need to add braces. If they're already there, you don't.
And don't use "Egyptian" braces. The latter is actually an
ill-begotten attempt to remedy the damage from the former. Just stop
overusing braces, and there'll be no need for the remedy. Easy.
>
This is the proper formatting style with braces
>
if (failed)
{
...
}
else
{
...
}
Again, many, perhaps most, C programmers will disagree with this.
What you call "Egyptian" braces is the style used by the creators of the
language and in a *lot* of open source software. Even if you don't like
the style, you'll need to deal with it.
I have my own fairly strong preferences about brace placement, but the
most important rule is to follow the conventions of the code I'm working
on.
[...]
Thus :
if (failed) return -1; // Early exit
or
if (!failed) ok++;
Otherwise, put in all the braces.
>
Nope. Do not ever write single-line `if` statements. This is a major
impediment to step-by-step debugging.
I've rarely run into that problem. When I have, it's been easy enough
to temporarily modify the code.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */