[Tutor] Are you allowed to shoot camels? [kinda OT]

Smith, Jeff jsmith at medplus.com
Fri Feb 4 15:11:35 CET 2005


The problem with if/elif is that it doesn't express what is actually
gong on.

What you are try to do is "execute a block of code based on the value of
a single statement."  if/elif doesn't do that and thereby introduces the
possibility of errors.

switch on-this:
	case 'a':
		do something
	case 'b':
		do something else
	case 'c':
		do yet something else

Is much clearer and more maintatinable than

if on-this == 'a':
	do something
elif on-this == 'b':
	do something else
elif on-this == 'c':
	do yet something else

Note that the logic intended is that "on-this."  So why force the
programmer to rewrite it N times and thereby introduce the possibility
of N-1 typographical errors...particularly if the "on-this" is
sufficiently complex.

Note that I've left out break.  I'm not convinced that fall-through is
an important feature in switch and is usually the culpit in the cases of
abuse.  Of course, abuse has nothing to do with it.  Someone somewhere
will abuse any syntax you give them.  Just because it *can* be abused
doesn't mean it doesn't have value when used properly.

This is also true for the ternary operator.  The desired logic is to
assign the value of a variable based on the value of some other
variable.  The assignment is the primary action and therefore should be
the primary feature in the statement.  Using if/else makes the decision
point the primary action and leads to people stuffing other things in
the clauses which don't belong there.

IMHO, if/elif/else statements are far more abused than either switch or
ternary but I certainly wouldn't argue they should be removed from the
language.

I also like Perl's unless statement but really prefer VBs
DO/WHILE/UNTIL/LOOP constuct.  Nothing beats it for clarity of
expression.

Jeff



-----Original Message-----
From: Alan Gauld [mailto:alan.gauld at freenet.co.uk] 
Sent: Thursday, February 03, 2005 6:29 PM
To: Smith, Jeff; Jacob S.; Nicholas.Montpetit at deluxe.com;
tutor at python.org
Subject: Re: [Tutor] Are you allowed to shoot camels? [kinda OT]


> Perl and Python both resist the introduction of a switch statement
> which I (and many others) feel is the most elegant way to express 
> what it does.

Interesting. What do you feel is the problem with elif?
Its not even much more typing and allows for much more 
expressive test conditions. Switch is really only useful 
for a single subset of tests. And of course switch statements 
are notorious bug factories and maintenance nightmares - 
the reason why OOP tries to eliminate them wherever possible.

> I also wish Python would take up the C ternary operator
> which is also quite clear and elegant.

:-)
You joke I assume? ':?' is clear? Its succinct but also 
prone to abuse. I don't think the Python equivalent 

foo = x and y or z


is much less elegant than

foo = x ? y : z

> ... To paraphrase the famous quote:  There are no good programming
languages, just some that aren't as bad in some situations.

Absolutely true.
I still use assembler occasionally, and I even like bits of COBOL
(although not much, its my least favourite language!). And probably 
my favourite language of all is Logo even though I've never used 
it for any serious projects.

Alan G.


More information about the Tutor mailing list