Re: how to make a macro work as a single line if stmt without braces

Liste des GroupesRevenir à l c 
Sujet : Re: how to make a macro work as a single line if stmt without braces
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 21. Sep 2024, 10:47:10
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcm16e$1hm2u$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On 21/09/2024 09:35, Mark Summerfield wrote:
I have this macro:
 #define WARN(...)                                       \
     do {                                                \
         fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
         fprintf(stderr, __VA_ARGS__);                   \
     } while (0);
 which I use like this:
 total++;
if (failed) {
     WARN("failed because...");
} else
     ok++;
 I would prefer to be able to write this instead:
 total++;
if (failed)
     WARN("failed because...");
else
     ok++;
 but doing so results in a compiler error:
 error: 'else' without a previous 'if'
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++;
}
The rule here is that you the /only/ time you allow yourself to omit the braces is if you have a very simple "if" that has no "else", 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.
This also keeps your indentation consistent - open braces are only at the end of a line, and the next line starts an indented block.  Close braces are only ever the first non-indent character in a line, and you un-indent on the close brace.
(It's common to put the opening brace of a function at the start of its own line, and braces for struct or array initialisation can be freer.)
do, while and for loops always use braces.
There are a few other variant styles that are also consistent and clear, but usually they end up unnecessarily spread out.  (Opinions will vary about that.)  But getting good habits here from early one will make your code much clearer, greatly reduce the risk of blocking errors, and be a significant boon for code maintenance and the use of version control systems.

Date Sujet#  Auteur
21 Sep09:35 * how to make a macro work as a single line if stmt without braces6Mark Summerfield
21 Sep10:10 +- Re: how to make a macro work as a single line if stmt without braces1Lawrence D'Oliveiro
21 Sep10:47 +- Re: how to make a macro work as a single line if stmt without braces1David Brown
21 Sep11:13 +* Re: how to make a macro work as a single line if stmt without braces2Bart
21 Sep11:53 i`- Re: how to make a macro work as a single line if stmt without braces1Mark Summerfield
21 Sep11:15 `- Re: how to make a macro work as a single line if stmt without braces1Ike Naar

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal