[RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
Joel Brobecker
brobecker@adacore.com
Mon Dec 24 04:11:00 GMT 2012
> > 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
More information about the Gdb-patches
mailing list