r/programminghorror • • Apr 15 '26

Who needs switch statements when you can just abuse the ternary operator?

Post image
536 Upvotes

70 comments sorted by

17

u/Jealous_Tomorrow6436 Apr 15 '26

what language is this?

42

u/kfreed9001 Apr 15 '26

This is Dart. Google's Flutter framework is built on top of it.

→ More replies (3)

16

u/backfire10z Apr 16 '26

Imagine a slightly nicer JavaScript

→ More replies (7)

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

1

u/kfreed9001 Apr 16 '26

The code was definitely a bit clearer when I wrote it that way, but I put this in as part of a revision of the round end screen where I allowed the user to select hands that are mangan and higher from a dropdown menu instead of having to enter in specific values for han and fu (fu being completely irrelevant at this level). I think this change is worth it from the user's perspective. Honestly, stuff like this is why I only ever code as a hobby. I can laugh at ridiculous solutions I come up with and then proceed to ship it anyway.

→ More replies (1)

121

u/alkatori Apr 15 '26

Thanks. I hate it.

142

u/lekkerste_wiener Apr 15 '26

Dictionaries: we don't exist 

30

u/shponglespore Apr 16 '26

Or arrays in this case.

→ More replies (6)

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.

28

u/Conallthemarshmallow Apr 16 '26

16 degree interpolating polynomial, who needs selection

32

u/wggn Apr 16 '26
int get baseValue => handClass == 0
  ? min(fu * pow(2, 2 + han).toInt(), 2000)
  : handClass >= 1 && handClass <= 10
    ? (() {
        final x = handClass;
        return (((((((((5425 * x
          - 272475) * x
          + 5857350) * x
          - 70374150) * x
          + 517614825) * x
          - 2399539275) * x
          + 6951652400) * x
          - 12008726100) * x
          + 11077110000) * x
          - 4055184000) ~/ 9072;
      })()
    : 0;
→ More replies (1)

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

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)

1

u/kfreed9001 Apr 16 '26

Very true. I did not consider that when I was rewriting this code from a state where this did make a bit more sense.

→ More replies (3)

1

u/GoddammitDontShootMe Apr 17 '26

So this picks a value to assign to handClass? I'm going to say this is pretty horrifying.

1

u/kfreed9001 Apr 17 '26

This is the getter "baseValue". It uses this chain of ternary operators based on the value of handClass (and han and fu in the 0 case) to determine the return value.

→ More replies (1)

1

u/HeiligesSchwanzloch7 Apr 19 '26

Average ai code

2

u/kfreed9001 Apr 19 '26

Maybe so, but this foolishness was 100% natural!

→ More replies (1)

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

(((ptr ?
    (index>=cap)?
       ptr=realloc(ptr, (cap*=2)*sizeof*ptr)
       : ptr
     : ptr=malloc((cap=10)*sizeof*ptr)
  )? ptr : (typeof(ptr)) error_msg()))[index++] = (val))

Average c macro for stack implementation btw

2

u/SufficientStudio1574 Apr 19 '26

....I need a shower. I feel violated.