Programmers say: why do you like to use if… Else if instead of switch?


Please take 5 seconds to check the following code for bugs.



OK, the skilled programmer should have found the bug. I didn’t add the keyword break under line 13; This makes the behavior logic of this code inconsistent with my original design intention.


Shortcoming 1 Correct syntax, logical error

This is the first reason why program apes rarely use switch to make conditional judgment. It is very common for novices to forget to write break. Even old apes forget to write from time to time, and this grammatical error cannot be checked out on many grammar checkers, because it is correct from the perspective of grammar! But the processing logic of the code is wrong! If you rewrite this code with if, this error will not occur.

If can be used as long as you care about the conditions. Generally, there will be no grammatical problems (pay attention to judge congruence = = Oh). Once a bug occurs, it must be my logical problem.


Disadvantage 2 Rigid grammar

Although switch is very tolerant of break, it is very strict with judgment conditions. Only integer constants can be used as judgment conditions after case. For such a natural and unrestrained program ape, this restriction is too troublesome. If, let alone constants, I can use functions, which is really convenient and fast.


Disadvantage 3 Subfunctions are required to handle branches

This drawback is related to drawback 1. In order to prevent missing break, it is suggested to separate the branch processing method into a sub function to deal with it. In this way, the bugs caused by forgetting to write break will be reduced when reading the code. If I write it with if, I can write it as I want, which is very free, but it also leads to a great reduction in the readability of the code.


Advantages of switch

Since switch has such serious shortcomings, how can it still exist in all languages? Let’s talk about the advantages of switch. Its advantages are just its disadvantages.

A long time ago, when the computer performance was not as good as that of a bully learning machine, in order to improve the processing speed of the computer, smart computer scientists simplified some logic branch processing methods, fixed some operations that needed to make logical judgment, and then they could make corresponding responses one by one as long as they looked up the table.


For example, for the judgment of a = 0, the processing methods of switch and if on the CPU are different. Switch binds the sub function address and judgment conditions in the compilation stage. As long as a is directly mapped to the sub function address for execution, but if processing is different.

It first puts the value of a into the CPU register, then puts the compared value into another CPU register, then subtracts, and then jumps to the sub function according to the calculation results. In this way, there will be three more operations. If there are many logical judgments, there will be many times more operations than switch, although the register operation is very fast, But for the learning machine at that time, this speed was not enough.

There’s another question. Why use break to make a judgment? Isn’t it easy to make grammatical mistakes? Then let’s talk about the sub function.


In the early computer code, there is no concept of sub function. At that time, goto was used to jump freely. If you want to go to line 10, it is very simple. Goto 10 is OK. This kind of programming thinking was always affected in the early stage of C, so there were no sub functions in the early stage of C. It was a pile of logic processing confusion, and goto flew all over the sky, so you couldn’t write a program without a strongest brain at that time. In order to tell the program that the conditional judgment processing ends here, break is added as the termination symbol. Later, with subroutines and better programming specifications, writing code was reduced to manual labor step by step.

In order to flaunt their ancestry, the new language developed later had to refer to C, and then inherited the strange grammar of switch. However, not all languages are copied. For example, the new languages golang and kotlin invented by Google wrap the switch again, remove the misleading syntax, and make the switch more flexible. By the way, when refactoring the code, we still use switch to make the code more concise!


Click me to enter the shortcut