views:

5107

answers:

22
+24  Q: 

Switch vs if-else

What's the best practice for switch vs if for a 30 unsigned enumerations where about 10 have an expected action (that presently is the same action). Performance and space need to be considered but are not critical. I've abstracted the snippet so don't hate me for the naming conventions :p

// numError is an error enumeration type, with 0 being the non-error case
// fire_special_event() is a stub method for the shared processing

switch (numError)
{  
  ERROR_01 :  // intentional fall-through
  ERROR_07 :  // intentional fall-through
  ERROR_0A :  // intentional fall-through
  ERROR_10 :  // intentional fall-through
  ERROR_15 :  // intentional fall-through
  ERROR_16 :  // intentional fall-through
  ERROR_20 :
  {
     fire_special_event();
  }
  break;

  default:
  {
    // error codes that require no additional action
  }
  break;       
}

versus an if statement

if ((ERROR_01 == numError)  ||
    (ERROR_07 == numError)  ||
    (ERROR_0A == numError)  || 
    (ERROR_10 == numError)  ||
    (ERROR_15 == numError)  ||
    (ERROR_16 == numError)  ||
    (ERROR_20 == numError))
{
  fire_special_event();
}
+15  A: 

Compiler will optimise it anyway - go for the switch as it's the most readable.

Richard Franks
+6  A: 

The Switch, if only for readability. Giant if statements are harder to maintain and harder to read in my opinion.

ERROR_01 : // intentional fall-through

or

(ERROR_01 == numError) ||

The later is more error prone and requires more typing and formatting than the first.

scubabbl
+2  A: 

I'm not sure about best-practise, but I'd use switch except trap intentional fall-through via 'default'

da5id
+2  A: 

IMO this is a perfect example of what switch fall-through was made for.

Nescio
in c# this is the only case where fall thought happens. Good argument right there.
BCS
A: 

I would pick the if statement for the sake of clarity and convention, although I'm sure that some would disagree. After all, you are wanting to do something if some condition is true! Having a switch with one action seems a little... unneccesary.

William Keller
+1  A: 

If your cases are likely to remain grouped in the future--if more than one case corresponds to one result--the switch may prove to be easier to read and maintain.

TSomKes
A: 

Please use the switch. The if statement will take time proportional to the number of conditions.

David Nehme
A: 

Im not the person to tell you about speed and memory usage, but looking at a switch statment is a hell of a lot easier to understand then a large if statement (especially 2-3 months down the line)

Ed Brown
+32  A: 

Use switch.

In the worst case the compiler will generate the same code as a if-else chain, so you don't lose anything. If in doubt put the most common cases first into the switch statement.

In the best case the optimizer may find a better way to generate the code. Common things a compiler does is to build a binary decission tree (saves compares and jumps in the average case) or simply build a jump-table (works without compares at all).

Nils Pipenbrinck
Technically there will still be one compare, to make sure the enum's value lies within the jump table.
0124816
Jep. That's true.Switching on enums and handling all cases may get rid of the last compare though.
Nils Pipenbrinck
Can someone edit this to change loose to lose?
Dave L.
Note that a series of ifs could theoretically be analyzed out to be the same as a switch by a compiler, but why take the chance? By using a switch, you are communicating exactly what you want, which does make code generation easier.
jakobengblom2
jakoben: That could be done, but only for switch-like if/else chains. In practice these don't occur because programmers use switch. I digged into compiler technology and trust me: Finding such "useless" constructs takes a lot of time. For compiler guys such an optimization does mot make sense.
Nils Pipenbrinck
+1  A: 

They work equally well. Performance is about the same given a modern compiler.

I prefer if statements over case statements because they are more readable, and more flexible -- you can add other conditions not based on numeric equality, like " || max < min ". But for the simple case you posted here, it doesn't really matter, just do what's most readable to you.

SquareCog
+1  A: 

switch is definitely preferred. It's easier to look at a switch's list of cases & know for sure what it is doing than to read the long if condition.

