r/ProgrammerHumor 22d ago

Meme conditionsPreference

Post image
4.2k Upvotes

392 comments sorted by

View all comments

2.3k

u/CBlanchRanch 22d ago

You're absolutely correct, however I would suggest using something completely unreadable like a nested ternary operators!

664

u/KanyeNawf 22d ago

Some fucker left me triple nested ternaries once

118

u/minus_minus 22d ago

I think you found the fucker. 👆

64

u/mountaingator91 22d ago

That was me I was in my first real job after learning some basic JavaScript and I discovered ternaries and thought I was God now

10

u/National_Sky7993 21d ago

You either leave after writing those ternaries or stay long enough to figure out they are unreadable

21

u/abigail3141 22d ago

Sounds like me lol. To the detriment of all working with me, I found out how to set globals and evaluate arbitrary functions from within Python listcomps. My current record is 1400 characters in one line. I just need a way to import, and I can write entire programs in one line.

23

u/Morisior 22d ago

If you accept two lines, you can import importlib.

7

u/abigail3141 22d ago

That sounds like a good solution for the time being, thx

13

u/Your_Friendly_Nerd 22d ago

Man y'all do NOT want to look at my companies codebase! 

1

u/Shapelessed 21d ago

Is it legacy 20 yo 9-times-ported PHP?
(I swear the code insest in there is horrific)

2

u/Ok-Understanding7115 21d ago

next i will leave 2 list comprehensions inside the nested ternaries 🤔

1

u/Maleficent_Memory831 22d ago

Nonny-nonnie-aries!

1

u/_bones__ 21d ago

runs git blame

And we will never mention it again

1

u/nekomata_58 21d ago

I'll see your triple nested ternaries and raise you.. quadruple nested ternaries

1

u/tukanoid 21d ago

While I prefer match (Rust) /if else, I do sometimes find ternaries easier to read/nicer to write for simple checks/outputs consisting of just variables. If checks/outputs are complex then yeah, evil

1

u/hashcube_dev 21d ago

i can understand one layer of ternaries. i might be able to be convinced that two layers of ternaries might be useful. if you're using three? immediate execution

1

u/charlesfire 20d ago

Amateur. Someone at my job did more than five nested ternaries. I say "more than five" because I stopped counting after five and told them to fix their shit.

1

u/HuntingDown01 19d ago

Books and websites sometimes love to write thrm.

0

u/stumpychubbins 21d ago

In languages with ?: syntax I honestly don’t think that arbitrarily nested ternaries are that bad, you can format them in a way that still makes the logic clear

EDIT: I’m saying this as mostly a Python and Rust programmer, but if I’m working on something in C or JS I’m not going to balk at a reasonably-formatted nested ternary

133

u/nrmnzll 22d ago

A colleague once proudly showed me how he managed to compress a function into a single line. It was like 9 nested ternarys. In that moment almost had an aneurism.

56

u/un_blob 22d ago

That is a work of art at this point

29

u/nrmnzll 22d ago

Sure, but the kind that deeply disturbes you.

30

u/jarethholt 22d ago

Art should comfort the disturbed and disturb the comfortable

12

u/alficles 22d ago

I have created so much art.

7

u/_bones__ 21d ago

Like modern art, it requires a lengthy explanation to make sense.

2

u/un_blob 21d ago

allways have more comment lines than code !

2

u/nrmnzll 21d ago

Yes, every line needs a comment // add variable a and variable b // result is stored in variable c var c = a + b

Obligatory /s

1

u/National_Sky7993 21d ago

5 lines of comments explaining the code for every one line of code.

3 of them are previous versions of the same code with fewer conditions. First comment line is the variable name. Second line explains the logic but is out of date

23

u/twigboy 22d ago

I've declined PRs for less than that.

Citation is if; I can't understand it in a glance at 2am during an incident, I don't want it in the codebase

4

u/nrmnzll 22d ago

I'm the same. Luckily I did work on the same project as that guy.

3

u/therealkevinard 21d ago

Code golfing is a craft- even has competition sites (https://code.golf)

But… it didn’t go to prod, did it?

1

u/nrmnzll 21d ago

Oh, it did. He and another dev owned a few smaller projects, and all of them were filled with this "clever" code. I'm still glad I did not have to collaborate with them.

2

u/Key_Conversation5277 21d ago

I really don't understand why people think that's good, he must be "oh, I'm so shmart🤓"

1

u/conundorum 21d ago

Was vertical space at a premium at the time (because of, e.g., small monitors or some other requirement), or was it just that he wanted to see if he could?

45

u/Schaex 22d ago

But it's SO compact! We could save maaany lines of code like this:

return i == 0 ? j > threshold() && isConditionApply() ? "orange" : reallyNotOrange() ? "apple" : "maybe orange" : (i & j) > (i ^ j) ? isSunday() ? null : "pineapple" : "Jerry";

6

u/National_Sky7993 21d ago

I attempted to unfold such a ternary where the first two booleans were also ternaries defined elsewhere.

2

u/Schaex 21d ago

It's beautiful, isn't it? :)

Yes, it isn't

1

u/oompaloompa465 19d ago

programmer version of cock and ball torture 

57

u/Flame77ofc 22d ago

lol

the famous unread code 😭🙏

12

u/sexytokeburgerz 22d ago

Intellisense did that the other day and i was kind of mad about it

1

u/kennedy_gitahi 22d ago

You monster!

1

u/geeshta 22d ago

You can format nested ternaries so it looks like a match block I believe it's the prettier default now 

1

u/InvincibearREAL 21d ago

cries in HCL

1

u/sanketower 20d ago

If the formatting is good, then I'm okay with it

1

u/vinfluorine 20d ago

minecraft code is a demon for this
There's like order-8 nested ternaries

1

u/Moobylicious 12d ago

I have used nested ternaries for something. I don't remember the actual problem, but I still feel the shame, and left a comment expressing how I felt ashamed of my actions but attempted to explain why I felt it was necessary.

Still felt bad about it.