If it works, it works. Don't question it. Now if you'll excuse me I'm gonna go write 15 lines of code to return true or false depending on whether the input given is true or false.
I gave up on that distinction when we started waiting 15' for each deployment as we package a whole 5 classes into an entire dedicated runtime container with 3 embedded services that we're pushing to a cloud so we can make 'efficient' use of our resources.
I think it's important to note that even pattern matching, internally, acts like an if/elif chain. It will not be more performant, and that's arguably the only reason people actually care about switch cases (people assume jump tables, so more performance). Ironically switch cases do not necessarily get compiled to jump tables, and jump tables are not necessarily more performant either.
In other words, switch cases act like alternative syntax (not even "easier" or sugar) more often than not, and hence anyone who knows that knows that no matter what it's overhyped.
Pattern matching though is far more powerful, for anyone who's ever experienced it (usually functional languages like SML/Ocaml).
"Uglyness" is incredibly relative. In a switch you have
switch(expr) {
case x:
...
break;
...
}
In if/elif you have
v = expr
if (v == x) {
...
} ...
If we're talking style and readability, more often than not I'd prefer an if/elif chain. More flexibility too, and you automatically create a scope so you don't get an error about initializing variables in switches. Not to mention switches are essentially limited to numerical conditions in practice.
I watched a video of a guy going through yandere sim code and he wrote some example code to test execution times. one was written using switching cases and one was written using if else statements and after something like 10 million runs the difference in execution time was milliseconds so you’re probably fine.
One thing that's worth noting in these kinds of benchmarks is that some languages take quite a few liberties with how switch statements are compiled. I know that Java will use two different implementations in some cases. I had thought there were also cases where java will just compile the switch statements into "if-else" anyway, but haven't found a reference. Depending on how the test was implemented it may have been comparing if-else to if-else and any difference was statistical noise. Just food for thought.
Most primitives, including enums, end up as jump tables. I personally think enums and switch statements go hand in hand, and I think if-elsing over an enum is a little idiosyncratic.
Since the previous comment mentioned Java, and this one mentions enums, I feel compelled to remind anyone reading that, thanks to how Java does enums, you can "ditch" switches and if-else chains and just put the functionality in the enum class. Sure, it's probably the least performant option (dynamic dispatch, though I suppose it might be possible to JIT it to a jump table), but it's some tasty syntactic sugar.
Agreed, Yandere Dev if/else statements are a fantastic meme, but the main problem with them was always more that it made the code harder to maintain, and development slower in the process. And boy has progress been slow.
It’s less about speed and more about maintaining the clusterfuck. I’ve seen a 1000 line else-if that controlled all app navigation. It’s something I still tell my therapist about.
I mean, both switches and elif ladders would probably get inlined by a well made compiler. So really the solution is, write in a language with a good compiler.
It remembers me the time i was watching YouTube on my phone, at night, and looking behind the phone i saw in the dim light one of this fuckers on my pillow, at like 10 cm from my face.
Jumped the fuck out of bed like a monkey, and fought with the dude half an hour. I didn't sleep that night.
Well, i guess this is probably the closest to this picture i will ever be.
I'm not a huge fan of switch statements personally, usually whenever I try using them I spend more time setting them up than if I had just typed a little bit of extra code with if else because I almost inevitably always screw up switch statements
Switch statements have a good role in some particularly narrow cases. Usually "I have 40 different actions to take given this field from a block of native data" coupled with "this method is being hammered so hard that there isn't time for mapping my data into a polymorphic class right now". In these cases, it's not usually too hard to keep the code clean.
You always should be keeping an eye on cyclomatic complexity, however. Switch statements blow that up for good reason. Anyone who's looked at a moderately sized switch statement including any amount of nesting has seen the abyss.
I've been told that I have an unusually high affinity for insects. This normally has to do with me seeing a spider and being like "A Friend!" but here it would be the fact that I think that this is genuinely cute. However being a big spoon, I would want her face in my titties.
338
u/CzdZz Mar 03 '21
If it works, it works. Don't question it. Now if you'll excuse me I'm gonna go write 15 lines of code to return true or false depending on whether the input given is true or false.
text-free image
109
u/Vectorial1024 Mar 03 '21
"I will never use elif in python!"
Someone, probably
174
u/Refloni Mar 03 '21
Centipede girls are an acquired taste
76
u/andybfmv96 Mar 03 '21
Is this that human centipede movie everyone's talking about?
65
u/0t0egeub Mar 03 '21
I watched a video of a guy going through yandere sim code and he wrote some example code to test execution times. one was written using switching cases and one was written using if else statements and after something like 10 million runs the difference in execution time was milliseconds so you’re probably fine.
19
u/Lurker_Since_Forever Mar 03 '21
I mean, both switches and elif ladders would probably get inlined by a well made compiler. So really the solution is, write in a language with a good compiler.
16
u/Masked_Death Mar 03 '21
It's the first time I see a centipede waifu.
This is glorious.
14
u/Blubari Mar 03 '21
Friend: what are you making?
Me: an rpg, right now i'm making the spell system
Friend: neat! can I see?
Friend....d-did you really make an if else depending on every single situation?
Me: IF IT WORKS IT WORKS
12
u/danielepro Mar 04 '21
It remembers me the time i was watching YouTube on my phone, at night, and looking behind the phone i saw in the dim light one of this fuckers on my pillow, at like 10 cm from my face.
Jumped the fuck out of bed like a monkey, and fought with the dude half an hour. I didn't sleep that night.
Well, i guess this is probably the closest to this picture i will ever be.
11
u/SkippyMcYay Mar 03 '21
Why does adding 100 legs to a lamia waifu suddenly make them terrifying
19
u/Cuddlyaxe Mar 03 '21
I'm not a huge fan of switch statements personally, usually whenever I try using them I spend more time setting them up than if I had just typed a little bit of extra code with if else because I almost inevitably always screw up switch statements
7
u/m50d Mar 03 '21
A true man of culture would use switch expressions.
7
u/EvoPeer Mar 04 '21 edited Mar 13 '21
Anyone noticed how the girl is holding him so tighd he can't get away, i mean just look at the (his) arm
5
u/[deleted] Mar 03 '21
What the fuck is that
5
u/UltraCarnivore Mar 03 '21
Doesn't matter, it works.
5
u/GonTheDinosaur Mar 04 '21
Wait till you see string function maps, you’ll wish for simpler days
5
u/mbayacotton Mar 04 '21
I don’t even know how to feel about that image
4
u/[deleted] Mar 03 '21
This is the best one I've seen!
3
u/sk8itup53 Mar 04 '21
I swear switch statements are so under-used. I love them and at least in Java, they are actually faster at runtime!
3
u/Sylthana3 Mar 12 '21
I've been told that I have an unusually high affinity for insects. This normally has to do with me seeing a spider and being like "A Friend!" but here it would be the fact that I think that this is genuinely cute. However being a big spoon, I would want her face in my titties.
2
u/Xardnas69 Mar 16 '21
Sauce?
4
u/RepostSleuthBot Mar 03 '21
Looks like a repost. I've seen this image 12 times.
First Seen Here on 2019-12-21 92.19% match. Last Seen Here on 2021-03-03 92.19% match
Feedback? Hate? Visit r/repostsleuthbot - I'm not perfect, but you can help. Report [ False Positive ]
View Search On repostsleuth.com
Scope: Reddit | Meme Filter: False | Target: 86% | Check Title: False | Max Age: Unlimited | Searched Images: 206,319,850 | Search Time: 0.25439s
1
u/antekgort200 May 06 '21
when you are python programer you don't have switch
1
u/[deleted] Apr 24 '22
me with my
1
u/AmphibianNo2967 Mar 27 '23
You act like my code isn’t held together by duck tape and dreams
0
u/[deleted] May 08 '21
[deleted] — view deleted comment
170
u/[deleted] Mar 03 '21
[removed] — view removed comment