Should I use an if or switch statement? Objective-c

I always wondered, but I can not find the final answer. When should I use an if or switch statement? When is one better than the other?

For example, I could do:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (buttonIndex) { case 0: //Stuff break; case 1: //Stuff break; default: break; } //OR if (buttonIndex == 0) { //Stuff } if (buttonIndex == 1) { //Stuff } } 

This is just an example, but I’m sure that in different situations it will have different meanings.

+4
source share
8 answers

I think this is a subjective question. The answer is really up to you.

For my own code, I like to use switch , since it is pretty clear that what happens is a result of some condition that other things can do if the condition is different.

But if the code under the switch case: rather long or confusing, then for my own code I do the second: if (buttonIndex == 0) { bit.

+7
source

In this case, it does not matter. The reason is that it is faster than if. If you write long if ... else if ... else if ... code, it will work slower than the switch, because if performs an iterative search (O (n)), and switch searches the table (O ( 1))

+13
source

If you are only checking to see if one variable has a specific value (buttonIndex == 1), and you need to use multiple values ​​for this, you better use switch .

However, if you use more complex conditions (perhaps even including functions in state), then you should use if statements .

+4
source

I would like the code as close to human understanding as possible. This is why the three forms of solutions (if-then-else, switch-case-default ,? :) are suitable for matching three different processes:

  • ... and now there is fibromatic quibplatz in the event, we have to do ... (otherwise ... should be done) is a mapping for if )
  • ... and now, based on the value of quibplatz, we must do this ... if it is 0, ... if it is 1 (, ... if it is none of them), if the mapping is for switch
  • ... we use ... or ... based on the fact if ... has or not a mapping for ?:

Therefore, use one that compares with how you think of the actual decision as a person.

Of course, it should be emphasized that the correct OO solution in many scenarios ... and we do ... what we do this way for ..., so for .. and this way for ... where it is often used switch (but it matters for some if ), is to not use these constructs and not replace them with polymophism. Manual decision-making should be performed only when there is a real solution, and not a fake (logical) polymorphism.

+3
source

Switch statements execute nested if statements faster. It.

+1
source

Switch exactly the same as if elseif . Too much Switch or if elseif will make your code very difficult to read and understand. But there is no function.

0
source

it depends on what you want to do inside it. keep in mind that you cannot assign a new object inside a switch statement

-1
source

If "if" and "switch" are so evil, they should have been removed from modern programming languages. The fact is that they are still there. I think the key point is that we should try to make our code workable and efficient, understandable and supportive. OOP is just one way to write programs; we should not write programs for pure OOP. So this is really a subjective problem.

-3
source

Source: https://habr.com/ru/post/1384478/


All Articles