switch

Asun Friere afriere at yahoo.co.uk
Thu Dec 10 06:34:53 EST 2009


On Dec 10, 2:00 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
> On Dec 9, 5:02 pm, Asun Friere <afri... at yahoo.co.uk> wrote:
>
> > On Dec 9, 7:08 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
>
> > > What if the object is a string you just read from a file?
>
> > > How do you dispatch using polymorphism in that case?
>
> > This would be a pertinent question, were I advocating that _all_
> > switch statements should, or even can, be replaced with "dispatch
> > using polymorphism."
>
> Then why did you claim that a decent OO should never have a switch
> statement then?

The mere fact that it is possible to demonstrate a use of the 'goto'
statement, which does not offend against a program's structural
integrity, does not mean it's necessarily a good idea to have it lying
about given that in the large majority of cases it leads to
(encourages?) bad code.  Python sagely declined to implement 'goto'.

I feel the same considerations apply in regard to the switch/case
statement in the context of a true* OO (ie python but not java ;>).
Imo the decision not to implement a switch statement was wise.  This
is not to deny that a traditional switch is not in often a sane
solution.  I just think that if your elifs are getting unwieldy enough
to require the application of the proverbial layer of abstraction,
then a switch statement fails to deliver sufficient abstraction.

> You argued that a decent language OO should never
> have a switch statement because polymorphic dispatch is the right way
> to handle it in OO languages, which implies that polymorphism can and
> should take the place of any switch statement.

That is a misreading.  I wrote, that the case logic is "probably"
symptomatic of poor design. I advocated "considering" whether an OO
solution might be more appropriate.  You misunderstand my intent.  I'm
not here to postulate some ultimate OO truths, but to provide
practical advice (aimed here mainly at Hong and Kee).  In regard elif
chains, this is my advice.

If you see too many elifs in your code (or if you find yourself
wishing for a switch statement) alarm bells should start to go off.
STOP, give serious consideration as to whether applying some patterns
or similar will improve the design, and HINT:  first consider whether
that pattern might be something resembling a dispatch mechanism, it's
just downright spooky how in real-life programming situations this
turns out to be the answer.  Obviously the answers to these questions
are not invariably 'yes.'  Often enough they are.

>
> > What if, instead of reading strings from a file,
>
> Why don't you answer my question first,

Because, as I have already explained, I object to the question.  It
presupposes my holding a position I don't.

But look perhaps I'm being unfair to you Carl.  I presumed your
question was rhetorical, or at least you supposed it disproved
whatever it is you were attacking.  If instead you were asking for
clarification, the short answer is "wrap as you read."  A longer
answer is in the response to Tim, I'll send in a while.

cheers



More information about the Python-list mailing list