TRUE and FALSE

Flint Pellett flint at gistdev.gist.com
Wed Aug 29 23:59:05 AEST 1990


volpe at underdog.crd.ge.com (Christopher R Volpe) writes:

>In article <2316 at cirrusl.UUCP>, dhesi%cirrusl at oliveb.ATC.olivetti.com
>(Rahul Dhesi) writes:
>|>
>|>When I find somebody who really, really, really wants to define TRUE
>|>and FALSE, even somebody who uses them for assignment only, I recommend
>|>the following defines instead:
>|>
>|>     #define ZERO   0
>|>     #define ONE    1
>|>

>Ugh. Those say nothing. They don't hint to the boolean nature of the
>variables being assigned to.

I agree.  They may also get you into trouble should you ever wish to change
the values of your booleans: Off-hand, I don't know why you would want to,
but it is possible that at some point in the future you might have to port
to CBD C (Charlie's Brain-Dead C) where TRUE is -1, and you'd be rather
embarassed to have this line in your code:

#define	ONE	-1

>From a true-to-life example of why names like the above are bad:
an old program for a SYSV file system, which knew it needed 14 chars for
file names at each level, and was putting 2 character extensions (like
".c" elsewhere), did this:

#define  TWO        2
#define  TWELVE    12
char base_filename[TWELVE];
char filename_ext[TWO];

Then, sure enough, someone wanted it to handle file names it hadn't been
set up to handle, (which it should have handled in the first place, but
that's another story) like ones with more than 1 character extensions,
and with null-terminated names, etc., and we had this in the code:

#define  TWO	   15
#define  TWELVE    15

A few years from now (if the program had survived- I killed it; It was
a mercy killing) if it had migrated to a BSD file system, we might have
seen this:

#define  TWELVE   256

Please: pick names for constants that say what they are for, (like
CharsPerFilenameLevel) not what they are, because what they are for is
not going to change, the values might.  You'll also avoid ending up with
someone using the same constant for two different meanings because the
name isn't good enough to prevent it.  (If a name like ONE got used
throughout a program to represent both boolean TRUE and buffers that
needed to be 1 char long, and you wanted to lengthen the buffers, you'd
have a nice mess to sort out.)
-- 
Flint Pellett, Global Information Systems Technology, Inc.
1800 Woodfield Drive, Savoy, IL  61874     (217) 352-1165
uunet!gistdev!flint or flint at gistdev.gist.com



More information about the Comp.lang.c mailing list