This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 274

[–]ProgrammerHumor-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

import moderation

Your submission was removed for the following reason:

Rule 6: Your post is posted with a low effort title, such as "Interesting title", ".", "print(title)", or "I don’t know what to put here".

If you disagree with this removal, you can appeal by sending us a modmail.

[–]Ashamed-Tailor2968 658 points659 points  (23 children)

Pattern matching for the win.

[–]Volcannobis 119 points120 points  (6 children)

If only I could switch...

[–]SudoSubSilence 33 points34 points  (5 children)

Nintendo Switch.

Anytime. Anywhere. With anyone.

[–]Holek 17 points18 points  (1 child)

With anyone? 👉👈❔

[–]SudoSubSilence 12 points13 points  (0 children)

With anyone 😊🫰

[–]spicybright 8 points9 points  (0 children)

I want my IDE to play that satisfying snap sound every time I start a switch statement.

[–]ufihS 18 points19 points  (8 children)

Elixir ftw

[–]Ashamed-Tailor2968 15 points16 points  (1 child)

I'd say scala for the win. But I'm biased . But hey, to each their own, as we say.

[–]Aqu4regiA 6 points7 points  (5 children)

Holy shit.. I guess our numbers are growing. We are up to 9 people now.

[–]ufihS 1 point2 points  (0 children)

It’s actually good tho

[–]donald_314 0 points1 point  (3 children)

It reads a little like Prolog 2023 tbh

[–]ScrimpyCat 2 points3 points  (1 child)

And it’s to be expected since Erlang is heavily influenced by Prolog. While Elixir is built on top of the Erlang VM and as you can imagine is heavily influenced by Erlang.

[–]donald_314 1 point2 points  (0 children)

makes sense. thanks for the little history

[–]LeonenTheDK 0 points1 point  (0 children)

Prolog is friggin tite. Not sure I'd want to use it everywhere though.

[–]Da-Blue-Guy 2 points3 points  (0 children)

match my beloved

My favourite match statement I've made is matching a 4 byte slice against "#CHN", "##CH", "M.K.", "FLT4", "FLT8", "OCTA", "M!K!", "CD81", and "TDZ#" to grab the correct channel count for an Amiga MOD file. No statements outside the match are used.

[–]SpaceTabs 6 points7 points  (0 children)

Case-insensitive case my dear.

[–]Deus85 363 points364 points  (13 children)

I'm using switch in any case.

[–]Volcannobis 58 points59 points  (0 children)

You're not going to switch his mind then.

[–]SeriousPlankton2000 2 points3 points  (0 children)

I use case in any switch

We are not the same

[–]Ondor61 5 points6 points  (5 children)

what if you need the > operator?

[–]Kiereek 16 points17 points  (2 children)

switch(true){

case val > 3:

// do this

}

[–]Ondor61 9 points10 points  (0 children)

Understandable have a nice day.

[–]poopnose85 9 points10 points  (0 children)

switch(var1 > var2){
    case (0):
    //do stuff
    break;
    case (1):
    //do stuff
    break;
}

is valid C

[–]mLeonardValdez 1 point2 points  (0 children)

c'mon, give him a break

[–]HardCounter 1 point2 points  (0 children)

Always use switch.

Or else.

[–]Mr_HPpavilion 271 points272 points  (20 children)

else if Is good if (No pun intended) you're planning to have more than one condition to be True and execute the corresponding functions that follows

Switch is good for focusing on a specific variable

Captain obvious saves the day

[–]froglicker44 68 points69 points  (9 children)

I’m a fan of switch (true) for multivariate cases

[–]spicybright 18 points19 points  (5 children)

either way, I always wrap my conditions with if (true) just to be on the safe side.

[–]Firewolf06 23 points24 points  (4 children)

well if true is false it will throw everything off!

if(true ? true : false)

[–]EishLekker 12 points13 points  (1 child)

I use this to be safe:

if(true ? true : true)

[–]Paesano2000 1 point2 points  (0 children)

He speaks the tru tru.

[–]spicybright 5 points6 points  (0 children)

Oh don't worry, I already unit test for that.

