Switch or If then else

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Switch or If then else

Post by Simon Knight » Wed Aug 26, 2020 9:01 am

Hi,

I wish to examine the value in a variable "tStatus" which may hold one of three strings. I have three ways of doing this; two use the if statement and one the the Switch construct. For some reason I resist using Switch statements as I think I was told years ago that they were not efficient. So which of these examples would you use ?

Code: Select all

   If tStatus is "Invalid" then
      put false into tResultsA ["ValidFileType"]
      Return tResultsA
   end if
   
   If tStatus is "LittleEndian" then put true into tIsLittleEndian
   If tStatus is "BigEndian" then put false into tIsLittleEndian
   
   
   ## OR ##
   
   If tStatus is "Invalid" then
      put false into tResultsA ["ValidFileType"]
      Return tResultsA
   end if
   
   If tStatus is "LittleEndian" then 
      put true into tIsLittleEndian
   else
      put false into tIsLittleEndian
   end if
   
   ## OR ##
   
   Switch tStatus
      Case "Invalid"
         put false into tResultsA ["ValidFileType"]
         Return tResultsA
         break
      Case "LittleEndian"
         put true into tIsLittleEndian
         break
      Case "BigEndian"
         put false into tIsLittleEndian
         break
   end Switch
best wishes
Skids

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: Switch or If then else

Post by LCMark » Wed Aug 26, 2020 9:15 am

Switch (over value) is as efficient, and perhaps slightly more efficient than a sequence of if/then/else ifs (although the difference is probably negligible until you to a very large number of cases - switch only evaluates the expression it is looking for once - i.e. eval of tStatus variable in your example).

Really this comes down to readability I think - your third switch-based option is a lot more immediately readable for me, but then it comes down to what you are used to.

The only thing to be aware of with switch is making sure you don't accidentally fall-through by missing out something which exits the switch in each case.

Our (internal) coding conventions mandate that each case of a switch which contains statements must terminate in an exit, return, next repeat or break. If fallthrough is intended then an explicit /* FALLTHROUGH */ comment should be there instead. (The latter then makes it easy on review to see whether the fallthrough was intended or accidental).

As with most things, it comes down to personal taste :)

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Switch or If then else

Post by Simon Knight » Wed Aug 26, 2020 9:30 am

Hi Mark,

Its good to know that there is no penalty in using the Switch statement as I agree in thats its my preferred syntax for clarity of code. Thanks for your input.
S
best wishes
Skids

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Switch or If then else

Post by mwieder » Thu Sep 03, 2020 2:02 am

...and begs the question as to why there's no switch statement construct in LCB.

Post Reply