Re: Recommended style

From: Michael Wojcik (mwojcik_at_newsguy.com)
Date: 01/07/05


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


Relevant Pages

  • Re: Michel Houellebecq amplifies Aristotle
    ... Possibly one reason I never heard ... I do not refer to the close relatives of any boor that I ... I do not share their sort of mentality, ... false education imposed by mendacious European scholarship. ...
    (rec.arts.books)
  • Re: Deconversion
    ... The obvious sort of thing (albeit rather unimaginative, ... specific claim about the nature) of God should look like. ... morality etc require some variety of theism for their justification. ... argument for limiting the capacity of human reason, ...
    (uk.religion.christian)
  • Re: Chrissy was [snip]
    ... that sort of thing. ... So, based on that, I'd say that there's good reason to think that I'm ... it had something to do with gravitation, and from them to figure out ... who just yell at me and insult me when I say things that upset them. ...
    (uk.people.support.depression)
  • Re: Why should atheism be the default position?
    ... Now where are your arguments that the existence of any sort of "God" is ... I don't believe there are any arguments from atheists. ... The only "argument" needed is that when there is no reason to believe ...
    (uk.philosophy.atheism)
  • Re: "Victory for Christian registrar bullied for refusing to perform sinful g
    ... people (perhaps the majority, ... reason for being especially careful. ... requiring any sort of accommodation. ... Because I think the sense that democracy is 'right' actually has more to do ...
    (uk.religion.christian)