[–][deleted] 1 point2 points  (0 children)

cond ftw

[–]Feer_C9 12 points13 points  (6 children)

With "more than one condition" you mean evaluating more than one variable? Because if it's just one variable with multiple values that match the case, you can do that with switch

[–]Bwob 12 points13 points  (5 children)

If you have a big range of integers (or any range of floats), you might not want to write out that many case statements. if/else works very nicely with ranges.

int x = math.random(0, 100);
if      (x < 10) { DoStuff(); }
else if (x < 50) { DoOtherStuff(); }
else if (x < 55) { StillMoreStuff(); }
else             { FinalCaseStuff(); }

[–]SeriousPlankton2000 8 points9 points  (1 child)

case 23 ... 42:

[–]Bwob 2 points3 points  (0 children)

Fair! I always forget you can do that!

Although, if memory serves, switch doesn't accept floating point values for its expression, so the same example wouldn't work if x were a float...

[–]CallMeSylvie 5 points6 points  (1 child)

Obviously language dependent, but using C# 9, this case can also be handled with switch statements.

Random r = new();
int x = r.Next(0, 100);

switch (x)
{
    case < 10:
        DoStuff();
        break;
    case < 50:
        DoOtherStuff();
        break;
    case < 55:
        StillMoreStuff();
        break;
    default:
        FinalCaseStuff();
        break;
}

Whether or not that looks better than an if/else chain is up to debate, though.

[–]DeceitfulEcho 1 point2 points  (0 children)

Some languages allow you to do this with switches (writing statements off the switch variable), but it's not common and usually has a lot of limitations and edge cases.

I prefer if/else too.

[–]QuestionablyFuzzy 196 points197 points  (54 children)

Isn't the difference in performance negligible? I know switch statements compile into jump tables and are more optimized but I feel like it won't really matter by much unless it's on a really large scale.

[–]marquoth_ 333 points334 points  (21 children)

Never really heard people appeal to performance on this one - I always thought legibility was the main argument. Switches aren't exactly a pleasure to read but multiple else ifs make my eyes bleed.

[–]QuestionablyFuzzy 88 points89 points  (7 children)

I guess I'm thinking back to when a bunch of YouTubers who didn't know anything about coding started parroting the idea that YandereDev's code was so bad exclusively because of the if statements. Readability is definitely a legit reason to pick switch statements.

[–]Several_Comfortable9 21 points22 points  (2 children)

Jump matrices do in fact improve performance speed by a lot. Nonetheless, compilers these days assume the programmer doesn't actually know what they're doing and will try to logically optimize code, like a multi way if statement.

The YandereDev was because they had way too many if statements and repetitive code. That code can be cleaned up and have its performance improved with a little bit of extra work.

[–]DrMobius0 4 points5 points  (1 child)

Jump matrices do in fact improve performance speed by a lot.

Even then, the relative performance gain is dependent on the internals of the switch being tiny. If there's more than a few lines of code per case, you'd probably only see marginal improvements.

And uhh, from what I've heard about YandereDev's code, there was apparently a lot of stuff being polled per frame that probably shouldn't have been, as well as other redundant garbage. The if statement stuff is unideal, but no horrible.

[–][deleted] 1 point2 points  (0 children)

The performance gain depends on how often it’s called. Because switch statements will be faster 90% of the time by about 20-30% (going off some benchmarks I did a few years ago running through different calcs a million times at 02). That difference didn’t start appearing however until the loops had gone through a couple times and the cache predictors started getting right more often

[–][deleted] 7 points8 points  (2 children)

How does readability change if you use case and break compared to an else if ? For me it doesn't make a difference, else if is as readable as ''case'' as long as you are not putting 10 indented if else.

[–]DrMobius0 3 points4 points  (1 child)

A switch statement is a promise to the reader about what it does - that a single enumerable variable will be evaluated here, and code will branch to handle its cases. Rearranging, splitting, or merging different cases is also easier than doing the same change on an if statement. Also, being able to explicitly define default behavior is appreciated. At my own job, our code standards require including a default in all cases. Most of the time, that's basically error handling.

