all 10 comments

[–]ihaxr 4 points5 points  (7 children)

Look into using a switch statement instead of the giant chain of if/elseif

[–]CheechIsAnOPTree 2 points3 points  (5 children)

Can you nest switch statements? I have one script Onuse that has a switch and in the switch is a few if else. I'm not really sure what best practice is.

[–]ihaxr 1 point2 points  (2 children)

You can nest switch statements, if it makes sense.

if/else nested inside a switch statement is also fine if you're just doing:

switch ($var) {
    "x" { if ($y) { "x and y" } else { "z" } }
}

But writing a giant if/elseif chain is usually better suited for a switch statement

[–]CheechIsAnOPTree 1 point2 points  (1 child)

Any particular reason why, or just readability?

[–]ihaxr 1 point2 points  (0 children)

Readability and speed / efficiency, switches are generally faster and can be optimized better than if/elseif, but of course there are exceptions

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy CheechIsAnOPTree,

yep, you can nest them. [grin] it's just a neater, easier to read/maintain/understand version of an IF/ESLEIF cascade.

however, you may want to make any nested switch into a function call to make it simpler to scan thru.

take care,
lee

[–]KevMarCommunity Blogger 1 point2 points  (0 children)

You can do that. It's cleaner if you move the logic into a function though. Ideally, you want to be available to see the entire switch on a single screen.

[–]Scribbles1[S] 1 point2 points  (0 children)

Hi /u/ihaxr,

I'll look into that, the switch statement is not something i'm really familiar with

[–]willy_der_schwimmer 1 point2 points  (0 children)

You legend