home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!wirzeniu
- From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
- Newsgroups: comp.lang.c
- Subject: Re: Hows this for odd
- Message-ID: <1992Nov10.223737.13813@klaava.Helsinki.FI>
- Date: 10 Nov 92 22:37:37 GMT
- References: <1992Nov9.102217.23060@monu6.cc.monash.edu.au> <1992Nov9.231340.10201@ptcburp.ptcbu.oz.au>
- Organization: University of Helsinki
- Lines: 34
-
- michi@ptcburp.ptcbu.oz.au (Michael Henning) writes:
- > if (strncmp(p, "", 1) != 0) {
- [...]
- >There are lots of gems like that around :-(
-
- I'm sure there are, but I don't think this is one of them. It would
- IMHO be much worse to leave the explicit comparison off. This is
- because strcmp and strncmp (and other functions with a similar
- interface) do not in fact return a value that is conceptually boolean,
- therefore doing a boolean operation on it is bad. Likewise, since an
- equality test does not yield a conceptually boolean value, you should
- avoid constructs like:
-
- if ((i==0) + (j==0)) printf("i or j is nonzero\n");
-
- If one is not concentrating (like most people reading code do not seem
- to be) the statment
-
- if (strcmp(foo, bar)) foobar();
-
- looks like it is going to call foobar if foo and bar compare equal,
- when in fact the opposite is true. By making the test explicit,
-
- if (strcmp(foo, bar) != 0) foobar();
-
- it becomes clearer that strcmp does not return a boolean value.
-
- You are free to disagree about this, of course, and many people
- probably do. It is a completely stylistic issue, and doesn't deserve
- a heated discussion.
-
- --
- Lars.Wirzenius@helsinki.fi (finger wirzeniu@klaava.helsinki.fi)
- MS-DOS, you can't live with it, you can live without it.
-