[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Booleans as integers



Andy,

> setpc-var not pc = yellow
>
> if your color is yellow, set pc-var to false (1), else set it to true
> (0). (We do the opposite, because we want to count them up, and false is
> 1, not 0.

This kind of code is a very brief way of putting it. But doesn't it go
against the reference manual's warning which discourages the use of
boolean values as integers since they may be differently implemented in the
future?
A somewhat longer, but implementation-independent solution (and perhaps
more readable) would be:

ifelse pc = yellow [setpc-var 1] [setpc-var 0]

Speaking about the issue of using boolean values as integers in general, I
think it is also dangerous not only because the implementation of StarLogo
may change. You may very easily make a mistake because the code is not
readable. On the other hand, it is always easy to find a substitute like
above.

My experience has been that even if you do not use booleans as integers
explicitly, the comiler may do so in cases you wouldn't expect.
One important case is the Logo rule saying that maths operations have
higher precedence over procedure calls, so in

if random 10 < 3 [die]

what you have is 10 < 3 evaluated first to be false, which is 1, and then
generate a number according to random 1, which is necessarily 0, meaning
true. (The correct code is if (random 10) < 3 [die].)

This kind of mistake is quite frequent, at least with me it has been,
and also with the children that I've worked with.

As a remedy, I would suggest that StarLogo produces a warning if a boolean
is used as an argument to other procedures or maths operations except for
the logical ones (and, or, not, xor). Also, a warning could be produced
when an integer is used as a boolean. This would be very similar to what
we have in C compilers where one very often puts only one = in a
condition; that is likely to be a mistake, and the compiler produces a
warning for it, but you can use it if you want to.
I understand that StarLogo does not use types as strictly as for example
Pascal, but I guess that such warnings could still be generate at compile
time.


Cheers,
Gyorgy