From: Joel Brobecker <brobecker@adacore.com>
To: Sergio Durigan Junior <sergiodj@redhat.com>
Cc: Eli Zaretskii <eliz@gnu.org>,
Pierre Muller <pierre.muller@ics-cnrs.unistra.fr>,
gdb-patches@sourceware.org
Subject: Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
Date: Mon, 24 Dec 2012 04:11:00 -0000 [thread overview]
Message-ID: <20121224041116.GP5370@adacore.com> (raw)
In-Reply-To: <m3fw2wo12z.fsf@redhat.com>
> > Since when is that bad C, so much so that we would need to enforce it?
>
> Interesting... I remember when I started hacking GDB, I was strongly
> discouraged to do assignments inside `if' checkings. I don't remember
> who told me that, but the reason was something related to the Coding
> Standards (I don't have any references either). Anyway, since that
> moment I stopped doing this...
It is in the GNU Coding Standards:
http://www.gnu.org/prep/standards/standards.html#Syntactic-Conventions
In particular:
| Try to avoid assignments inside if-conditions (assignments inside
| while-conditions are ok). For example, donâÂÂt write this:
|
| if ((foo = (char *) malloc (sizeof *foo)) == 0)
| fatal ("virtual memory exhausted");
| instead, write this:
|
| foo = (char *) malloc (sizeof *foo);
| if (foo == 0)
| fatal ("virtual memory exhausted");
FWIW, I mostly agree with this suggestion, particulary in the
examples above. I do remember seeing some examples where it was
more practical if we could have assignments inside conditions
(in an "if ... else if ..." situation), but even then, I find
that it makes it harder to notice the assignment.
In the examples that Pierre chose to fix, it seems obvious to me
(this is a judgement call, so potentially subject to disagreement)
that there was no need to have the assignment inside the condition,
and that the patched code is easier to grasp. I think it is
a clear improvement.
> FWIW, I agree that it is not bad C, and totally valid
I think that the point is not that this is bad C, or not
valid/portable. I think that the point is that it is judged
to be a poor practice.
--
Joel
next prev parent reply other threads:[~2012-12-24 4:11 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <001201cde13f$af3ad4b0$0db07e10$%muller@ics-cnrs.unistra.fr>
2012-12-23 19:29 ` Eli Zaretskii
2012-12-23 21:58 ` Pierre Muller
2012-12-23 22:28 ` Andreas Schwab
2012-12-24 3:41 ` Eli Zaretskii
2012-12-24 10:17 ` Andreas Schwab
2013-01-10 12:28 ` Pierre Muller
2013-01-11 4:29 ` Joel Brobecker
2012-12-23 22:28 ` Sergio Durigan Junior
2012-12-24 4:11 ` Joel Brobecker [this message]
2012-12-23 19:00 Pierre Muller
2012-12-24 4:17 ` Joel Brobecker
2012-12-24 10:22 ` Andreas Schwab
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20121224041116.GP5370@adacore.com \
--to=brobecker@adacore.com \
--cc=eliz@gnu.org \
--cc=gdb-patches@sourceware.org \
--cc=pierre.muller@ics-cnrs.unistra.fr \
--cc=sergiodj@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox