r/programminghorror • u/kfreed9001 • Apr 15 '26
Who needs switch statements when you can just abuse the ternary operator?
535
Upvotes
r/programminghorror • u/kfreed9001 • Apr 15 '26
17
u/Jealous_Tomorrow6436 Apr 15 '26
what language is this?
4
u/brh131 Apr 16 '26
This is a riichi mahjong scorer right? Then why bother with this extra handClass variable? A switch statement with han<5, han==5, han<=7, etc. would easier to understand (and a comment for each hand type "mangan", "haneman", etc. would help)
Nvm fuck it, LGTM
121
u/alkatori Apr 15 '26
Thanks. I hate it.
142
u/lekkerste_wiener Apr 15 '26
Dictionaries: we don't exist
47
u/realmauer01 Apr 15 '26
Why even a switch, every finite amount of numbers can be connected via a mathematical function. So just do that.
9
u/NeverYelling Apr 16 '26
I don't hate this as much as I should. THe line breaks make it quite readable, and it should do what it's supposed to do, but sure, it's hella unconventional und unnecessary
6
u/dweomer5 Apr 15 '26
LGTM
2
u/uvero Apr 16 '26
Style-wise, the problem here is not the ternary instead of switch (although a switch will be theoretically faster here), the problem here is you're should be using a dictionary (or an array)
5
u/trutheality Apr 15 '26
So pretty
1
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Apr 17 '26
So this picks a value to assign to handClass? I'm going to say this is pretty horrifying.
1
u/HeiligesSchwanzloch7 Apr 19 '26
Average ai code
2
u/Nixinova Apr 16 '26
Shit like this is not awful in some languages (JavaScript) because of a lack of expression switch statement.
2
u/Agitated-Display6382 Apr 16 '26
A switch keeps the cyclomatic complexity still too high: better use a dictionary or list or similar
2
u/un_virus_SDF Apr 16 '26
did you never
Average c macro for stack implementation btw
2
u/SufficientStudio1574 Apr 19 '26
....I need a shower. I feel violated.
3
u/captnkrunch Apr 15 '26
Id punch whatever dev pushed any edit after 3 in the dick repeatedly til they fixed it
1
u/Berinchtein3663 Apr 16 '26
Holy hell this is rough
1
u/LeaveMickeyOutOfThis Apr 16 '26
All it needs is correct indenting to be worthy
1
u/Ok_Chemistry_6387 Apr 16 '26
I like how the answer is questioned.
1
u/Lines25 Apr 16 '26
Yk that u can jst use math?
1
u/Top_Dragonfruit_1020 Apr 16 '26
I hate this.
1
u/PlaceReporter99 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Apr 16 '26
does list indexing exist?
1
u/randomInterest92 Apr 16 '26
The people that write this code then complain about leetcode being useless
1
u/Kinrany Apr 16 '26
This is better than a switch once you spend a few seconds to understand the pattern.
1
u/MrFartyBottom Apr 16 '26
I don't have a problem with multiline ternaries but I do have a problem with their formatting. Install Prettier and let it format it for you for much better readability.
1
u/AdSeveral5047 Apr 16 '26
Can relate, I won't use switch cases even if my life depended on it
1
u/Xandaros Apr 17 '26
That... is surprisingly readable. I hate it out of principle, but well done making that work... I guess?
Also, what a crossover. Now I'm tempted to open jantama and play a hanchan lol
1
u/NamedBird Apr 17 '26
I think it's actually quite readable?
Perhaps have the result on the same line as the boolean logic so each line forms it's own case?
1
u/DefinitionPhysical46 Apr 17 '26
Switches are like training wheels /s
1
u/Blockque Apr 19 '26
Abuse truly is the right word here 😭
1
u/AttitudeElectronic68 Apr 19 '26
Use an array of function pointers
1
u/TehBrian [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Apr 16 '26
i hate ternary rahhhhhh ternary is just poor man's if expression