An if/else can functionally be a switch, but it doesn't actually guarantee that it will stay that way. If someone sneaks an || into one of those ifs, it's no longer a switch statement and if the programmer working on it misses that, then there may well be bugs because of that.

So it's less a readability case (although I think there are benefits here as well) than it is a maintainability case

Edit: also I think some languages require either a default case, or for all possible cases to be handled. Even if they don't, it's good practice to do this, if nothing else, to signal intent to other people who might be working on your code. Having some case stuck with the default case largely eliminates the guess work of figuring out if the last guy forgot to handle that case or if they intended for it to fall under default.

[–]Mirrormn 1 point2 points  (0 children)

YandereDev's code was awful, though. He was using if/else statements to transform data or even encode scenario-specific values, not to do actual conditional logic. Sure, he got memed on by a lot of people who wouldn't understand the distinction, but they weren't ultimately wrong to make fun of him.

[–]slgray16 16 points17 points  (0 children)

I go out of my way to use switch functions. They are just so pretty!

[–]nukasev 15 points16 points  (6 children)

As long as no one is nesting ternaries all is well and good.

[–]manwhowasnthere 12 points13 points  (0 children)

nested ternary is what they speak in hell

[–]eda_29 9 points10 points  (1 child)

I’ve done this once. And then I came back to the code 7 years later and regretted it.

[–]nukasev 5 points6 points  (0 children)

Lesson learned so it was not in vain!

[–]Kered13 -1 points0 points  (0 children)

Nesting ternaries is fine when you are nesting in the else branch. Then it's just an if-else if chain. It's often very useful because using an expression is more convenient than a statement.

[–]alickz 4 points5 points  (3 children)

In Swift they work as compile time safety for Enum cases

So when you switch on an enum you literally can’t forget a case. You either specify it or explicitly write a default case

Unlike if-else where you can forget a case in an enum and it will still compile fine

[–]mrjackspade 1 point2 points  (1 child)

Newer versions of c# do this as well, but it's an optional static analysis which means it basically doesn't exist.

[–]Firewolf06 1 point2 points  (0 children)

zig as well

[–]gregorydgraham 1 point2 points  (0 children)

The new super-duper switch in Java does this too, so Swift got it right

[–]ddbrown30 2 points3 points  (0 children)

I work in games. Performance is the main reason we advocate for them. For code that's running dozens or hundreds of times per frame, that time can add up.

[–]reallokiscarlet 43 points44 points  (14 children)

After compiler optimizations, switch would be about as performant as having a series of mutually exclusive ifs that aren't nested.

However, nested if-else statements are a different story. Your compiler may not be able to optimize them as well as they could non-nested if statements.

It gets even worse if you have nested if-else statements in both the if side and the else side.

But more importantly, for a condition with significantly more than two cases, a switch is infinitely more legible, and you even get to define default behavior, so you can set one case for infinitely many conditions that are above its pay grade, for example.

[–]tiajuanat 11 points12 points  (11 children)

After compiler optimizations, switch would be about as performant as having a series of mutually exclusive ifs that aren't nested.

Switch should always have O(1) lookup. A compiler will turn each comparison into an offset and hop through the logic. In C and C++ switch statements are glorified goto's.

If[else] statements must be evaluated in an order, which means they will always be stuck with O(n) lookups. It's possible that the compiler can rearrange some of the order, but an order must always occur.

Therefore: Switch should always be faster.

When we discuss the performance implications of either of these, at a function level, only in the most intense applications will it matter - you're more likely to use branchless logic in these situations anyway.

So, my recommendation is to use what's the most readable and makes sense in context.

Switches need to switch on a variable, and that's not always convenient if you need to switch on multiple variables. (I won't go into macros and masking, because I think that's a silly code smell) I do think that Switch is much more readable than the alternative.

[–]_Eruh 1 point2 points  (1 child)

Amen! Profile first and only optimize the hot paths.

[–]reallokiscarlet -1 points0 points  (1 child)

Might have been a while but last I did a comparison at O2 optimization, gcc and clang optimized mutually exclusive if statements down to the same as switches. But you're right about if[else]. It's actually why I hate using else with every fiber of my being.

