Switch vs else if Efficiency

Just wondering what is better to use in compiling a groovy driver for Hubitat. Also, does it make any difference the order of which you write things. Say if I am using an if statement should I put my most likely true items up at the top. I would think if I am using breaks this could help a bit.

Any thoughts!


Switch is technically more expensive in Groovy due to the underlying method calls to isCase (which does a conversion operation to boolean after checking for nulls and equality). If statements use direct compare* methods which do not use a conversion for return values. However, unless you are using a LOT of cases, you shouldn't see any performance bottlenecks.


Personally, I code in the style of exit statements go at the top of the if stack. So, if I know that if I have a null value possibly coming in, I'll handle that first. If nothing else, it makes it more readable.