Switch versus if/then/else ( was: Main menu puzzle, Klaus)

Graham Samuel livfoss at blueyonder.co.uk
Mon Feb 20 06:17:26 EST 2006


On Sun, 19 Feb 2006 14:27:07 -0800, Geoff Canyon  
<gcanyon at inspiredlogic.com> wrote:
>
> On Feb 19, 2006, at 11:41 AM, Graham Samuel wrote:
>
>
>> Just to add a tiny footnote to this: Judy, how would you tackle a
>> situation where a user (or a handler) can put in say a number from
>> 1 to 50, and each of those numbers requires the program to carry
>> out a different action? It wouldn't really be feasible to have a
>> depth of 50 if-then-elses, so if you want to stick to a simple
>> sequential model, either you'd have to use a switch (case)
>> statement, or alternatively a structure like a handler 'myHandler'
>> containing 50 statements like:
>>
>> if my parameter = 33 then
>>  doMyThirtyThirdThing
>>  exit myHandler
>> end if
>>
>
> Just to beat on the horse some more, given the above problem
> statement, I would likely store the names of the fifty handlers in a
> custom property, and do the following:
>
> do (line myParameter of the uHandlerList of this cd)

In the first draft of my mail quoted above, I was going to mention  
this and the general idea of lookup lists and/or some kind of array  
approach (finite-state automata, yet!) but I realised that this was  
so far from the original spirit of the conversation that I deleted  
it. In fact as Judy says:

> And, again, in the spirit of the original postings of the thread,  
> I'd say
> this:
>
> If-Thens are readily comprehensible by non/novice-programmers.
>
> Case-Switch statements are preferred by "real" programmers.
>
> Real Programmers can take a look at any given set of if-thens and
> internally posit the corresponding, perhaps superior, case-switch
> statement(s).
>
> But it doesn't work the other way around.

I see the justice of what Judy is saying - I must rate as a Real  
Programmer by now, so I can't quite get the feel of the puzzlement  
felt by non/novice-programmers, but I take it on trust: and I should  
remind myself that my head often spins when I try to understand some  
concept which is totally normal to people who have followed only  
slightly different paths of geekiness than mine - for example the  
idea of an Object Request Broker used to be something I was forced to  
talk about but which I never understood. My mind kind of floated  
around the subject with nowhere to make anchor. Maybe case statements  
are like that for novices.

Good luck to all those who teach in this field.

Graham


------------------------------------------------------------------------ 
---
Graham Samuel / The Living Fossil Co. / UK and France




More information about the Use-livecode mailing list