The duplication in the if condition is hard on the eyes. Suppose one of the == was written !=; would you notice? Or if one instance of 'numError' was written 'nmuError', which just happened to compile?

I'd generally prefer to use polymorphism instead of the switch, but without more details of the context, it's hard to say.

As for performance, your best bet is to use a profiler to measure the performance of your application in conditions that are similar to what you expect in the wild. Otherwise, you're probably optimizing in the wrong place and in the wrong way.

Jay Bazuzi
A: 

I would say use SWITCH. This way you only have to implement differing outcomes. Your ten identical cases can use the default. Should one change all you need to is explicitly implement the change, no need to edit the default. It's also far easier to add or remove cases from a SWITCH than to edit IF and ELSEIF.

switch(numerror){
    ERROR_20 : { fire_special_event(); } break;
    default : { null; } break;
}

Maybe even test your condition (in this case numerror) against a list of possibilities, an array perhaps so your SWITCH isn't even used unless there definately will be an outcome.

lewis
There are about 30 errors total. 10 require the special action, so I am using the default for the ~20 errors that do not require an action...
Zing-
Completely got the wrong end of the stick!
lewis
+2  A: 

I agree with the compacity of the switch solution but IMO you're hijacking the switch here.
The purpose of the switch is to have different handling depending on the value.
If you had to explain your algo in pseudo-code, you'd use an if because, semantically, that's what it is: if whatever_error do this...
So unless you intend someday to change your code to have specific code for each error, I would use if.

François
I disagree, for the same reason that I disagree with the fall-though case. I read the switch as "In cases 01,07,0A,10,15,16 and 20 fire special event." There's no fall-though to another section., This is just an artifact of the C++ syntax where you repeat the 'case' keyword for each value.
MSalters
A: 

Seeing as you only have 30 error codes, code up your own jump table, then you make all optimisation choices yourself (jump will always be quickest), rather than hope the compiler will do the right thing. It also makes the code very small (apart from the static declaration of the jump table). It also has the side benefit that with a debugger you can modify the behaviour at runtime should you so need, just by poking the table data directly.

Greg Whitfield
Wow, that seems like a way to turn a simple problem into a complex one. Why go to all that trouble when the compiler will do a great job for you. Plus it's apparently an error handler, so it's not likely to be so speed critical. A switch is by far the easiest thing to read and maintain.
MrZebra
A table is hardly complex - in fact it's probably simpler than a switch to code. And the statement did mention performance was a factor.
Greg Whitfield
+3  A: 

The switch is faster.

Just try if/else-ing 30 different values inside a loop, and compare it to the same code using switch to see how much faster the switch is.

Now, the switch has one real problem : The switch must know at compile time the values inside each case. This means that the following code:

// WON'T COMPILE
extern const int MY_VALUE ;

void doSomething(const int p_iValue)
{
    switch(p_iValue)
    {
       case MY_VALUE : /* do something */ ; break ;
       default : /* do something else */ ; break ;
    }
}

won't compile.

Most people will then use defines (Aargh!), and others will declare and define constant variables in the same compilation unit. For example:

// WILL COMPILE
const int MY_VALUE = 25 ;

void doSomething(const int p_iValue)
{
    switch(p_iValue)
    {
       case MY_VALUE : /* do something */ ; break ;
       default : /* do something else */ ; break ;
    }
}

So, in the end, the developper must choose between "speed + clarity" vs. "code coupling".

