JdBP>> if (FILE * f = fopen("file.txt","r")) {
JdBP>> // ...
JdBP>> fclose(f) ;
JdBP>> }
DM> Well, I would *hope* that whoever was bright enough to note that this
DM> was C++-only code, would also note that the implicit check against
DM> NULL makes this marginally confusing, and one would probably be
DM> better off moving the declaration (and initialization) of f to before
DM> the if anyway.
I disagree. Although I've seen a few "style guides" recommending to always
make the test of a pointer against NULL explicit, I think that this is a
mistake, founded in the belief that without the "== NULL" at the end, the
condition of an if statement means something else. But it doesn't mean
something else, and "== NULL" is really superfluous. So I don't think that
it's a valid criticism, since the "confusion" doesn't exist once one has
passed the stage of C++ novice, and one knows the rules for converting
pointers to bools.
DM> FILE* f = fopen("file.txt","r");
DM> if (NULL != f)
DM> {
DM> // ...
DM> fclose(f);
DM> }
DM>
DM> Now this is much more readable (IMNSHO), and, as a bonus, legal
DM> C under the proposed C9X. :-)
Which completely misses the point that the syntax in the original example has
a much smaller scope for `f' and is a very useful idiom because of this.
¯ JdeBP ®
--- FleetStreet 1.19 NR
---------------
* Origin: JdeBP's point, using Squish (2:440/4.3)
|