Re:“Clean” Code, Horrible Performance
by jbverschoor on Tuesday February 28, 2023 @06:57PM
(#34976703)
switch isn't 'invented' for enums. switch is a low-level construct which mimics several goto's / jumps. It's just as bad, except for the case where you have either: a) multiple behaviors for the same value, b) need to pass through (not break). b) is the nr 1 reason I hate switches, and my nr 2 reason is that most languages don't support proper enums, and will fail when you don't handle all possible values
[ Parent
| on HN ]
Re:“Clean” Code, Horrible Performance
by EVa5I7bHFq9mnYK on Tuesday February 28, 2023 @09:01PM
(#34977797)
Switch was invented because it allows to replace several ifs and gotos with a precomputed static jump table, an optimization trick.
Switch is not bad, far less is there any reason to hate it.
Agree. It's the same as saying don't use for loops or any other basic language constructs. Switch is very useful, please leave switch alone! You will not take switch away from me :)
Re:“Clean” Code, Horrible Performance
by jbverschoor on Wednesday March 01, 2023 @05:48AM
(#34980889)
When do you actually pass through? Only some state machines do that. But even I. That case, ifs are more clear and less error prone due to missing breaks, braces/scoping issues, etc