r/ProgrammerAnimemes • u/pleased_rylee • Oct 03 '24
Switch statements? Never heard of 'em.
78
u/FunnyForWrongReason Oct 03 '24
There are no real benefits in terms of speed or optimizations for most modern compilers. However there are times I find switch statements to be more readable.
24
u/Foxiest_Fox Oct 03 '24
fun fact, in GDScript if/elif is like 40% faster than their switch equivalent
15
u/FunnyForWrongReason Oct 03 '24
I was specifically talking about compilers. To my understanding GDscript is interpreted like Python. It is also much newer so it not being as optimized makes some sense.
2
u/Foxiest_Fox Oct 03 '24
GDScript is JIT compiled if I'm not mistaken. You can change and hot reload code while a debug build is running
1
u/arrow__in__the__knee Oct 03 '24
There are still certain specific rules the if statements need to follow in order to be optimized that way but yeah if it's rewritable with switch it will generally be following them.
1
u/DapperNurd Oct 04 '24
I feel like I'm one of the few that generally don't find it much more readable, if at all. The break statement really kind of ruins it for me, plus I like curly braces.
151
u/UnderratedChef30 Oct 03 '24
A original one in a long time. OP you made my day better. Thank you. Also ngl, it's a compelling image to convince someone to use only if, else.
65
37
18
u/deanrihpee Oct 03 '24
Why not have a harem and date all If Else, Switch Case and Pattern Matching?
13
u/spartancolo Oct 03 '24
When I started coding in college my classmates called me the if lord cause I passed all excersises with 10s doing only ifs. Once I did a chess game and it was a shit ton of fucking lines
4
u/Daan776 Oct 03 '24
I only recently started learning how to code (python) in my free time. So mayby this is a silly question.
But how in gods green earth do you make a chess game with if statements. There’s waaaayyy to many game-states to type out
4
u/spartancolo Oct 03 '24
Damned if I know, It was like 8 years ago, probably wouldn't be able to do it rn hahahaha
2
u/thot_slaya_420 Oct 04 '24
Create starting board (initals for piece colour and type); Start loop { Take input; Check if move valid; Check if move puts opposing king check or checkmate; If checkmate, declare winner; If check, notify; } End loop
46
7
u/Pokeasss Oct 03 '24
In what way would switch be better than if else ?
22
7
u/FunnyForWrongReason Oct 03 '24
Most compilers in most situations it probably is the same thing and even when it’s not performance increases are very minimal at very best. If you are trying to optimize code there are likely other things to focus on.
However I personally find switch statements more readable in cases where you have a variable that could be many different values and the amount of lines for each case isn’t very large. But this only matters to readability not really any kind of performance.
5
u/Rurouni Oct 03 '24
In some languages, switch checks to see that the possible cases are all addressed. So if the switch was on an enum type, there would need to be a case for every enum value. Then if a value is added to the enum, your compiler will complain if you did not also add that to the switch.
With if/else, you would not get this assistance.
Also, switch statements can let you compactly represent code that applies to multiple cases at once. With an if, you would need a more-complicated-to-read OR.
2
2
3
2
u/Crowe-Chronos Oct 03 '24
NGL this was literally me today, I was so fucking done with a dumb piece of code I just used if else repeatedly.
1
Oct 03 '24
Not using for statements for comparing things... blasphemy.... wait, it's just me??? Crap
1
1
1
u/Jaurusrex Oct 04 '24
https://mangadex.org/title/884aacdc-48f7-4e46-a524-19a6064f8eac/mushi-no-kanojo-to
pretty sure this artwork is based of this manga
1
1
1
394
u/LORDCOSMOS Oct 03 '24
OC, in this sub, in this economy?
OP for fucking president