Sujet : Re: how to make a macro work as a single line if stmt without braces
De : andreytarasevich (at) *nospam* hotmail.com (Andrey Tarasevich)
Groupes : comp.lang.cDate : 21. Sep 2024, 20:27:03
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcn6m8$1n1vu$1@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
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, which makes your code much more readable. It is also a very good location for a comment, if you decide to include one. Your future self with thank you.
... is small
enough to fit entirely on a single line, and the statement could not possibly be misinterpreted, use a macro, or have any other non-obvious behaviour.
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.
-- Best regards,Andrey