Deutsch English Français Italiano |
<87frpsr7tf.fsf@nosuchdomain.example.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> 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 14:54:52 -0700 Organization: None to speak of Lines: 68 Message-ID: <87frpsr7tf.fsf@nosuchdomain.example.com> 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 Content-Transfer-Encoding: 8bit Injection-Date: Sat, 21 Sep 2024 23:54:53 +0200 (CEST) Injection-Info: dont-email.me; posting-host="d15c88e3c436232dfad8af44b8464f14"; logging-data="1844631"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+PWhV6AtkySltyAYmNH1Y0" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:H7C6mvbf5C8OnK/SKRhMvjx5NGw= sha1:2rNTp3PSYvKT0iupCt1gbV6srb4= Bytes: 3123 Andrey Tarasevich <andreytarasevich@hotmail.com> writes: > 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. >> if (failed) { >> WARN("failed because..."); >> } else { >> ok++; >> } >> > > Nope. There's absolutely no reason to overuse braces. Plenty of C programmers, myself included, disagree with this. One reason to "overuse" braces is that you can easily add another statement. If you write: if (failed) WARN("failed because..."); else ok++; and later decide you need two statements in the else clause, you then need to add braces. If they're already there, you don't. > 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 > { > ... > } Again, many, perhaps most, C programmers will disagree with this. What you call "Egyptian" braces is the style used by the creators of the language and in a *lot* of open source software. Even if you don't like the style, you'll need to deal with it. I have my own fairly strong preferences about brace placement, but the most important rule is to follow the conventions of the code I'm working on. [...] >> 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. I've rarely run into that problem. When I have, it's been easy enough to temporarily modify the code. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */