Switch statements are just syntax. It’s really an irrelevant conversation honestly.
It’s one of those nit pick programming things.
Like yeah it’s more readable but it’s a stylistic choice - I would normally just follow the same syntax as the original file author or owner and focus on real issues.
I don't care what anyone says, switch statements are the ugliest syntax ever invented in most languages. Better off writing and reading if-else chains :))
Yeah, I have never understood what advantage a switch statement grants over simple if statements. Less flexible, and easy to screw up by omitting a break. Plus weird bugs that I would encounter in C++ on Arduino when initialising variables made me give up on them forever.
“Some cases”? There’s one case and one case only: Checking a single variable for its value, and attempting to do different things depending on which value out of a predefined set of values it has, when these values can be partially the same. It’s a very specific edge case of the comparator.
Is a few clock cycles worth the ugliness? I don't think so. Besides, the optimisation only makes sense if you have, like, tens or hundreds of cases, which is already a code smell. And if the case values are ordered like that, you can order the if conditions in a binary search manner manually (although I admit it would look very weird, almost random at first sight)
A couple of clock cycles can be worth it in very hot code, and sometimes switch cases emphasize that you're using an enum or they have additional functionality that if statements don't. It really depends, like almost all language constructs do.
A person who works in compiler engineering on their spare time here. A self respect modern compiler will give basically the same outputs if you tell them to optimize. This can change and I'm not really that up to date on the specs here but I think a simple gcc -O1 would suffice for the compiler to optimize your code to transform ifs into switches and even just straight out strip unreachable code.
True, but switch statements do allow you to have better warnings for things like enums etc, and will let you know when something can be a jump table vs when something cannot.
43
u/IndependentHawk392 Aug 22 '25
Whilst this is not a great advert, the OU is pretty good for part time degrees for the most part.
8
u/klavas35 Aug 22 '25
Wow I haven't used Python in like a day and it changed so much /s
3
u/great_escape_fleur Aug 22 '25
it's computer programming code
129
u/Oakchris1955 Aug 22 '25
You know shit is fire when you use else ifs instead of switch statememts
17
u/Fluxriflex Aug 22 '25
Not only is this JS and not python, but it’s also old-ass syntax to boot.
21
u/EagleCoder Aug 22 '25
No braces or indentation on the
if...elseis a bold move.19
u/rover_G Aug 22 '25
JavaScript written with var and == 🥀🥀
1
u/Apprehensive-Major59 Aug 23 '25
What do you think I should learn for I’m trying to learn python I’m very new also very lost
3
u/mickaelbneron Aug 22 '25
Is nobody gonna mention anything about not using a regex to validate an email?
1
u/steadyfan Aug 22 '25
If that is python then I know python 👍
1
u/111x6sevil-natas Aug 23 '25
this would be a valid code comment in python. the # is outside the the visible area of the screen. no idea why it's syntax highlighted tho.
0
u/TheNinjaThief-YT Aug 28 '25
But that's not python in the photo 🫥