A curious case

Nonsanity form at nonsanity.com
Thu Mar 3 13:40:41 EST 2011


On Wed, Mar 2, 2011 at 8:03 PM, Jim Ault <jimaultwins at yahoo.com> wrote:

> put the textcolor of fld 1 into testValue
>
> switch testValue
>   case "red"
>   case "yellow"
>   case "orange"
>        put "red" into newFontColor
>        break
>   case "green"
>   case "blue"
>   case "purple"
>        put "green" into newFontColor
>        break
>   case (testValue is among the words in "pink coral azure maize")
>        put "darkBlue" into newFontColor
>        break
>   default
>        put "black" into newFontColor
> end switch
>
> set the textcolor of fld 1 to newFontColor
>


Are you sure the "is among" line works there?

The case structure equates what comes after the "switch" keyword with what
comes after each "case", executing the code after first one to match and all
further code until it hits a "break". By those rules, you would only get
"darkBlue" if testValue contained "false". Since "false" is not in the color
list, the "is among" returns "false", which then matches the switch value,
so "darkBlue" is returned. But if testValue is "pink", you're going to get
"black" as a result.

I can't test it right now, so I'm not sure, but that's how I read the code.
But if that code works as you say, then LC's switch flow control is severely
distorted from the norm!

Personally, I avoid switch-case controls like the plague... UNLESS my needs
exactly match the control's two main strengths - one and only one value to
test, and fall-through concatenation of code blocks - AND the use is a
simple, one-screenfull affair.

Switches reek of the evil stench of gotos and taste of spaghetti. They
aren't as readable as the rest of LC's code, which tends towards spoken
language on the whole.

IF you can speak a switch statement in a sentence, THEN it might be the
better solution, ELSE you're probably better off just sticking with if-else
chains.

...Which are more flexible, anyway. It sucks to build a whole switch
structure, only to find out that you need to also test a second variable for
some cases, and have to convert the whole thing to if-elses after the fact.

[This option may not be equal to the majority's view, but it's mine.]

 ~ Chris Innanen
 ~ Nonsanity



More information about the use-livecode mailing list