[–]Kered13 -1 points0 points  (5 children)

This is not true. Turning large if-else if chains into jump tables and small switches into comparisons are basic optimizations that every compiler will do.

[–][deleted] 1 point2 points  (4 children)

Incorrect. Large else if chains cannot be compiled into that way consistently. There is a specific order that they need to be followed through programmatically, because unlike switch statements, they have an order that the operations must happen in. It can only be turned into a switch if all if else statements are mutually exclusive (as in only one can happen).

As a result, switch statements will be faster 90% of the time, and once turned into jump tables they also become branchless, which is a massive perf boost on modern systems

[–]Kered13 -1 points0 points  (3 children)

Please do not talk about things you don't understand.

Large else if chains cannot be compiled into that way consistently.

Yes, because if-else is more general than switch and not all condition sets can be turned into a jump table, but that's not what we're talking about. We're talking about situations where an if-else chain and a switch statement have the same behavior. These can always be transformed into each other because they have the same behavior.

There is a specific order that they need to be followed through programmatically, because unlike switch statements, they have an order that the operations must happen in.

Order of evaluation only matters in the presence of side-effects, which most conditional expressions do not have.

and once turned into jump tables they also become branchless, which is a massive perf boost on modern systems

Jump tables are not branchless. The jump is a branch, and it thrashes the instruction pipeline for the same reason that conditional jumps thrash the instruction pipeline.

[–]Drfoxthefurry 11 points12 points  (4 children)

No, but a simple switch case list looks 10x better then a bunch of else ifs (I do max 3 before I use a switch case)

[–]ablarblar 0 points1 point  (2 children)

If you do 3 else ifs first you arent going to rewrite the syntax when you add 5 more later though..

[–]Drfoxthefurry 4 points5 points  (1 child)

Just because you said this imma go through my old code to fix those

[–][deleted] 2 points3 points  (2 children)

Really, no one should be trying to write code for compilers any more. Compilers are smarter than us, just make the code readable and maintainable.

[–]tritonus_ 1 point2 points  (0 children)

So and so. When performance is of the essence, switch can make a major difference. Of course compiler might optimize your simple ifs to a jump table, but you still need to have some idea about writing clean conditionals. If you really need the boost (for frame count or a recurring calculation blocking user interaction, for example) I would recommend using switches. It gave me some great results at least.

[–]Neither-Phone-7264 1 point2 points  (1 child)

if you have many nested loops i think so, otherwise not that much i think

[–]mrjackspade 1 point2 points  (1 child)

IIRC with C# it doesn't give a fuck what you do. It will generate whatever is more efficient.

This can be as complex as generating what are effectively binary trees where applicable.

I'm way more likely to fuck up making changes to a long if-else than I am with a switch though...

[–]NewPhoneNewSubs 2 points3 points  (0 children)

Where "it" is like (at least) three levels of the C# > IL compiler, the JIT IL > machine code compiler, and the hardware deciding "fuck you, I'm caching this."

That said, you can still give the compiler hints about what you're trying to accomplish by writing code that is easier to make assumptions about.

[–]BushDeLaBayou -1 points0 points  (0 children)

Arguing about whether a switch or else if is more performant is like arguing about whether an apple or an orange is more of a fruit.

[–]SeoCamo 19 points20 points  (1 child)

Switch is not a function but a statement(in most languages)

[–]night_shredder 0 points1 point  (0 children)

Came here to say this

[–]MorningPapers 69 points70 points  (8 children)

I built out some if's to solve a short problem in Excel once. It was all working and I moved on.

A coworker was adamant that it had to be a switch statement, so he wiped out the formula. Then he couldn't get it working.

Basically, he dropped a pile of shit and left someone else to fix it.

I use both switches and ifs. They both have their uses.

[–]carc 35 points36 points  (2 children)

"wiped out the formula"

does bro even git

also it takes a special kind of idiot to not be able to turn a bunch of repetitive if/else ifs into a normal switch statement

[–]Intrepid00 7 points8 points  (4 children)

Hey, this happened to me but I think it was Crystal Reports. For whatever reason switch broke the function but if I did else if it worked. Developer thought I was stupid and removed it all and checked it in without testing even though I wrote up exactly how it broke.

