Re: Recommended style
From: Michael Wojcik (mwojcik_at_newsguy.com)
Date: 01/07/05
- Next message: William R.: "Re: Algorithms IN C"
- Previous message: Andrey Tarasevich: "Re: const function() !!??"
- In reply to: Eric Sosman: "Re: Recommended style"
- Next in thread: Chris Croughton: "Re: Recommended style"
- Reply: Chris Croughton: "Re: Recommended style"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 7 Jan 2005 18:34:35 GMT
In article <crhk69$pet$1@news1brm.Central.Sun.COM>, Eric Sosman <eric.sosman@sun.com> writes:
> >
> >>Victor Nazarov wrote:
> >>
> >>> if (!fp)
> >>> fclose (fp);
>
> This sort of mistake is, I think, a good reason
> to prefer `x == 0' (or `0 == x' if a backward-thinking
> person you are, young Skywalker) over `!x' when `x'
> isn't "obviously boolean."
I'm not convinced that would significantly reduce this sort of error.
I strongly doubt it would for me (though I can't recall having made
this particular mistake; doesn't mean I haven't, but it hasn't happened
often or recently enough for me to remember). I don't find the
comparison with an explicit zero any more readable than the logical-
negation one, or see any other reason why it might discourage error.
But, of course, YMMV.
> I particularly dislike
>
> if (!strcmp(password, "drowssap"))
>
> ... which tests for equality but appears on a hasty
> reading to do the opposite.
With this, on the other hand, I agree. Inverted strcmp sense errors
are one of the more common sort I find in others' code. I don't
remember making one myself since I adopted Peter van der Linden's
macro, which I think does improve readability significantly. For
example:
#define MySTRCMP(s1, op, s2) (strcmp(s1, s2) op 0)
if (MySTRCMP(password, ==, "drowssap"))
Inlining the operator brings string comparisons syntactically closer
to their numeric cousins.
-- Michael Wojcik michael.wojcik@microfocus.com An intense imaginative activity accompanied by a psychological and moral passivity is bound eventually to result in a curbing of the growth to maturity and in consequent artistic repetitiveness and stultification. -- D. S. Savage
- Next message: William R.: "Re: Algorithms IN C"
- Previous message: Andrey Tarasevich: "Re: const function() !!??"
- In reply to: Eric Sosman: "Re: Recommended style"
- Next in thread: Chris Croughton: "Re: Recommended style"
- Reply: Chris Croughton: "Re: Recommended style"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|