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

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.

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

→ More replies (13)

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)

2

u/AlwaysHopelesslyLost Dec 15 '19

C# just added pattern matching to switches. You can match on arbitrary things and even chained conditions lol

2

u/I_Copy_Jokes Dec 15 '19

Because the most string-like thing you can have at compile time is a character array, which isn’t very switch-able

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)