home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!think.com!snorkelwacker.mit.edu!enterpoop.mit.edu!bloom-picayune.mit.edu!news
- From: scs@adam.mit.edu (Steve Summit)
- Subject: Achilles and the Tortoise (was: The Correct Way To Write C if-Statements)
- Message-ID: <1992Nov23.163316.1551@athena.mit.edu>
- Sender: news@athena.mit.edu (News system)
- Nntp-Posting-Host: adam.mit.edu
- Organization: none, at the moment
- References: <1992Nov12.212728.8864@dg-rtp.dg.com> <hansg.722248402@risken>
- Date: Mon, 23 Nov 1992 16:33:16 GMT
- Lines: 40
-
- In article <hansg.722248402@risken>, hansg@risken.vd.volvo.se (Hans Granqvist) writes:
- > goudreau@batman.rtp.dg.com (Bob Goudreau) writes:
- >> The thing which I find objectionable is the silly testing against
- >> TRUE or FALSE. Certainly the practice of using
- >> if (condition)
- >> instead of
- >> if (condition == 1) /* or == TRUE */
- >> is well established in C; look at K&R1. So why add needless
- >> verbiage?
- >
- > ...I must object strongly to your
- > saying that there's only need to test 'if' or 'if not'. Some FAQ I think
- > once said (it still does, I presume) that there is a LOGICAL difference
- > to 0, '\0', and NULL. So why not test against the correct values?
-
- The comp.lang.c FAQ list indeed discusses differences between 0
- and NULL ('\0' is a red herring in that context), and suggests
- that using NULL where appropriate may show good stylistic intent.
- Testing a single (conceptually boolean) variable explicitly
- against TRUE or FALSE is relatively harmless (depending on how
- the variable was set). Of the more general case "if(<expr> == TRUE)",
- however, the FAQ list has this to say:
-
- ...the test
-
- if((a == b) == TRUE)
-
- will work as expected (as long as TRUE is 1), but it is
- obviously silly. In general, explicit tests against TRUE and
- FALSE are undesirable, because some library functions (notably
- isupper, isalpha, etc.) return, on success, a nonzero value
- which is _not_ necessarily 1. (Besides, if you believe that
- "if((a == b) == TRUE)" is an improvement over "if(a == b)", why
- stop there? Why not use "if(((a == b) == TRUE) == TRUE)"?) A
- good rule of thumb is to use TRUE and FALSE (or the like) only
- for assignment to a Boolean variable, or as the return value
- from a Boolean function, never in a comparison.
-
- Steve Summit
- scs@adam.mit.edu
-