4 ms·
'switch' is a really, really nice language construct that was fully implemented long before C89. Using lots of nested 'if's instead is not a good idea.
by peterfirefly 3y ago
'switch' is a really, really nice language construct that was fully implemented long before C89. Using lots of nested 'if's instead is not a good idea.
- sylware 3y agonested "ifs" are optimized out by compilers. Moreover in the latest horrible gcc extensions you have the case statement using a _not_compiler constant expression (you can find the usage of such horrible gcc extension in linux net code).
- mnurzia 3y agoThis was my one of my main justifications for making this design choice, in addition to the (in my opinion) overwhelming amount of break statements that would result from using switches. But more importantly, many of the "if" statements have non-constant or more complex expressions in them that aren't supported in switch statements in ANSI C.
- sylware 3y agoYep. And as you stated, it is important to stay as much as possible close to c89, because ISO is literaly doing planned obsolescence, but on a long time cycle (5-10 years). Hopefully risc-v will be a success, and all system components and interpreters of very-high-level languages will be rewritten in risc-v assembly and it will become actually very hard to do planned-obsolescence.
- hgs3 3y ago'switch' is good, but for VM's computed goto is better.