Re: how to make a macro work as a single line if stmt without braces

Liste des GroupesRevenir à cl c  
Sujet : Re: how to make a macro work as a single line if stmt without braces
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 22. Sep 2024, 12:59:53
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcp0rq$26p7b$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 21/09/2024 21: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.
To be clear here - many people have strong opinions about this topic, and there is no good evidence for claiming that one style is "better" than other styles.  Different styles can have their pros and cons, and people will give different weightings to those pros and cons when choosing a style.
So I fully respect your opinion here.  But you are still wrong :-)

>
if (failed) {
     WARN("failed because...");
} else {
     ok++;
}
>
 Nope. There's absolutely no reason to overuse braces.
I am not suggesting overuse of braces.  I am suggesting /good/ use of them.
<https://www.synopsys.com/blogs/software-security/understanding-apple-goto-fail-vulnerability-2.html>
That's perhaps the most famous example of the havoc caused by omitting useful braces.
Consistency and clarity is important.  So is maintainability.  Suppose, for example, you want to add a line "attempts++;" alongside the "ok++;" line.  When the braces are there, the change is exactly that - add the new line you want.  Without the original braces, you are now making changes to the structural syntax of the code as well when you add in original braces - or you are making a mistake in the code.

 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.
No, it makes code longer and more tedious to read, and wastes significant vertical space so that you can see less code at a time.  It leads people to think that it is a good idea to skip braces when they can.  Put braces in a sensible place and there is no incentive to skip them.
One important point for readability is to keep the conditionals relatively short so that the open brace is not far to the right - an overly complex expression is usually a bad idea anyway.

 
...  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.
 
That is indeed true.  Sometimes when you have code that you need to single-step or debug carefully, you need to express it a little differently or make syntactic changes to help.  (A common one is to mark some of the local variables as "volatile".)
I think it is reasonable to have such short one-line conditionals in simple cases, but it is certainly not a requirement - filling them out with the braces is fine too.

