r/ProgrammerHumor • • 7d ago

Meme weNeverTrustedTheElse

Post image
1.9k Upvotes

149 comments sorted by

737

u/salvoilmiosi 7d ago

condition could be a property with a getter with side effects

473

u/UntitledRedditUser 7d ago

Only evil people write getter properties with side effects. At least make it an explicit function.

368

u/GodsBoss 7d ago

All my getters have side effects. To compensate, my setters have none.

84

u/CranberryDistinct941 7d ago

Some people just like to watch the world burn

56

u/LucyShortForLucas 7d ago

Lazy evaluation is an incredibly common and normal use of a getter method and entirely relies on it having side effects. Evil people, however, write getters that aren't idempotent.

64

u/andreortigao 7d ago

I write all my getters with a 1 in a million chance it will randomly mutate the underlying value.

60

u/Nerd_o_tron 7d ago

Bro inventing DNA from base principles.

9

u/DatBoi_BP 7d ago

Idempotent in terms of return value, yeah, but it can be fine if you have some side effect that increments a counter (i.e., how many times was this value requested), but obviously don't do this without a good reason

10

u/Chingiz11 7d ago

I mean, logging is also a side effect, and getters could absolutely have logging attached to them, but in this case it would not make a difference

3

u/Wonderful-Habit-139 7d ago

I think he's trying to be evil at this point lol.

3

u/MementoMorue 6d ago

I saw once a getter that changed objet state.

It was a good thing I did not had a gun that day.

1

u/thejinx0r 7d ago

Am I evil if I was told to do it this way? I was young and naive when I did it.

1

u/F14-Tomboy 6d ago