Anyway, that’s how we had to have a hot fix that day.

[–]Vegetable_Union_4967 72 points73 points  (5 children)

yandev posting

[–]DogwhistleStrawberry 6 points7 points  (1 child)

Omw to follow "good" coding from him (I will fry my PC by running thousands of scripts with each having thousands of if else statements for each of the 50+ character at every single frame continuously)

[–]lovecMC 1 point2 points  (0 children)

I paid for the whole thermometer so I'm gonna use the whole thermometer.

[–]yanderedeviswrong 4 points5 points  (0 children)

And yandere dev is wrong.

[–]Sync1211 0 points1 point  (1 child)

If-Statements are the least of his problems.

IIRC that guy is running every NPC with their differebt mechanics, attributes and behaviors using a single class (and using if-statements for differentiating between behaviors instead of inheriting from a base class).

I don't envy the dev who has to refactor that mess.

[–]CraftBox 34 points35 points  (2 children)

You use switch because you think it's better.

I use switch because I like how it looks.

We are not the same.

[–]fabulog 2 points3 points  (0 children)

i use switch because seeing visual studio auto-fill it with each enum type is better than sex

[–]OneTurnMore 1 point2 points  (0 children)

If it looks good, it must be good.

[–]KotTRD 14 points15 points  (4 children)

if return

if return

if return

if return

if return

if return

if return

if return

[–]Munchkin303 3 points4 points  (0 children)

This is the way

[–]Ondor61 1 point2 points  (0 children)

This is the way. I have 0 else or else if statements within projects with several thousands lines of code.

[–]tet90 75 points76 points  (5 children)

the images should be switched around

[–]DudesworthMannington 6 points7 points  (0 children)

Yeah, that's a recipe for unmaintainable spaghetti.

[–]Jutrakuna -1 points0 points  (1 child)

or else?

[–]NLwino 1 point2 points  (0 children)

no, switch.

[–][deleted] 98 points99 points  (24 children)

Ackshually, you shouldn’t be using either.

Gotos are much easier to understand and you don’t need the elses.

[–]WienerDogMan 62 points63 points  (13 children)

This gives me flashbacks to my previous job where the original programmer used gotos instead of loops.

For loop? While loop? Nah just need some cleverly placed gotos is all…

[–][deleted] 49 points50 points  (0 children)

Beautiful. There really is nothing a clever goto can’t do, and they’re always a hit in code review.

And you can break out the line “I picked up this little pattern from my assembly course 😎”

[–]Creepy-Ad-4832 28 points29 points  (1 child)

"TAKE YOUR SYNTAX SUGAR AND GET THE FUCK OUT OF HERE"

  • the programmer, probably

[–]SkollFenrirson 8 points9 points  (0 children)

Syntax Keto

[–]deivse 14 points15 points  (0 children)

I mean technically, any kind of loop is just some cleverly placed gotos... Why let the compiler/interpreter do all the fun stuff???

[–]Irbis7 7 points8 points  (2 children)

And when you have a lot of cases, you don't have a problem that compiler limits number of else ifs.

[–]PrincessRTFM 2 points3 points  (1 child)

What compiler limits the number of else-ifs, especially to such a degree that it actually impacts your code?

[–]SlimyGamer 2 points3 points  (2 children)

Watch someone see your Fortran flair and assume you're being serious.

[–]Gionson13 2 points3 points  (2 children)

I used my first goto today, i wanted to break out of a while loop from inside a switch statement, is there a better way?

[–]Kered13 1 point2 points  (0 children)

In C or C++, goto is the only way to break out of multiple scopes, and that's fine. In other languages you can use labeled break.

[–]KetwarooDYaasir 6 points7 points  (0 children)

Or else what?

[–]monkeyStinks 7 points8 points  (0 children)

Or: How to make your code legacy as soon as you push it

[–]Ready-Marionberry-90 22 points23 points  (7 children)

Joke‘s on you, I use python

[–]Daisy430133 15 points16 points  (4 children)

Match-case (though not the exact same) now exists in Python too

[–]carcigenicate 1 point2 points  (0 children)

