Path: ...!3.eu.feeder.erje.net!feeder.erje.net!news.in-chemnitz.de!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown Newsgroups: comp.lang.c Subject: Re: how to make a macro work as a single line if stmt without braces Date: Sun, 22 Sep 2024 13:59:53 +0200 Organization: A noiseless patient Spider Lines: 97 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 22 Sep 2024 13:59:54 +0200 (CEST) Injection-Info: dont-email.me; posting-host="92d2f9b37acfcaeb5a5d0a5a62bab213"; logging-data="2319595"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1866RCxCJUo34UcFDhE+Au/v3qlVoCkG9o=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:JOUUHaipTlOvxghfFo8yH4KQpkY= In-Reply-To: Content-Language: en-GB Bytes: 4697 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. 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.