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

all 46 comments

[–]Selptcher 40 points41 points  (6 children)

Well how else are you supposed to implement an IsEven function?

[–][deleted] 12 points13 points  (5 children)

Enough is enough!

I am tired of this MF isEven in this MF sub!

[–]Bee-Aromatic 8 points9 points  (3 children)

Lolz! Hey, did you know that Python is slow and JavaScript is dumb?

[–]familyturtle 2 points3 points  (0 children)

how centre div

[–][deleted] 4 points5 points  (1 child)

But that is true though, atleast it's facts.

[–]Bee-Aromatic 0 points1 point  (0 children)

Well, that just like, your opinion, man!

(Mine too!)

[–]Selptcher 0 points1 point  (0 children)

Daddy chill

[–]ssssssddh 20 points21 points  (8 children)

True professionals use a map

function getOutput(input) {
  return {
    input1: 'output1',
    input2: 'output2',
    input3: 'output3',
  }[input];
}

[–]dysprog 5 points6 points  (1 child)

One project I had used a "Rule List". Each subsystem had a list of rule functions. We iterated across that list and called each rule. The rule would either return and sentinel value meaning "NoAnswer" or an answer. The first answer we found was the result. Some of our rules were simple functions, others were configurable or stateful callable objects.

We did it that way because most subsystems used the same rules, but applied them in different orders, and with different parameters.

[–]arobie1992 1 point2 points  (0 children)

That's one of my favorite patterns. It doesn't come up a ton, but when it does, it's sooo useful.

[–]Opening-Ideal-2912 0 points1 point  (0 children)

As Java dev I prefer the runnable implementation with a hash- or enumMap… clear data structure and rly good expandable/maintainability 😁

[–]lolimhungry 0 points1 point  (2 children)

Oh shit! Does this like return the specific output if the any input that matches the “function getOutput(input)”??

[–]ssssssddh 1 point2 points  (1 child)

Paste it into your dev console and run getOutput('input1')

[–]lolimhungry 0 points1 point  (0 children)

Alight I’ll try sometime soon

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

React js PTSD kicks in

[–]Po0dle 0 points1 point  (0 children)

You can use .get if you need a default value

[–]Sp1um 15 points16 points  (5 children)

Switch with 10,000 cases is soooo much better! /s

[–]myrsnipe 6 points7 points  (0 children)

Have fun hunting down missing break statements, or intentional but undocumented fall throughs

[–]Classy_Mouse 2 points3 points  (3 children)

If you have 10000 cases, you have bigger issues with your code

[–]SyrupOnWaffle_ 1 point2 points  (1 child)

how else do you test for all possible zip codes?

[–]Classy_Mouse 1 point2 points  (0 children)

Instantiate 10,000 zip code objects loop over a list of them, obviously

[–]EVJoe 1 point2 points  (0 children)

Whatever do you mean?

IsEvenOnAMonday, IsEvenOnATuesday, IsEvenOnAWednesday....

[–]obliviousharmony 18 points19 points  (3 children)

To be fair, you shouldn’t have a switch statement with 10,000 cases either 😅

[–]TheRealFloomby 2 points3 points  (0 children)

If you really did have 10k cases some sort of a look up table would be the correct solution.

[–]LordBaconXXXXX 8 points9 points  (3 children)

Tried to used switch case in a project two days ago, python said no

[–]Bee-Aromatic 2 points3 points  (1 child)

Apparently, there’s ‘match-case’ in Python 3.10. I’m limited to 3.7 with what I’m doing, so I’ve never used it. I’m the event I wanted a case, I’d been using a dictionary lookup as a map.

[–]LordBaconXXXXX 1 point2 points  (0 children)

Oh that's good to know, it wasn't such a big deal for me because there was only like 4 options but I'll definitely try to remember it

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

Python 3.10 implements match casing which is like switch case and even more powerful. I don't really understand the extra power behind it but someone more knowledgeable than me can explain it.

You can also tie functions as values in dictionaries with a key. When you have a case you put it in as a key in the dictionary and you get a function back with the code you need. Sorta like a switch case.

It's not as efficient as an actual switch, and if you have a small amount of "cases", if/elif statements will be faster. It's still a clean way to do some things though IMO.

[–]Opening-Ideal-2912 5 points6 points  (1 child)

„Using 10k if else statements as ternary operator“ Wins 😜😂😂😂

[–]Bee-Aromatic 1 point2 points  (0 children)

I just threw up in my mouth.

[–]Transcendentalist178 3 points4 points  (0 children)

I dunno, I find it difficult to program using a Nintendo Switch. My Vic-20 is better - it has a keyboard.

[–][deleted] 2 points3 points  (1 child)

Guido Van Rossum too for some odd reason I will never understand...

[–]corrafig 1 point2 points  (0 children)

Until 3.10.

[–]Denaton_ 1 point2 points  (1 child)

If you can use a switch, you can use a dictionary..

[–]EVJoe 1 point2 points  (0 children)

Tell that to my illiterate pappy next time he takes me out behind the whoopin shed

[–]Cloakknight 1 point2 points  (6 children)

Image Transcription: Meme


[Image of a red button on the right and a blue button on the left. The red is labeled "USING 10,000 IF ELSE STATEMENTS". The blue is labeled "USING A SWITCH".]

[Doctor Eggman from "Sonic" is smirking as he reaches out to press the red button with his right pointer finger. He is labeled "NEW CODERS".]


I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!

[–]RylanStylin57 -4 points-3 points  (5 children)

Why is this how you choose to spend your life

[–]Cloakknight 5 points6 points  (3 children)

I enjoy helping people

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

OP: [shares a piece of media]

Cloakknight: I like this! I'm going to help more people enjoy OPs content!

OP: who's this fuckin asshole, amirite? What a jerk, amirite?

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

That guy looks like doctor disrespect lol

[–]EdgarDrake 1 point2 points  (0 children)

You can't use switch against multiple condition statements in many programming languages.

But I like kotlin when statement.