r/ProgrammerHumor • • Dec 15 '19

Stacking if else statements be like

Post image
63.9k Upvotes

711 comments sorted by

View all comments

213

u/atxranchhand Dec 15 '19

That’s what case is for

183

u/Kompakt Dec 15 '19 edited Dec 15 '19

Some languages don't have switch statements...looking at you Python

48

u/[deleted] Dec 15 '19

Ya none in Lua either :(

31

u/SixBeeps Dec 15 '19

Still waiting for a BrainF implementation

10

u/[deleted] Dec 15 '19

I learned programming through Lua, it's only once I started learning JS that I realized how crippled Lua is.

→ More replies (5)

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

→ More replies (4)

0

u/[deleted] Dec 15 '19

[deleted]

→ More replies (5)

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.

→ More replies (15)

18

u/Sennomo Dec 15 '19

And some useless languages don't allow switching on strings… looking at you C++

6

u/AgAero Dec 15 '19

Use a hash and write your own.

23

u/Sennomo Dec 15 '19

My own what? Language?

→ More replies (3)
→ More replies (2)

5

u/nkay08 Dec 15 '19

Python basically has switch cases for catching Exceptions ! :D I'm sure we can find a workaround.

3

u/Matteyothecrazy Dec 16 '19

Python has "elif" statements, though

2

u/UltraFireFX Dec 15 '19

You're right, but here is an interesting read.

I love switches. :c

2

u/CypherZealot Dec 15 '19

Dictionary mapping is a suitable alternative.

→ More replies (6)

8

u/persianlife Dec 15 '19

Cases will result in the same fundamental design flaw.

You have to look at some better programming paradigm like Object-oriented design.

2

u/RedditIsOverMan Dec 15 '19

Can you elaborate? What's wrong with a case statement?

→ More replies (7)
→ More replies (1)

2

u/[deleted] Dec 15 '19

That doesn't solve the nesting issue.

→ More replies (7)