Deutsch English Français Italiano |
<vcup01$38rb3$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.roellig-ltd.de!open-news-network.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: how to make a macro work as a single line if stmt without braces Date: Tue, 24 Sep 2024 17:22:25 +0100 Organization: A noiseless patient Spider Lines: 87 Message-ID: <vcup01$38rb3$1@dont-email.me> References: <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk> <vcm16e$1hm2u$1@dont-email.me> <vcn6m8$1n1vu$1@dont-email.me> <vcp0rq$26p7b$1@dont-email.me> <20240922080605.59@kylheku.com> <vcpeo0$28shf$1@dont-email.me> <20240922192726.000061fc@yahoo.com> <86ikul6ruw.fsf@linuxsc.com> <vcujmj$384dh$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 24 Sep 2024 18:22:25 +0200 (CEST) Injection-Info: dont-email.me; posting-host="95ee115af698825001a4f77256ea5076"; logging-data="3435875"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DuyAbbpjrHHuZSY5NUzJ3" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:AMM2cvnXN2w/AYH+zK5uMt14a6s= In-Reply-To: <vcujmj$384dh$1@dont-email.me> Content-Language: en-GB Bytes: 4313 On 24/09/2024 15:52, Andrey Tarasevich wrote: > On 09/24/24 7:36 AM, Tim Rentsch wrote: >> >> My long-standing habit is to write this >> >> if(x) bar(x); >> >> or this >> >> if(x){ >> bar(x); >> } >> >> but never this >> >> if(x) >> bar(x); >> >> The reason for this habit is that many years ago I got bitten by >> trying to add a line in the last case. The habit subsequently >> adopted has proven very effective at eliminating such errors. >> > > It is just weird. > > This is no different from insisting in using "Yoda conditions", claiming > that "many years ago I was bitten by an accidental = in place of ==". In > reality we all know that regardless of how many scary stories someone > might tell about dangers of accidental assignment in place of > comparison, it just does not happen. There's no such issue. People just > don't make this mistake. > > The same applies to > > if(x) > bar(x); > > This is the right thing to do. It is the most readable and elegant way > to write a simple `if`. Well, it is the ONLY way C provides to write the branches of an if-statement. Each only takes a single statement. For more than one statement in a branch, they have to be enclosed in a compound statement with braces. > And the dreaded "one day you will add a line and > suffer" just doesn't happen. There's no such issue. No, this must happen ALL THE TIME. That is, you write one statement, then decide you need more than one. Or you start with 3 statements and get rid of two of them. So, what happens: is there are some of sort dance where you add {,} braces when N increases beyond 1, and remove them when N reaches 1 again? What happens when N changes from 1 to 0 (eg. a statement is temporarily commented out? Without braces, this will causes problems (unless somehow you have its closing";" on its own line), because then the following statement is taken to be the branch, with an additional problem if 'else' actually follows. I haven't even got to the bit where you might make a mistake that is not picked up by a compiler. It's a just one big palaver that can solved easily by always using a compound statement which has opening and closing braces. Within such a statement, N can vary between 0, 1 and any other number, any of combination can be commented out, without needing to take any special action. It can also happen that when starting to write a branch, you don't know what N is going to be, or maybe the statements will be filled in later. Here it is useful to have {} as a placeholder for those statements. > People just don't > make this mistake. And there is that too. Maybe YOU don't make it, in the same way that you probably know all the binary operator precedences by heart and never get them wrong. Well, not everyone is perfect.