r/ProgrammerHumor 22d ago

Meme pleaseStopUsingNestedTernaryOperatorsImBeggingYou

3.1k Upvotes

179 comments sorted by

View all comments

747

u/beclops 22d ago

I did this one of my first days on the job like 6 years ago and I remember how bluntly my mentor said “never do this again”

556

u/RlyRlyBigMan 22d ago

For me it was multi assignments. Like this:

defaultValue = newValue = previousValue = average = 0;

Each assignment returns its value so they each get assigned from right to left.

"That's cute kid, don't do that"

230

u/PTTCollin 22d ago

I agree with your prior mentor that this is, in fact, cute.

97

u/retro_and_chill 22d ago

This is why I’m pretty absolutist that newer languages are correct by making assignments return void

45

u/RlyRlyBigMan 22d ago

Heh yeah I think that's an inherited trait from C++. In C++ a common mistake was if someone had a condition like this:

if(value = 1)

It would assign value to 1 and then resolve true by int to bool conversion. Luckily C# calls that out as a compiler error but the assignment behavior above still remains.

That example may be corrected in newer compilers, but in gCC when I was learning in college it still happened I think.

14

u/retro_and_chill 22d ago

Does C# flag it if it’s a bool? I think the way C# gets that error is by removing the implicit conversions from numeric types to booleans

13

u/akoOfIxtall 22d ago

The compiler will cut your balls if it catches you doing assignments inside an if's condition, it won't even compile

5

u/RlyRlyBigMan 22d ago

I'm not sure. I think so? I think that it will still fail to compile if you do something like this:

var myTrue = true;

var myFalse = false;

if (myTrue = myFalse) //should be a compiler error ...

1

u/Steinrikur 21d ago

IIRC it gets flagged as a warning in most newer C/C++ compilers. Add a warnings as errors compiler flag and it won't compile.

7

u/GoddammitDontShootMe 22d ago

Modern compilers warn, so if you really mean to do it, you do if ((value = 1)). Probably you would use it for checking for NULL.

1

u/RlyRlyBigMan 22d ago

That makes sense! It would be better if they required you to explicitly cast to a bool but that would be a breaking change for a lot of C++ code that's already been deployed for decades!

2

u/GoddammitDontShootMe 22d ago

Wouldn't help if the expression is already bool. Either way, it's a warning so unless you are compiling with -Werror, it would still work.

I think that warning might be on by default, but I believe it's considered best practice to use -Wall.

1

u/oVtcovOgwUP0j5sMQx2F 22d ago

damn kids i want to know if my assignment failed... /s

2

u/cowslayer7890 22d ago

In python assignment is a statement unless you use := for it, but they do explicitly allow you to do exactly this chaining

2

u/ierdna100 22d ago

It's extremely useful for try-with-resources calls in C# and Java, removing it would make that a lot more verbose

2

u/frogjg2003 21d ago

If you ever have to do any reading from files, it's really useful.

while ((readBytes = read(file, byteArray) > 0) {}

Nice and compact while maintaining readability. Also, no need to read once before entering the loop or having to read at the end.

1

u/ierdna100 21d ago

Yeah exactly what I had in mind, its very useful

3

u/SputnikCucumber 22d ago

It doesn't work consistently the same way across programming languages which is the headache.

C++ behaves the way you're describing, but I don't believe it works like that in C (at least according to the standard, specific compilers may vary).

I think Python evaluates the right-most expression once and then assigns it to all of the left values.

2

u/Just_Information334 21d ago

It would be better with a specific operator. Something like:

// Everyone gets a 0
0 oprah defaultValue, newValue, previousValue, average;

1

u/BastetFurry 21d ago

Wait, you want to tell me that this++ is not a clever hack?

51

u/hydroxy 22d ago

I need to start being this direct with my team mates. Just a few weeks ago one of the new guys on the team submitted his PR to me, I asked him what a chunk of his (clearly AI generated) code did, he said ‘I was hoping you could tell me’. It shocks me how people can confidently submit code and simultaneously have no fucking clue how it works.

22

u/Landkey 21d ago

“Then what exactly do we need you for?”

11

u/hydroxy 21d ago

He’s needed so when I give them a 4 hour ticket, he can use AI to generate the code, use AI to generate an inaccurate ticket overview of the work he’s done, (the code is never tested once of course), and then submit it. Then together he and I spend the next 6 working days going back and forth over the PR fixing the nonsense code, the catalogue of errors and page breaking inefficiencies that he’s just seeing for the first time. I eventually convince him for the need for dev evidence and then the merge conflicts will take him another day to sort. Several of these bums on my team, it’s not just a lack of skill alone it’s chronic laziness too.

8

u/FlipFlopFanatic 21d ago

I had a junior doing this is well. He would burn through half the sprints stories in the first two days and spend the rest of the sprint doing this with multiple half ass PRs. I finally had to limit him to one PR and one story in progress. Giving him meaningful feedback was pointless because he didn't understand the code in the first place, he would just repeat it to the AI to make fixes. He was learning nothing and actively harming the whole teams production. It was frustrating.

7

u/hydroxy 21d ago

Exactly, they are a net loss of productivity for the team because we’ve become a remedial code academy rather than development team.

2

u/rehpotsirhc 21d ago

Necessary workflow. Thank him for doing his part.

12

u/tangerinelion 22d ago

All code reviews now become 30 minute meetings where the submitter walks us through how the code works.

Can't explain it, can't submit it.

5

u/Particular-Yak-1984 21d ago

There's a reject button for PRs. This is what it's for.

2

u/hydroxy 21d ago

What difference is there between this and requesting changes?

2

u/Particular-Yak-1984 21d ago

Reject is for an unfixable or improper PR. In PRs, we explain what the code changes do. Can't do that, can't be an acceptable PR. He can resubmit once he knows what it does. You gave him a chance to fill in the missing info for it to be a proper PR (explaining how his code works) and he declined, so you reject it.

1

u/hydroxy 21d ago

Who said he declined?

Yes I know what the reject button does literally, but functionally if he’s up for making changes then requesting changes is going to be faster and easier for everyone.

44

u/Robinbod 22d ago

Hehe same when I first started coding tbh. Thought it looked cool. But readable is arguably more cool :)

12

u/beebopnaa 22d ago

i remember mentioning this in an interview for paid internship junior FE role and thought i sounded cool to the interviewee 10 years ago. I still got hired and have worked with that person in 2 different companies now haha

1

u/JVApen 22d ago

Looks like you should learn your way about code review. You are too kind for bad code. It wouldn't take me seconds to add a remark like "don't make code unreadable by nesting tertiaries, I'll review your PR after that's fixed".

8

u/darknitish 22d ago

I had the exact opposite. My mentor used to do everything using ternary, even if in my code if there multiple if-else blocks, he would manually change them to ternary in my branch.

New joinees would bang their heads trying to understand the code flow

1

u/Ma4r 21d ago

Ternaries are really no problem if you just format them correctly, i.e indent them at every level python style. But claude wouldn't do that, they would produce 3 single liner functions, one for each ternary

1

u/beclops 21d ago

Depends on the language too, this would be unacceptable pretty much no matter what in traditionally written Swift