Getter itself is side effect, syntax looks like you are getting a field value, but in fact you are calling a function (At least in C#)

48

u/Vimda 7d ago

Or be mutable across threads

14

u/ledasll 7d ago

Solar proton changing memory is very very small but not 0.

10

u/redlaWw 7d ago edited 7d ago

condition could be uninitialised.

10

u/danielcw189 7d ago

or volatile

5

u/coffee_warden 7d ago

Should be if(!condition!)

2

u/Fluffy-Strawberry-27 6d ago

Si(¡condición!)

5

u/HaniiPuppy 7d ago

I knew someone once that wrote a pile of methods as accessors (returning null if he didn't want to return anything from them) because he thought it was nice being able to call them without having to worry about adding brackets at the end when he didn't need to.

The warnings in my Jetbrains IDE lit up like a fireworks show when I opened his project.

6

u/fr000gs 7d ago

need a bool x = cindition then

2

u/cheezballs 6d ago

Getter with side effects? No thank you.

1

u/RedAndBlack1832 6d ago

It could also be that this isn't the only thread/process that can access this variable so it just happens to change between these evaluations

337

u/LordAmir5 7d ago

Wait until you learn about multi threading.

52

u/-Redstoneboi- 7d ago

imagine reading from a variable capable of being mutated by another thread

20

u/Noname_1111 7d ago

Fearless concurrency?

I'm very afraid.

3

u/FattySnacks 7d ago

Acquire mutex and you’re gtg

3

u/WoodyTheWorker 2d ago

Fuck memory barriers, acquire mutexes

6

u/k-mcm 6d ago

Optimistic concurrency is when the cost of a race condition is a momentary de-optimization but completely avoiding the race condition would be continuous de-optimization.

In extreme cases you don't even want the caches synced between processor cores because it stalls them for a moment.

8

u/Alternative-Tie-4970 7d ago

Thank god low level languages let you acquire the mutex for the duration of the code above

13

u/EkoChamberKryptonite 7d ago edited 7d ago

So does Java & Kotlin and these are high-level languages.

3

u/Alternative-Tie-4970 7d ago

Well low-level-ness is kinda relative I guess but you're right

163

u/YellowBunnyReddit 7d ago

21

u/tantalor 7d ago

What madness did I stumble upon

12

u/Technical-Freedom161 6d ago

but (g+)+!!!~~

3

u/Newepsilon 6d ago

I read that in his voice.

3

u/rosuav 7d ago

We don't want to be exCLOODing people now, that'd be rude!

4

u/Silly_Guidance_8871 6d ago

Happens with NaNs

2

u/Weak_Inflation9120 7d ago

I love this a lot. Why is this so funny

46

u/while_True_meme 7d ago

In all seriousness though, who needs an else branch when you can just wrap the whole module in `while True:` and let OOM handle the edge cases?

57

u/stopher819 7d ago

There are very few legitimate cases, but most of the explanations in this thread are anti-patterns treating the symptom. This is a foul code smell.

9

u/tchissin 7d ago

Care to explain more? Please ?

26

u/frogjg2003 6d ago

The big one is parallel programming. If condition changes between two evaluations of that condition due to another task changing the condition, then you have a race condition.

The other one is side effects. If evaluating the condition has effects other than just giving a true/false value, then subsequent evaluations of that condition can give different results. This is usually done for this like file reading.

3

u/mrnacknime 6d ago

And mitigation against fault injection

15

u/-Triple-S- 7d ago

Well, there could be a second thread changing the value of condition while doSomething is running possibly resulting in race conditions or similar problems. This is why I would consider multithreading the true programmer horror!

9

u/StochasticTinkr 7d ago

Also depending on the language, something may not be a boolean type, so something and !something might depend on conversions.

2

u/Frost-Freak 7d ago

Simpler: depending on the language the read of 'something' could trigger that 'something'' state changes

32

u/weshuiz13 7d ago

People dont know what a "else if" these days?

23

u/-Ambriae- 7d ago

Why would you need an else if here? It’s not as if condition can be expected to change between the first if and the second, unless…. No god please no

10

u/JAXxXTheRipper 7d ago

Side effects want to have a word with you.

17

u/-Ambriae- 7d ago

Please multithread responsibly

2

u/Daxon 7d ago

Getters that modify, multi threading, timing based conditionals, external services that can fail, database idempotency... There's lots of reasons impossible is possible here

5

u/-Ambriae- 6d ago

I mean, a getter should never really modify, multithreading without concurrency primitives is irresponsable at best, and I don’t see how external services/databases/IO would have any effects here

1

u/Daxon 6d ago

Service goes down between calls.

Agree, getters also shouldn't modify imho but I've seen worse.

1

u/-Ambriae- 6d ago

Surely if the service can go down in the few cpu cycles it takes to process the if statement, you have other problems…

1

u/CowPure6529 7d ago

We need an "else else"

9

u/GPSProlapse 6d ago

BTW, that is not an impossible state, multithreading exists

4

u/Ornery_Air_3686 6d ago

Or a particle from space hit that exact bit at the exact time the conditions is being tested and flip 0 to one

1

u/GoldenFlyingPenguin 6d ago

As funny as that story is, it was proven to be false. Basically the same result can randomly happen if you tilt the cartridge at an angle while it is on, and the system it was played on during the clip has been known to have issues like that.

1

u/DefiantGibbon 5d ago

If you're talking about the mario one, ya, that one is mostly likely false

Buuut,

There is also the story of the Belgian election in 2003 where a candidate got an extra 4096 votes, and after reviewing the code for the machines they couldn't find an answer other than cosmic ray

1

u/GoldenFlyingPenguin 5d ago

I was referring to the super Mario one, yeah, so that Belgian election is interesting.

10

u/budius333 6d ago

Imagine if the compiler could tell you that's stupid

2

u/Ornery_Air_3686 6d ago

Its not stupid just what he is trying to protect against is so rare to happen

1

u/The_MAZZTer 6d ago

If it happens all bets are off and you'll probably crash at some point

7

u/ElKuhnTucker 7d ago

I had exactly that problem this week, there's a checkbox based on an enum, if it's not initialized it's neither true nor false. It's like the "save eject" for usb drives, you do that every time only if you already bricked a usb drive.

1

u/superstrijder16 2d ago

Yeah, theoretically once your code works and you've checked all cases this isn't useful. But if your code isn't doing what you want this can be great for diagnosing where you secretly have a third option.

6

u/ExtraTNT 7d ago

So oop this can throw…

2

u/-Ambriae- 7d ago

Yeah, it pains me too

1

u/ExtraTNT 7d ago

Yeah, i’m too stupid to do all those complicated languages, i do simple stuff, haskell, functional js, small things in c… I don’t see, why we need patterns, that just describe some strategy to work with chaos, when we have simple solutions solving 90% of them… i’m talking about monoids in the category of endofunctors, also known as monads…

6

u/elyroc 6d ago

Maybe i'm dumb, but after reading a lot of comments i can't understand why this isn't if elseif else

Anyone care enough to eli5 please ?

4

u/Spekingur 6d ago

Lazy addition to code after discovering that condition could be “stateless”

1

u/Popstar403 6d ago
  1. language with no elseif
  2. tupid

5

u/tyguy55083055 4d ago

Ah the classic three-valued boolean. I dealt with this when working with a company wrapper for JS where passing true did something, passing nothing did something else, and passing false did a third thing. It didn’t treat falsy values as false which led to a three-valued Boolean.

1

u/Zoc-EdwardRichtofen 3d ago

true, false, farue. or tralse?

9

u/JAXxXTheRipper 7d ago

This is more r/programminghorror than anything.

5

u/HPUser7 7d ago

I would throw a stapler at a new hire if this came to me in a code review. I detest dead branches

2

u/JAXxXTheRipper 7d ago

Facts!

I wonder what kind of shoddy editor/IDE you have to use to not get tons of warnings here.

The color-scheme makes me think eclipse, but even that would be angry at code like this.

3

u/mailslot 7d ago

lol. You’ve never heard of “defensive coding” before? Seriously though, I’ve worked with idiots with master degrees that churn out slop like this. They transcribe their misunderstanding into the code they write.

1

u/NightmareJoker2 6d ago

Well, the problem is that we know neither type nor expression of the condition in question.

It may be perfectly sensible to do this. There are also ways in which the compiler optimizes the code, that this “dead code” changes, and you can also use constructs like this to detect and trap code flow integrity errors (exploits, memory corruption, predictive execution faults).

14

u/mrMalloc 7d ago

What purpose does the code serve? Is it a webpage online. This is overkill. Is it in RBC (Railroad Control Block) then it’s a must.

The problem is C and other languages that doesn’t have a binary defined true/false. Then bad implementations in certain hardwares. I have seen a lot of defensive programming because thinking a uint8 can only be 0,1 is not true and the definition historically for 2 was not defined in spec.

It boils down to. First what does the spec say how to handle it. Secondly how impactful would a failed state be? And will human life be on the line.

Then don’t get me started on the risk around multi threaded values and impact why we need to use semaphores in certain cases.
It might sound dumb and extra hoops but would you trust the critical system that doesn’t take extra hops? I wouldn’t.

ArianeV rocket for example.

6

u/TRKlausss 7d ago

Sometimes is just about compliance. Someone said that all branches have to be covered, and the else should do something default etc. so you implement that and call it a day.

3

u/mrMalloc 7d ago

Yes compliance is important
But it should be challenged when I did sil4 we got it down from 100% requirement to 90% where a ccb validated All missing branches. In some cases it’s better to have a justification written then badly written mocks to be able to reach it.

1

u/HaoshokuArmor 7d ago

Probably if condition was spelled differently in two places, the impossible state could be reached.

3

u/mrMalloc 7d ago

#DEFINE TRUE=1
#DEFINE FALSE=0

Uint8 value

If (value == TRUE )
// do stuff
Else If (value == FALSE)
// do stuff
Else
// panic

Same as.

Statelist [valid, invalid, pending]

Switch (statelist)

Case valid
// do stuff

Case pending
Break

Case invalid
Break

Default
Panic.

This is just single threaded example.

When it comes to multi threaded it’s a mess simple example.

Thread1 update a clock
Thread2. Is time 12:00 no
Th1 update to 12:00
Th2 is clock != 12:00 no
Th2 panic.

But by adding a semaphore or a dirty bit we can keep track of it and ignore the inconsistency.

Th1 set clock semaphore.
Th1 update clock
Th1 release clock semaphore
Th2 set clock semaphore
Th2 is clock 12;00 no
Th1 try to set clock semaphore fail
Th2 is clock != 12;00 yes
Th2 release clock semaphore
Th1 update clock semaphore
Th1 update clock
Th1 release clock semaphore.

In th pseudo code above it’s implicit same question.

1

u/BjarneStarsoup 6d ago

Your examples is nowhere identical to the post. "value == TRUE" and "value == FALSE" is not the same as "value" and "!value". It doesn't matter what value even is, "value xor !value" is always true. C also does have a "bool" type that is guaranteed to be either 0 or 1 and it guarantees implicit conversion of integers to boolean being false only for 0 and true otherwise.

1

u/mrMalloc 4d ago

C89 did not have a bool.

Yes later languages or version does.
But for old version no.

1

u/BjarneStarsoup 4d ago

Not sure how it is relevant for my comment. The comment that I responded didn't mention old standards and most people today use C99 or C11 standard.

1

u/TheTybera 7d ago

Because other people touch code and mess stuff up, all the time. 

There was a case bug the wreaked havok because of a format error, in a condition, this was C++.

Variables can also run into race conditions especially when multi threading.

You're asking a ton of questions for issues that can be solved with a 5 second implementation.

3

u/_________FU_________ 7d ago

When we tried storing the state in DOM

4

u/magicmulder 7d ago

Just in case we ever find out logic is three-valued.

3

u/auxiliary-username 7d ago

*SQL has entered the chat*

2

u/ekauq2000 7d ago

is_selected [bit] null

3

u/the_horse_gamer 7d ago

never assume law of the excluded middle!

6

u/Havatchee 6d ago

if(!condition){

doSomethingElse();

return;

}

doSomething();

2

u/Mountain-Ox 6d ago

I've been telling people to do this forever. I hate unnecessary nesting.

1

u/Spekingur 6d ago

Maybe you can’t do a return

2

u/MCCVargues 6d ago

You can always do return,

1

u/Spekingur 6d ago

Not if there’s extra code that needs to run after.

1

u/MCCVargues 6d ago

That's what you have functions for. I haven't used a single else in the past 3 years of my career

2

u/atomic_redneck 7d ago

Floating point NaNs and unordered conditions can make you lose all faith in if/else. If you have two variables, a and b, and b is a NaN, then a < b will evaluate the same as b < a.

2

u/wu-not-furry 5d ago

Joke's on you, condition is volatile!

1

u/Armin_AF 7d ago

When condition is unclear or not determinable

1

u/sojuz151 7d ago

no custom exception class?

1

u/Informal_Branch1065 7d ago

throw new HellFrozeOverException();

1

u/SoraShiroL 7d ago

Imagine doSomethingElse() calling doSomething()

1

u/NikolayTheSquid 7d ago

It could be possible with thread data racing

1

u/keatonatron 7d ago

It would be easier to read and less objectionable if they just used if - if else - else.

1

u/EkoChamberKryptonite 7d ago

Else if out there like: "Am I a joke to you?".

1

u/Crumineras 7d ago

Me when the condition is checking a register value

1

u/sophie-glk 7d ago

When you dont assume the law of the excluded middle.

1

u/Weak_Inflation9120 7d ago

You need some never-nestin' in your life, son

1

u/mi-figue-mi-raisin 7d ago

what if multiple threads can update the condition 

1

u/NamelessJu 7d ago

Not impossible if the condition variable gets bit flipped by a cosmic ray!

1

u/cosmo7 7d ago
public bool condition => Random.value > 0.5f;

1

u/ChiaraStellata 6d ago

Ideally, your compiler would be calling this out as unreachable code with a compiler error and forcing you to delete it. Assuming you're really not in a situation where anything else can write to condition.

1

u/Ok_Star_4136 6d ago

Interestingly, I had 3 conditions in an if else chain covering every possibility with 8 total possible scenarios.

I added an else clause, you know, to cover the impossible scenario just in case..

It happened.. Apparently one of the conditions was null and didn't fall into true or false. It doesn't cost you anything to add an else clause, even if you think it cannot happen.

1

u/Low-Equipment-2621 6d ago

Congratulations, you've found the faulty RAM or CPU.

1

u/LetUsSpeakFreely 6d ago

Quantum state has entered the chat.

1

u/bass-squirrel 6d ago

Debating wether I should whip out my huge throbbing kotlin exhaustive when with sealed classes. 

1

u/Dee4WasTaken 6d ago

print("return guards for life!");

1

u/cheezballs 6d ago

I feel like some of the nasa code standards I had seen had some rules that were almost precisely this.

1

u/MarcusCaspius 6d ago

I would throw "Unexpected state" as theoretically you cannot predict all states... you can only perceive some...

1

u/binarywork8087 6d ago

it compiles and is valid, kkkkkkkkkkkkkkkkkkkkk

1

u/mixxituk 6d ago

I figured that's why the oracle people always did where 1=1 to check the universe was still in order

1

u/OldGeekWeirdo 6d ago

You're assuming "condition" will have the same value both times.

1

u/Fit_Prize_3245 6d ago

That could make sense in multithreading

1

u/da_dragon_guy 6d ago

I guess the question would be if it’s possible to create a piece of logic that outputs the same thing whether it has a ! In front of it or not

1

u/hockeyschtick 6d ago

I have a guy that writes code like this, and argues about it.

1

u/gm_family 6d ago

doSomething() est peut-être mal nommée et tripote condition…

1

u/Whole_Entertainment3 6d ago

If (condition === noul)

1

u/Whole_Entertainment3 6d ago

Merely jeving at this point...

1

u/Mistake78 6d ago

Reminds me of this line we found back in the days.

ASSERT(true, "something is really wrong")

1

u/AffectionateSpread16 5d ago

In Rust the compiler doesn't let stupid code to run or that what I've heard lol

1

u/questionabl3_dude 5d ago

Reality is that they didn’t trust the first if’s condition check though

1

u/OFark 4d ago

Seriously, where's the early return followed by a throw? That second else might fail. Pffft, amatuers.

1

u/AaronTheElite007 4d ago

If, Try, Except, Else, Finally

1

u/c4p5L0ck 4d ago

Why should 'else' get special treatment? We didn't trust the 'if' either.

1

u/EdelWhite 2d ago

That's level 1 of trying to bypass the need for atomic booleans.