PEP 285: Adding a bool type

Paul Sidorsky paulsid at shaw.ca
Sat Apr 6 14:46:45 EST 2002


Laura Creighton wrote:

> I think there is a time and a place for bools --  Describing things
> that have two states that can never, ever, have 3 or more states.
> These things are vanishingly close to non-existent, unless you are
> a mathematician in which case you may be playing with these sorts
> of things all the time.  What I am trying to do is save people
> like Mark McEahern from his isValid function, which returns whether
> a username is valid or not.  On the day his boss demands a third
> value - valid-but-I-won't-let-you-in-anyway-you-didn't-pay-your-bill,
> he will thank me.

I haven't been following this thread closely, but this caught my eye.  

I sincerely hope this isn't the approach you are using to teach
programming.  If it is, it's no wonder you have a problem teaching
bools.

An isValid() function answers the question "is this valid"?.  valid-but
is not an answer to that question.  There are only two answers:  yes and
no.  Or, if you like, True and False.  Returning valid-but in an
isValid() function would be like returning 6-if-you-add-1 as the answer
to 3+2.

The proper course of action here would be to add a second check,
isPaid(), to wherever the new information is needed.  This has the
following advantages:

1) Adding a new function call is generally far easier than modifying a
bunch of existing calls.
  
2) If any checks do not care about the Paid status, they need never be
modified.

3) The clarity of the old function is preserved.

4) The functionality of the old function is preseved, i.e. there is no
chance of introducing bugs in a working function caused by ripping its
guts apart.

You don't demolish a house when you just need to add a bedroom, so why
rebuild a perfectly good isValid() function?

> That is what I mean by teaching people not to use bools -- teaching
> when not to use them. This is very hard to teach.  I know it because
> I have been doing it for years and years.   I even know _why_ it is
> hard to teach, but that doesn't make it any easier.

Teach people instead that half-answers like valid-but do not have a
place in programming (and why they don't), and then maybe you won't even
have to teach them when not to use bools.

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid at shaw.ca                        http://members.shaw.ca/paulsid/





More information about the Python-list mailing list