Take 5 seconds to see if the code below is buggy.
OK, skilled programmers should have spotted the Bug. I didn’t add the keyword break under lines 8 and 10; This causes the behavior logic of the code to be different from what I designed it to be.
Shortcoming 1. Correct grammar, logical error
This is the first reason why application apes rarely use the switch to do the condition judgment, for beginners to forget to write break is ordinary but again, even the old forgot also happen from time to tome, apes and the grammatical mistakes in grammar checker, there is no way to check out on because from grammatical point of view is correct! But the processing logic of the code is wrong! This error would not occur if the code was rewritten using if.
If I forget to write an else, the syntax compiler will prompt me to add curly braces. I can even use a tool like ESLint to force me to use curly braces. This way, I won’t make any grammar mistakes. If there is a bug, it must be my logic.
Weakness 2. Rigid grammar
Switch, while forgiving of breaks, is very strict about judging conditions. Cases can only be followed by constants, or even ints if you’re writing in C. For us so natural and easy to program ape, this limitation is really too troublesome, use if, let alone constant, I can use the function, really convenient and fast.
Disadvantage 3. Need subfunctions to handle branches
The disadvantages associated with weakness, in order to prevent leakage write break, so the recommended treatment method independent branch into a child function to deal with, so when reading the code will reduce the bug forgot to write a break, so use if to write, how I want to write how to write, very casual freedom, but it also led to the readability of the code is reduced greatly.
The advantages of the switch
If switch has such serious flaws, how does it still exist in all languages? On the upside of the Switch, its upside is also its downside.
In a long time ago, at that time, computer performance is worse than a small bully machine learning, intelligent computer scientists in order to improve the processing speed of computer, some logic branch simplifies the processing methods, gave some of the need to perform the logic operation to fixed die, and then just as one of the look-up table can react accordingly.
For example, when a=0 is determined, switch and if are handled differently on CPU. Switch binds the address of the subfunction to the judgment condition during compilation, so it is ok to directly map the address of A to the address of the subfunction, but if is handled differently.
It’ll be the first to put a value in the CPU registers, and then to compare the value of the put in another of the CPU registers, and then do subtraction, and then jump to sub function according to the results of the calculation to perform, so that will be more than 3 steps of the operation, if more than logic, then will be many times more than than the switch operation, Although register operation was fast, it was not fast enough for the learning machine of the time.
Then there is another question, why use break to make a judgment end? Isn’t it easy to make grammatical mistakes? That brings us to the question of subfunctions.
In the early days of computer code, there was no notion of a subfunction, it was all goto, you wanted to goto line 10, it was just goto 10.
This kind of programming mentality was still affected in the early days of C, so early C had no subfunctions, it was just a jumble of logical processing, gotos flying around, so you couldn’t write programs without a strong brain.
So to tell my program that I’m done with the condition, I add a break. Then, with the advent of subroutines and better programming specifications, code was gradually reduced to manual labor.
Later languages developed to assert their lineage more or less refer to C, and then inherit the weird syntax switch.
However, not all languages are copied. For example, Golang and Kotlin, the new languages invented by Google, have packaged switch to remove the misleading syntax and make switch more flexible. By the way, when refactoring code, switch is still used, which makes the code look simpler.
From: 3 g.163.com/tech/article/e02rde6c0511sddl.html