switch

Albert van der Horst albert at spenarnc.xs4all.nl
Mon Dec 21 09:53:38 EST 2009


In article <pan.2009.12.10.05.47.18 at REMOVE.THIS.cybersource.com.au>,
Steven D'Aprano  <steven at REMOVE.THIS.cybersource.com.au> wrote:
>On Wed, 09 Dec 2009 18:50:29 +0000, Nobody wrote:
>>
>> Compiled languages' switch statements typically require constant labels
>> as this enables various optimisations.
>
>Pascal, for example, can test against either single values, enumerated
>values, or a range of values:
>
>case n of
>   0:
>     writeln('zero');
>   1, 2:
>     writeln('one or two');
>   3...10:
>     writeln('something between three and ten');
>   else writeln('something different');
> end;

This shows where the merit of a switch/case statement lies.
It guarantees both to the compiler and to the human reader
that we are dealing with mutually exclusive and constant cases.
This allows optimisation (c) and verification (h) advantages.

A chain of elif's OTOH doesn't do that. On the top of my toes
I will have to check every line whether the regularity
breaks down.

As regards polymorphism. It is dubious that an inherently
different technique can be consistently and confidently
promoted as an alternative. Sometimes it is, sometimes it
isn't. Polymorphism is IMO promoted for far too much
situations. As a maintenance programmer (being at least
twice as smart as the people who wrote the original code)
I can testify that code can be polymorphed to the point
that it stops working and/or that it practically stops
working and/or that it stops working practically.

I'm the author of PostItFixup assembler. This assembler
manages to assemble code with no control structures
at all. 1]
No IF, no FOR, no WHILE, no CASES. Nothing conditional,
and no looping. How's that for a change?
Although this is truly remarkable and well worth studying,
I will not promote this style of programming as the ultimate
alternative for everything. (And it breaks down at a point,
sure.)

Bottom line: "Give to the Caesar what belongs to the Caesar..."

Not adding a switch to python is a matter of taste,
good taste as far as I'm concerned.

Adding a switch to C was also good taste, by the way.

>--
>Steven

Groetjes Albert

--

1] Only in the  error checking there is an IF.
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst




More information about the Python-list mailing list