(Not that a switch can't be written to be confusing as hell... Most the switch I currently see are of this "confusing" category"... But this is another story...)

EDIT:

bk1e added the following comment: "Defining constants as enums in a header file is another way to handle this".

Of course it is. The point of an extern type was to decouple the value from the source. Defining this value as a macro, as a simple const int declaration, or even as an enum has the side-effect of inlining the value. Thus, should the define, the enum value, or the const int value change, a recompilation would be needed. The extern declaration means the there is no need to recompile in case of value change, but in the other hand, makes it impossible to use switch. The conclusion being Using switch will increase coupling between the switch code and the variables used as cases. When it is Ok, then use switch. When it isn't, then, no surprise.

paercebal
Defining constants as enums in a header file is another way to handle this.
bk1e
+2  A: 

Code for readability. If you want to know what performs better, use a profiler, as optimizations and compilers vary, and performance issues are rarely where people think they are.

Bdoserror
+10  A: 

For the special case that you've provided in your example, the clearest code is probably:

if (RequiresSpecialEvent(numError))
    fire_special_event();

Obviously this just moves the problem to a different area of the code, but now you have the opportunity to reuse this test. You also have more options for how to solve it. You could use std::set, for example:

bool RequiresSpecialEvent(int numError)
{
    return specialSet.find(numError) != specialSet.end();
}

I'm not suggesting that this is the best implementation of RequiresSpecialEvent, just that it's an option. You can still use a switch or if-else chain, or a lookup table, or some bit-manipulation on the value, whatever. The more obscure your decision process becomes, the more value you'll derive from having it in an isolated function.

Mark Ransom
This is so true. The readability is so much better than both the switch and the if-statements. I was actually going to answer something like this myself, but you beat me to it. :-)
mlarsen
A: 

Aesthetically I tend to favor this approach.

unsigned int special_events[] = {
    ERROR_01,
    ERROR_07,
    ERROR_0A,
    ERROR_10,
    ERROR_15,
    ERROR_16,
    ERROR_20
 };
 int special_events_length = sizeof (special_events) / sizeof (unsigned int);

 void process_event(unsigned int numError) {
     for (int i = 0; i < special_events_length; i++) {
         if (numError == special_events[i]) {
             fire_special_event();
             break;
          }
     }
  }

Make the data a little smarter so we can make the logic a little dumber.

I realize it looks weird. Here's the inspiration (from how I'd do it in Python):

special_events = [
    ERROR_01,
    ERROR_07,
    ERROR_0A,
    ERROR_10,
    ERROR_15,
    ERROR_16,
    ERROR_20,
    ]
def process_event(numError):
    if numError in special_events:
         fire_special_event()
mbac32768
A language's syntax *does* have an effect on how we implement a solution... => It looks ugly in C and nice in Python. :)
rlerallut
paperhorse
+2  A: 

Use switch, it is what it's for and what programmers expect.

I would put the redundant case labels in though - just to make people feel comfortable, I was trying to remember when / what the rules are for leaving them out.
You don't want the next programmer working on it to have to do any unnecessary thinking about language details (it might be you in a few months time!)

Martin Beckett
A: 

Could we also tag this "compiler"? There is also a very low-frequency "compiler-technology" tag that is really the most appropriate.

jakobengblom2
A: 

I know its old but

public class SwitchTest {
static final int max = 100000;

public static void main(String[] args) {

int counter1 = 0;
long start1 = 0l;
long total1 = 0l;

int counter2 = 0;
long start2 = 0l;
long total2 = 0l;
boolean loop = true;

start1 = System.currentTimeMillis();
while (true) {
  if (counter1 == max) {
    break;
  } else {
    counter1++;
  }
}
total1 = System.currentTimeMillis() - start1;

start2 = System.currentTimeMillis();
while (loop) {
  switch (counter2) {
    case max:
      loop = false;
      break;
    default:
      counter2++;
  }
}
total2 = System.currentTimeMillis() - start2;

System.out.println("While if/else: " + total1 + "ms");
System.out.println("Switch: " + total2 + "ms");
System.out.println("Max Loops: " + max);

System.exit(0);
}
}

Varying the loop count changes a lot:

While if/else: 5ms Switch: 1ms Max Loops: 100000

While if/else: 5ms Switch: 3ms Max Loops: 1000000

While if/else: 5ms Switch: 14ms Max Loops: 10000000

While if/else: 5ms Switch: 149ms Max Loops: 100000000

(add more statements if you want)

McAnix
Good point, but sry, dude, you're in the wrong language. Varying the language changes a lot ;)
Gabriel Schreiber
A: 

while (true) != while (loop) Probably the first one is optimised by the compiler, that would explain why the second loop is slower when increasing loop count.

MarioFrost