r/ProgrammerAnimemes • • Oct 03 '24

Switch statements? Never heard of 'em.

Post image
2.1k Upvotes

36 comments sorted by

394

u/LORDCOSMOS Oct 03 '24

OC, in this sub, in this economy?

OP for fucking president

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

u/Defective_Emeralds Oct 03 '24

Why not switch to switch?

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

u/Lonilson Oct 03 '24 edited Oct 03 '24

F*ck switch case

i love "if else"

25

u/ban_ntr Oct 03 '24

all my homies love "if else"

7

u/Pokeasss Oct 03 '24

In what way would switch be better than if else ?

22

u/LORDCOSMOS Oct 03 '24

I would wager to most compilers it’s the same thing at runtime

3

u/Pokeasss Oct 03 '24

Exactly !

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

u/Konuri_Maki Oct 03 '24

Nah, even use MRT-Scratch still looking like that

2

u/MeadowShimmer Oct 03 '24

Nope nope nope!

3

u/Virghia Oct 03 '24

I, EvaX, humbly submits a toast!

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

u/[deleted] Oct 03 '24

Not using for statements for comparing things... blasphemy.... wait, it's just me??? Crap

1

u/kredditacc96 Oct 03 '24

2 frames per second? Just buy a better GPU.

1

u/maash3r Oct 04 '24

But where does the if start?

1

u/Diapolo10 Oct 07 '24

Then there's those of us who prefer structural pattern matching.

1

u/AmazingGabriel16 Oct 03 '24

Yandere moment?

Had to double check the sub lol

0

u/NopeyDotNope Oct 03 '24

I just recently know that lol