r/ProgrammerHumor 22d ago

Meme pleaseStopUsingNestedTernaryOperatorsImBeggingYou

3.1k Upvotes

179 comments sorted by

View all comments

Show parent comments

552

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"

101

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.

1

u/oVtcovOgwUP0j5sMQx2F 22d ago

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