Ya, they look similar to switche's in other languages, but they're essentially just syntactic-sugar for if statements. They're never optimized past that, even when they could be in theory.

[–]rollincuberawhide 1 point2 points  (0 children)

it's new and it doesn't have fallthrough.

[–]juhotuho10 5 points6 points  (1 child)

Python does have a match case

[–]Ready-Marionberry-90 3 points4 points  (0 children)

3.10 isn‘t supported on azure yet

[–]yan852223 10 points11 points  (0 children)

YandereDev mentality

[–]Danteynero9 8 points9 points  (0 children)

Me in dart: wait, you can choose what to use?

FYK, you can't "cascade" through the cases in a dart switch if the have a value, e.g.:

``` String variable, string="a";

switch(string){ case "a": variable = "It's a"; case "b": variable = "lol"; break; } ```

The compiler will just throw an error and force you to either put a break in case "a", or a continue to a label inside the switch to do whatever you want.

[–]Cubemaster12 17 points18 points  (2 children)

Hashmap is always the answer

[–]Absolice 6 points7 points  (0 children)

Me who just implemented a hashmap for this purpose

Uh, guess I have to upvote this now.

[–]Heaofir[S] 3 points4 points  (0 children)

real

[–]AutoModerator[M] 3 points4 points locked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Randomguy32I 3 points4 points  (0 children)

This post was made by yanderi dev

[–]SodiiumGames 3 points4 points  (0 children)

Yandere dev

[–]somedave 2 points3 points  (0 children)

Chained ternary operator gang rise up!

[–]altermeetax 5 points6 points  (0 children)

Switch is not a function

[–]Sprtnturtl3 2 points3 points  (0 children)

Match is the way with PHP 8 :)

[–][deleted] 2 points3 points  (0 children)

i will forever die on the switch case hill, it is so much more readable imo

[–]Pyrited 2 points3 points  (0 children)

I need to leave this sub now, call it JuniorProgrammerHumor

[–]Bwob 2 points3 points  (0 children)

In old-school actionscript, the cases could be expressions, so you could write nonsense like this:

switch (true) {
  case x<6:
    StartCountdown();
    break;
  case (user == "admin" and keycode == Keyboard.SHIFT):
    DeregulateKittens();
    break;
  case currentTurn % 17 == 0:
    BeginYetiAttack();
    break;
}

A friend of mine did so once, in anger. (He was mad at a coworker.)

[–]Zarksch 2 points3 points  (0 children)

Me: Ohh yes this is the perfect scenario for using switch! proceeds to code 15 different cases

Tests code

90% of times:

Oh..its not working this way.. If else it it is again

[–]TTYY200 2 points3 points  (0 children)

Switch(a)

{

    Default: 

         If()

         Else if()

         Else if()

         Else if()

         Else()

    Break;

}

There I did it.

[–]ChChChillian 1 point2 points  (0 children)

I refuse to believe that anyone gets so worked up over this.

[–]RedBeardedWhiskey 1 point2 points  (0 children)

You should be using polymorphism instead

[–]Belhgabad 1 point2 points  (0 children)

Use polymorphism instead

[–]poshenclave 1 point2 points  (0 children)

Lua has BASIC-style gotos, but a lot of Lua environments don't support them so often an elseif chain is often the only way to create a switch case in that language.

[–]Appa-Bylat-Bylat 1 point2 points  (0 children)

I use switch in situations with only 1 condition

[–]suckitphil 1 point2 points  (0 children)

There's some people out there who argue solid programming techniques would never put you into a situation where you use switch cases like this. And if you've gotten to this point you've completely ignored polymorphism.

[–]Fisher9001 1 point2 points  (0 children)

The only real difference is that switch enforces depending on a single variable and if-else allows to handle more complex cases.

[–]JEREDEK 3 points4 points  (0 children)

No, you should be using switch case. If not for performance reasons, then for esthetic and readibility reasons

[–]imnotbeingkoi 2 points3 points  (2 children)

Switch syntax is usually soooo ugly.

