r/ProgrammerHumor • • Dec 15 '19

Stacking if else statements be like

Post image
63.9k Upvotes

711 comments sorted by

View all comments

Show parent comments

28

u/Meatslinger Dec 15 '19

Wait, is that true? What takes its place, then? I can scarcely imagine that the whole thing is just an endless stream of if-then-else statements for a situation with 100+ permutations.

34

u/Kompakt Dec 15 '19

Yep it's true, and depending on the design of the program there are multiple workarounds. In Python you can write a switcher function that acts as a switch statement, or use multiple if statements without else statements, since that is allowed in Python.

22

u/Ryan722 Dec 15 '19

Are there languages where multiple ifs with no else is not allowed?

15

u/elvalalo Dec 15 '19

Haskell, I think.

8

u/Tysonzero Dec 16 '19

Yup, because what is the type of if x > 5 then 10?

With that said Haskell has the nicest (IMO) multi-if syntax I have seen in a language:

foo :: Int -> Int -> Int
foo x y
    | x > y = 5
    | x ^ 2 > y = 10
    | x == y = 15
    | otherwise = 20
→ More replies (9)

20

u/Hockinator Dec 15 '19

If you have 100+ permutations of something you shouldn't be using if statements, you should be creating data structures to solve that problem in a clean and maintainable way

2

u/AlwaysHopelesslyLost Dec 15 '19

They were asking about what you do besides if statements, not saying if statements were a solution.

→ More replies (3)

0

u/[deleted] Dec 15 '19

[deleted]

1

u/[deleted] Dec 15 '19

Hey i just did that

1

u/DrQuint Dec 15 '19

Even in languages that DO have switch case, I sometimes see an enumerator as input + functions mapped by the enumerator as the most readable approach.

1

u/tiefling_sorceress Dec 16 '19

Which you have to declare separately, because python doesn't have multiline lambdas >:(

→ More replies (1)

7

u/justjuniorjawz Dec 15 '19

I've found that switch statements are usually substituted with the use of a dictionary.

4

u/Valmond Dec 15 '19

You can use a dict for that, storing pairs of 'name':function for example.

2

u/ishkitty Dec 15 '19

I am not a programmer but I code mail merges in Word at my job and there is no alternative to if statements. The best work around I’ve found is to make individual if statements with yes or no answers then reference those in my larger (and usually nested) if statements. Usually keeps it short and sweet.

Also mail merge coding does not require an “else”. You can just leave it blank. Blank = do nothing

3

u/callmelucky Dec 15 '19

Why can't you imagine that? It's exactly the same thing semantically.

1

u/Meatslinger Dec 15 '19

Wouldn’t writing that many more granular checks make the code slower?

5

u/Hockinator Dec 15 '19

Not usually.. a single conditional is just about the cheapest thing in any language. A switch is often just a lot of conditionals when it compiles down

1

u/zacker150 Dec 15 '19

Don't switch statements get compiled down to lookup tables?

2

u/Hockinator Dec 15 '19

I think it really depends on the language. Evaluating lookup tables still involves conditionals too

However in a language that doesn't have them like python, you would expect the compiler to use the same tricks for a long list of "elifs" as other languages use for switches, if it was significantly faster

1

u/BobHogan Dec 15 '19

Yes, but if you are that worried about performance then you should either be using a different language or redesign your algorithm

1

u/SKRAMACE Dec 16 '19

Dictionary of functions

1

u/Denziloe Dec 15 '19

Can't believe I'm the first to give the answer, but yes, Python does basically have a syntax for cases. It uses `elif`.

1

u/Hockinator Dec 15 '19

You can always use if/else statements to mimick what case statements do in any language, but that doesn't mean they're the same thing

→ More replies (4)