Deutsch   English   Français   Italiano  
<vcnate$1nr8u$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!2.eu.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Opus <ifonly@youknew.org>
Newsgroups: comp.lang.c
Subject: Re: how to make a macro work as a single line if stmt without braces
Date: Sat, 21 Sep 2024 22:39:10 +0200
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <vcnate$1nr8u$1@dont-email.me>
References: <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk>
 <vcm16e$1hm2u$1@dont-email.me> <vcn6m8$1n1vu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 21 Sep 2024 22:39:11 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="175f026b74d07f159012881db9efd31b";
	logging-data="1830174"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/ELae6xhgHIYIh4tR586e8"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:EeKo6kkHSln59RCcHm1pX+0Gf6c=
Content-Language: en-US
In-Reply-To: <vcn6m8$1n1vu$1@dont-email.me>
Bytes: 3664

On 21/09/2024 21:27, Andrey Tarasevich wrote:
 > 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.
I agree with that, but you realize you are kinda fighting 
"establishment" here. What you call "egyptian" braces is the so-called 
"TBS" which has infected the "default style" of approximately all 
programming languages that use braces. People use that almost 
religiously and the only rationale is to save space. Might have made 
sense 40 years ago when screen estate was a lot smaller, but these days, 
it's just madness. It's a lot less readable. The details are also odd: 
they seem to despise having an opening brace alone on a line, but they 
don't mind the closing brace to be.

This is even worse: } else {
starting a line with a closing symbol and ending it with an opening 
symbol, really?

As to overuse, my own style and guideline is this: if both 'then' and 
'else' blocks are single statements, don't use braces.

If any of these blocks have more than one statement, use braces for both 
to make it look consistent. Mixed braces in if/else do look terrible 
IMHO (and the lack of consistency always hinders readablity), like this:

if (something)
     dothis();
else
{
     dothat();
     andalsothat();
}

Just avoid it.

I've seen that code style guideline in various environments, so this 
isn't just my own either.

Now the rationale for always using braces even for single statements at 
least makes some sense. Usually, it's for avoiding the case when a 
developer will add statements to a if or else block, forget to add 
braces to enclose it, and so that becomes a bug (which can't be easily 
trapped by compilers unless it's between a if and else. Some 
compilers/static analyzers, if you at least used proper indenting, will 
spot the indenting and lack of braces and will warn you about a 
potential mistake though.