PEP 285: Adding a bool type

Joshua Macy l0819m0v0smfm001 at sneakemail.com
Sat Apr 6 15:05:45 EST 2002


Laura Creighton wrote:

> My pleasure, especially since this is such a cool example.  You have
> just shot yourself in the foot, and do not know it.  When you have
> shipped your isValid function, across the world to your 10,000 clients,
> you will discover this.
> 
> Your boss comes in and says, oh, Mark, we need a change to that
> isValid function.  We need to return 3 states, Valid, Invalid, and
> Valid-but-You-Don't-Have-Access-Because-You-Didn't-Pay-Your-Bill.
> 
> What do you do now?  

   Write another function, accountPaid perhaps, that clearly expresses 
the intent.  To me something like:

    valid = isValid(user, password)
    if not valid:
       warn("Invalid password")
    elif valid == 2:
       warn("Account not paid up")
    else:
       grantAccess()

is clearly worse (uses magic values, is less clear and harder to extend) 
than something like:

   if not isValid(user, password):
      warn("Invalid Password")
   else:
      if not accountPaid(user):
         warn("Account not paid up")
      else:
         grantAccess()

I think coding a bunch of magic integer return values is far more likely 
to shoot yourself in the foot than a bunch of properly-named boolean 
functions.  To me that kind of enum-think is a legacy of non-object 
oriented code, with vast switch statements to handle program state 
encoded into integers.


   Joshua




More information about the Python-list mailing list