| Deutsch English Français Italiano |
|
<vpq3id$35inm$3@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Which code style do you prefer the most?
Date: Thu, 27 Feb 2025 17:23:41 +0100
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <vpq3id$35inm$3@dont-email.me>
References: <vpkmq0$21php$1@dont-email.me> <vpl2k4$24fmt$1@dont-email.me>
<20250225104754.267@kylheku.com> <zWovP.1403558$21T3.838698@fx18.iad>
<vplf5d$26v09$1@dont-email.me> <874j0g2a8u.fsf@onesoftnet.eu.org>
<vpn3l8$2ipsc$1@dont-email.me> <877c5cpf3s.fsf@nosuchdomain.example.com>
<vpoe1e$2q6ak$6@dont-email.me> <87wmdbilm0.fsf@onesoftnet.eu.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 27 Feb 2025 17:23:41 +0100 (CET)
Injection-Info: dont-email.me; posting-host="06a8019a08c63385063fcd606aa3c794";
logging-data="3328758"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZSnDmuIfUMcEZPLNoOSqlT9SeCctIeGs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:rxmsHDKszDL8/cbNvOfxQpwcBj4=
Content-Language: en-GB
In-Reply-To: <87wmdbilm0.fsf@onesoftnet.eu.org>
Bytes: 3067
On 27/02/2025 14:05, Ar Rakin wrote:
> Lawrence D'Oliveiro <ldo@nz.invalid> writes:
>
>> On Wed, 26 Feb 2025 13:31:51 -0800, Keith Thompson wrote:
>>
>>> One interesting feature of Perl is that the braces are required.
>>
>> And yet it doesn’t have C-style macros, which to me are the most obvious
>> way one can get into trouble by omitting needed braces.
>
> What do you mean? Sorry I'm stupid sometimes.
He is thinking of situations like this (ignore the stupidity of the
example) :
#define message(x) printf("Message: "); printf(x); printf("\n")
void foo(int y) {
message("Starting foo... ");
if (y < 0)
message("Y is negative");
message("Done foo.")
}
Reading "foo", you'd think that it would print "Message: Y is
negative\n" if y is negative. But in fact only the first statement in
the macro is covered by the conditional, and "Y is negative\n" will
always be printed. You can easily imagine how things could get a whole
lot worse from such mixups.
There are many ways to avoid this, including putting a "do { ... } while
(0)" wrapper around the statements in the macro, using an inline
function instead of a macro, writing macro names in all-caps as a
warning (I strongly dislike that convention), and always using braces
with conditionals even if they appear to cover just one statement.
I am not sure if there is a basis for judging the "most obvious" problem
that can occur from omitting braces in conditionals, but this is
certainly a strong contender for the title.
Another case that occurs is confusion about matching up else's with if's
in nested conditionals without braces.