Sujet : Re: how to make a macro work as a single line if stmt without braces
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 21. Sep 2024, 21:11:21
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcn997$1nfpt$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 21/09/2024 20:27, Andrey Tarasevich wrote:
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.
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
{
...
}
The vertical spacing introduced by the `{` line provides separation between condition and the branch,
Have a look at the middle of that example:
} else {
Here the braces serve no purpose; you already have 'else' separating the two branches to which you can add extra vertical space if you like.
This is not just because of braces, you get the same thing with:
end else begin
Both are symptoms of the idea that any branch can only have a single statement, and if you need more, than you need a compound statement using {s1; s2; s3;} (unless you're Tim then you use s1, s2, s3 which may or may not work and is probably the worst of all).
Since this is C, then if braces are to be mandatory, then at least allow people to write their code a little more compactly if they wish. In a language that allows proper statement sequences, you wouldn't commonly see this:
if failed then
s1
else
s2
...
But this exactly how you are suggesting C should be written, when looking purely at the content: 50% blank lines.
It looks ridiculous and those statements look very lonely!