Mail Archives: djgpp/1996/04/17/13:33:03
From: j DOT aldrich6 AT genie DOT com
Date: Wed, 17 Apr 96 13:28:00 UTC 0000
Reply to message 4728013 from KUKU AT GILBERTO on 04/17/96 3:37AM
>I didn't want to suggest sincerely using farse,tlue as variables
>in the program (that's why I used a smiley).
Oh, I know. It gave me a good laugh, too. :) But I still wanted to make
my point.
>When we are at judging
>about style I would state that defining a pair of boolean variables
>as an enum is bad style anyway since I'm not sure where is guaranteed
>that the compiler always chooses the value 0 for the first
>entry in the enum list - maybe it's defined in ANSI somewhere.
>At least I would consider it dangerous.
I think ANSI does specify that the first enumerated value is automatically
zero. But you are right in thinking that using enums can mask the actual
significance of the values. I can imagine some novice who depends too
much on enums doing something like "if ( (exp) == true )" and getting
Indeed ANSI specifies that enum values get applied sequentially beginning with
zero (0). Of course one could just specify the values and end all abiguity:
typedef enum TRUTH enum { True=1, False=0 } Truth;
Truth IsOK;
I personally prefer this to #defines and I'm no novice (16 years programming in
8+ languages in "C" for 10 years).
creamed. :) In fact, the only real reason for using enums at all is for
preventing coding errors. A series of #defined constants usually works
just as well unless you are really concerned about debugging.
That's reason enough! But also do not discount the improved documentation
gained by using enums properly. One knows that the variable IsOK is being
used exclusively as a boolean and not accidentally as a test for non-zero
status! Obviously is one defines enums and assigns their values to variables
declared as int they are no better than #defines, but that is just BAD CODE
not limited value.
--
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com
A proverb is no proverb to you 'till life has illustrated it. -- John Keats
- Raw text -