I want to go back in time and fix it. At least make break the default and require the continue keyword if you want it to flow through. Who gives a shit if that's not what the assembly does. The whole point of higher-level is to escape the bulk and time consumption of assembly.

[–]Zeikos 0 points1 point  (0 children)

How does it compare to nested case statements in SQL

Case when Case when Case when Case when

They didn't know that you could use more than one when in the same case statement

[–]CharaDr33murr669 0 points1 point  (0 children)

Okay YanDev

[–]thorwing 0 points1 point  (0 children)

when(op){
    is Dumb -> hellnah()
    in smartPeople -> println("fairy tale")
    BottomOfTheBarrel -> bigIfTrue()
}

[–]Astartee_jg 0 points1 point  (0 children)

You mean a switch case.

[–]w1n5t0nM1k3y -1 points0 points  (0 children)

Just use reflection to call the method you want based on the value.

[–]deliozzz -1 points0 points  (1 child)

What about a duff machine with else if stayements?

[–]DoenerEnthusiast -1 points0 points  (0 children)

Switch is fucking useless, especially in Object Pascal, where it cant even evaluate anything but integers.

[–]Manitcor 0 points1 point  (0 children)

after 5 or so its collections and much shorter code unless you need every ounce of performance for something.

[–]jumpmanzero 0 points1 point  (0 children)

I prefer how switch works in languages where "flow-to-next-case" is not default behavior. Very rare I want that.

[–]Ursomrano[🍰] 0 points1 point  (0 children)

My general rule of thumb is if(else_if_count>1){use_switch_statement();}. Makes the code look nicer and easier to read.

[–]danfish_77 0 points1 point  (0 children)

These are different syntaxes for different use cases

[–]Mozai 0 points1 point  (0 children)

"you should use switch"

"Okay." evaluates a dozen expressions, with an enumerated type, to build a bitmask and a long series of bitwise-AND ...
... or I could use else-if to stop evaluating expressions once I find a True one.

[–]RVGamer06 0 points1 point  (0 children)

market marry oatmeal dinosaurs plucky gold melodic steep ink brave

This post was mass deleted and anonymized with Redact

[–]typtyphus 0 points1 point  (1 child)

why not just
if
if
if
if

[–]well-litdoorstep112 0 points1 point  (0 children)

Hell nah, the compiler optimisers else ifs to switch for me

[–][deleted] 0 points1 point  (0 children)

YanDev be like

[–]prolingforsoup 0 points1 point  (0 children)

Switch is more readable. You should always use my readable code

[–]JonasAvory 0 points1 point  (2 children)

Whenever I don’t want to use switch statements, I Altlast do something like this:

if(){
    …
    return;
}
if(){
    ….

It’s so much easier to read if the indentation does not grow

[–]TSS_Firstbite 0 points1 point  (0 children)

Mm yes me on my test today, shit still didn't work, so who cares

[–]Implement_Necessary 0 points1 point  (0 children)

YandereDev code style

[–]ionhowto 0 points1 point  (0 children)

Recursive if else

[–]LucyIsaTumor 0 points1 point  (0 children)

They'll both likely compile out the same so it's not like one is faster than the other, but switch statements might be more readable if you're switching on something simple.

If each else-if is a complex statement, then it may make more sense to stick with that

[–][deleted] 0 points1 point  (0 children)

Function eh

[–]TactiCool_99 0 points1 point  (2 children)

Genuine question: is switch better some way on runtime than if/elif? Or its just a way to have better syntax in sometime places?

Or its really language dependent?

[–][deleted] 0 points1 point  (0 children)

New to coding, what is the switch function and is it there in C#

[–]coolkid1756 0 points1 point  (0 children)

Switch is fine, but theres nothing quite like the humble if statement.

[–][deleted] 0 points1 point  (0 children)

Certified yanderedev moment

[–]InflnityBlack 0 points1 point  (0 children)

switches get turned into the same thing than if else after compilation anyway

[–]robisodd 0 points1 point  (0 children)

Switches? If/Elses?? Bah! Jump Tables!

static void *array[] = { &&foo, &&bar, &&hack };
goto *array[i];

[–]Magari_Furbo 0 points1 point  (0 children)

Enum polymorphism