On Wed, Jan 21, 2004 at 06:43:26PM +0200, Gaal Yahas wrote:
> On Wed, Jan 21, 2004 at 11:09:29AM -0500, Andrew Pimlott wrote:
> > use constant FALSE => !TRUE;
> > use constant TRUE => !FALSE;
>
> I start off all my programs with use strict :-)
Wimp. :-)
> But more to the point, how do you use your TRUE and FALSE? Suppose you
> want to test the return value of this sub:
>
> sub returns_a_true_value { 8.2 }
>
> Would you do "if (returns_a_true_value() eq TRUE)"? "== TRUE"?
Aieee!
> Neither would work, and you can just do "if (returns_a_true_value())"
> directly anyway.
Of course, even if it weren't broken, "eq TRUE" is logically
redundant and indicates the programmer was asleep, drunk, or
clueless. (Not that I haven't publically posted code with this
shameful wart. I couldn't look another programmer in the eyes for
weeks!)
The constants do improve code where it otherwise wouldn't be clear
to the reader whether 1 was an integer or a boolean. But such code
can sometimes be improved even more by defining a specific constant
for each use. Say a function takes a couple flags, one indicating
whether it should output verbose message, another whether it should
stop on the first error. If you write
foo($bar, $baz, 0, 1);
you might forget what they mean. If you write
foo($bar, $baz, !VERBOSE, STOP_ON_ERROR);
it's obvious.
Andrew