A curious case

dunbarx at aol.com dunbarx at aol.com
Thu Mar 3 16:08:24 EST 2011


Two tests:



on mouseUp
   put "coral" into testValue
   put ""
   wait 15
 switch  --this catches "coral"
   case testValue is among the words of  "pink coral azure maize"
        put "found coral" 
        break
   default
        put "no coral" 
 end switch
end mouseup


on mouseUp
   put "coral" into testValue
   put ""
   wait 15
 switch testValue  -- this does not
   case testValue is among the words of "pink coral azure maize"
        put "found coral"
        break
   default
        put "no coral"
 end switch
end mouseup



The case keyword can evaluate correctly, but only if it has (sort of) complete control, that is, the switch structure does not dictate what is to be evaluated.


If that makes any sense.


For my part, I find switch structures much more powerful, compact and readable than complex if/then structures. Oh, and more flexible, modifiable and fun to write.


Craig Newman


> 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
>







-----Original Message-----
From: Nonsanity <form at nonsanity.com>
To: How to use LiveCode <use-livecode at lists.runrev.com>
Sent: Thu, Mar 3, 2011 1:40 pm
Subject: Re: A curious case


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
_______________________________________________
use-livecode mailing list
use-livecode at lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

 



More information about the use-livecode mailing list