Date Sujet#  Auteur
21 Sep 24 * how to make a macro work as a single line if stmt without braces61Mark Summerfield
21 Sep 24 +- Re: how to make a macro work as a single line if stmt without braces1Lawrence D'Oliveiro
21 Sep 24 +* Re: how to make a macro work as a single line if stmt without braces50David Brown
21 Sep 24 i`* Re: how to make a macro work as a single line if stmt without braces49Andrey Tarasevich
21 Sep 24 i +- Re: how to make a macro work as a single line if stmt without braces1Bart
21 Sep 24 i +- Re: how to make a macro work as a single line if stmt without braces1Opus
21 Sep 24 i +* Re: how to make a macro work as a single line if stmt without braces5Keith Thompson
22 Sep 24 i i`* Re: how to make a macro work as a single line if stmt without braces4Andrey Tarasevich
22 Sep 24 i i +- Re: how to make a macro work as a single line if stmt without braces1Keith Thompson
22 Sep 24 i i +- Re: how to make a macro work as a single line if stmt without braces1David Brown
27 Sep 24 i i `- Re: how to make a macro work as a single line if stmt without braces1Tim Rentsch
22 Sep 24 i +* Re: how to make a macro work as a single line if stmt without braces25David Brown
22 Sep 24 i i`* Re: how to make a macro work as a single line if stmt without braces24Kaz Kylheku
22 Sep 24 i i +* Re: how to make a macro work as a single line if stmt without braces21Bart
22 Sep 24 i i i+* Re: how to make a macro work as a single line if stmt without braces14Michael S
22 Sep 24 i i ii+- Re: how to make a macro work as a single line if stmt without braces1Bart
24 Sep 24 i i ii`* Re: how to make a macro work as a single line if stmt without braces12Tim Rentsch
24 Sep 24 i i ii +* Re: how to make a macro work as a single line if stmt without braces5Andrey Tarasevich
24 Sep 24 i i ii i+- Re: how to make a macro work as a single line if stmt without braces1David Brown
24 Sep 24 i i ii i+* Re: how to make a macro work as a single line if stmt without braces2Bart
24 Sep 24 i i ii ii`- Re: how to make a macro work as a single line if stmt without braces1Keith Thompson
27 Sep 24 i i ii i`- Re: how to make a macro work as a single line if stmt without braces1Tim Rentsch
24 Sep 24 i i ii `* Re: how to make a macro work as a single line if stmt without braces6Bart
24 Sep 24 i i ii  +* Re: how to make a macro work as a single line if stmt without braces2Keith Thompson
24 Sep 24 i i ii  i`- Re: how to make a macro work as a single line if stmt without braces1Bart
24 Sep 24 i i ii  `* Re: how to make a macro work as a single line if stmt without braces3Kaz Kylheku
24 Sep 24 i i ii   `* Re: how to make a macro work as a single line if stmt without braces2Bart
25 Sep 24 i i ii    `- Re: how to make a macro work as a single line if stmt without braces1Kaz Kylheku
22 Sep 24 i i i`* Re: how to make a macro work as a single line if stmt without braces6Keith Thompson
23 Sep 24 i i i `* Re: how to make a macro work as a single line if stmt without braces5David Brown
23 Sep 24 i i i  `* Re: how to make a macro work as a single line if stmt without braces4Richard Harnden
23 Sep 24 i i i   `* Re: how to make a macro work as a single line if stmt without braces3David Brown
23 Sep 24 i i i    `* Re: how to make a macro work as a single line if stmt without braces2Richard Harnden
23 Sep 24 i i i     `- Re: how to make a macro work as a single line if stmt without braces1David Brown
22 Sep 24 i i +- Modern text editor - 'bout time someone paid attention to keeping the thread title relevant!!! (Was: how to make a macro work as a single line if stmt without braces)1Kenny McCormack
22 Sep 24 i i `- Re: how to make a macro work as a single line if stmt without braces1David Brown
28 Sep 24 i `* Re: how to make a macro work as a single line if stmt without braces16Tim Rentsch
28 Sep 24 i  +* Re: how to make a macro work as a single line if stmt without braces5Michael S
29 Sep 24 i  i`* Re: how to make a macro work as a single line if stmt without braces4Tim Rentsch
29 Sep 24 i  i `* Re: how to make a macro work as a single line if stmt without braces3Michael S
30 Sep 24 i  i  +- Re: how to make a macro work as a single line if stmt without braces1Tim Rentsch
30 Sep 24 i  i  `- Re: how to make a macro work as a single line if stmt without braces1Alan Mackenzie
29 Sep 24 i  `* Re: how to make a macro work as a single line if stmt without braces10Andrey Tarasevich
29 Sep 24 i   `* Re: how to make a macro work as a single line if stmt without braces9Tim Rentsch
29 Sep 24 i    `* Re: how to make a macro work as a single line if stmt without braces8Andrey Tarasevich
29 Sep 24 i     +- Re: how to make a macro work as a single line if stmt without braces1Michael S
30 Sep 24 i     +* Re: how to make a macro work as a single line if stmt without braces3Keith Thompson
30 Sep 24 i     i`* Re: how to make a macro work as a single line if stmt without braces2Kaz Kylheku
30 Sep 24 i     i `- Re: how to make a macro work as a single line if stmt without braces1Janis Papanagnou
30 Sep 24 i     +- Re: how to make a macro work as a single line if stmt without braces1Tim Rentsch
30 Sep 24 i     +- Re: how to make a macro work as a single line if stmt without braces1Kaz Kylheku
30 Sep 24 i     `- Re: how to make a macro work as a single line if stmt without braces1David Brown
21 Sep 24 +* Re: how to make a macro work as a single line if stmt without braces2Bart
21 Sep 24 i`- Re: how to make a macro work as a single line if stmt without braces1Mark Summerfield
21 Sep 24 +* Re: how to make a macro work as a single line if stmt without braces2Ike Naar
21 Sep 24 i`- Re: how to make a macro work as a single line if stmt without braces1Keith Thompson
21 Sep 24 +* Re: how to make a macro work as a single line if stmt without braces2Tim Rentsch
22 Sep 24 i`- Re: how to make a macro work as a single line if stmt without braces1Keith Thompson
21 Sep 24 +- Re: how to make a macro work as a single line if stmt without braces1Andrey Tarasevich
21 Sep 24 `* Re: how to make a macro work as a single line if stmt without braces2Blue-Maned_Hawk
24 Sep 24  `- Re: how to make a macro work as a single line if stmt without braces1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal