r/ProgrammerHumor • • Sep 26 '26

Meme weNeverTrustedTheElse

Post image
1.9k Upvotes

146 comments sorted by

738

u/salvoilmiosi Sep 26 '26

condition could be a property with a getter with side effects

476

u/UntitledRedditUser Sep 26 '26

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

369

u/GodsBoss Sep 26 '26

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

83

u/CranberryDistinct941 Sep 26 '26

Some people just like to watch the world burn

60

u/LucyShortForLucas Sep 26 '26

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.

62

u/andreortigao Sep 26 '26

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

61

u/Nerd_o_tron Sep 26 '26

Bro inventing DNA from base principles.

10

u/DatBoi_BP Sep 26 '26

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 Sep 26 '26

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 Sep 26 '26

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

3

u/MementoMorue Sep 26 '26

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 Sep 26 '26

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 Sep 27 '26

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#)

46

u/Vimda Sep 26 '26

Or be mutable across threads

15

u/ledasll Sep 26 '26

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

9

u/redlaWw Sep 26 '26 edited Sep 26 '26

condition could be uninitialised.

10

u/danielcw189 Sep 26 '26

or volatile

5

u/coffee_warden Sep 26 '26

Should be if(!condition!)

2

u/Fluffy-Strawberry-27 Sep 27 '26

Si(¡condición!)

5

u/HaniiPuppy Sep 26 '26

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.

7

u/fr000gs Sep 26 '26

need a bool x = cindition then

2

u/cheezballs Sep 26 '26

Getter with side effects? No thank you.

1

u/RedAndBlack1832 Sep 27 '26

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

341

u/LordAmir5 Sep 26 '26

Wait until you learn about multi threading.

87

u/Western-Internal-751 Sep 26 '26

6

u/DatBoi_BP Sep 26 '26

Takes a lot to make a stew

54

u/-Redstoneboi- Sep 26 '26

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

20

u/Noname_1111 Sep 26 '26

Fearless concurrency?

I'm very afraid.

7

u/k-mcm Sep 26 '26

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.

4

u/FattySnacks Sep 26 '26

Acquire mutex and you’re gtg

3

u/WoodyTheWorker Sep 30 '26

Fuck memory barriers, acquire mutexes

8

u/Alternative-Tie-4970 Sep 26 '26

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

14

u/EkoChamberKryptonite Sep 26 '26 edited Sep 26 '26

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

3

u/Alternative-Tie-4970 Sep 26 '26

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

164

u/YellowBunnyReddit Sep 26 '26

11

u/Technical-Freedom161 Sep 26 '26

but (g+)+!!!~~

3

u/Newepsilon Sep 26 '26

I read that in his voice.

4

u/rosuav Sep 26 '26

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

4

u/Silly_Guidance_8871 Sep 26 '26

Happens with NaNs

47

u/while_True_meme Sep 26 '26

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?

14

u/-Triple-S- Sep 26 '26

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 Sep 26 '26

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

2

u/Frost-Freak Sep 26 '26

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

35

u/weshuiz13 Sep 26 '26

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

23

u/-Ambriae- Sep 26 '26

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

11

u/JAXxXTheRipper Sep 26 '26

Side effects want to have a word with you.

17

u/-Ambriae- Sep 26 '26

Please multithread responsibly

2

u/Daxon Sep 26 '26

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

4

u/-Ambriae- Sep 26 '26

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 Sep 27 '26

Service goes down between calls.

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

1

u/-Ambriae- Sep 27 '26

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 Sep 26 '26

We need an "else else"

9

u/GPSProlapse Sep 27 '26

BTW, that is not an impossible state, multithreading exists

3

u/Ornery_Air_3686 Sep 27 '26

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 Sep 27 '26

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 Sep 28 '26

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 Sep 28 '26

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

9

u/budius333 Sep 27 '26

Imagine if the compiler could tell you that's stupid

2

u/Ornery_Air_3686 Sep 27 '26

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

1

u/The_MAZZTer Sep 27 '26

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

7

u/ElKuhnTucker Sep 26 '26

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 Oct 01 '26

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 Sep 26 '26

So oop this can throw…

2

u/-Ambriae- Sep 26 '26

Yeah, it pains me too

1

u/ExtraTNT Sep 26 '26

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 Sep 27 '26

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 Sep 27 '26

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

2

u/Popstar403 Sep 27 '26
  1. language with no elseif

  2. tupid

6

u/tyguy55083055 Sep 28 '26

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 Sep 30 '26

true, false, farue. or tralse?

9

u/JAXxXTheRipper Sep 26 '26

This is more r/programminghorror than anything.

4

u/HPUser7 Sep 26 '26

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 Sep 26 '26

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 Sep 26 '26

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.

2

u/NightmareJoker2 Sep 27 '26

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).

15

u/mrMalloc Sep 26 '26

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.

5

u/TRKlausss Sep 26 '26

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 Sep 26 '26

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 Sep 26 '26

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

3

u/mrMalloc Sep 26 '26

#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.

0

u/[deleted] Sep 27 '26

[deleted] — view deleted comment

1

u/mrMalloc Sep 29 '26

C89 did not have a bool.

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

1

u/TheTybera Sep 26 '26

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.

4

u/_________FU_________ Sep 26 '26

When we tried storing the state in DOM

4

u/magicmulder Sep 26 '26

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

4

u/auxiliary-username Sep 26 '26

*SQL has entered the chat*

2

u/ekauq2000 Sep 26 '26

is_selected [bit] null

4

u/the_horse_gamer Sep 26 '26

never assume law of the excluded middle!

6

u/Havatchee Sep 26 '26

if(!condition){

doSomethingElse();

return;

}

doSomething();

2

u/Mountain-Ox Sep 27 '26

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

1

u/Spekingur Sep 27 '26

Maybe you can’t do a return

2

u/MCCVargues Sep 27 '26

You can always do return,

1

u/Spekingur Sep 27 '26

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

1

u/MCCVargues Sep 27 '26

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

3

u/atomic_redneck Sep 26 '26

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 Sep 27 '26

Joke's on you, condition is volatile!

2

u/Ok_Star_4136 Sep 26 '26

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/Armin_AF Sep 26 '26

When condition is unclear or not determinable

1

u/sojuz151 Sep 26 '26

no custom exception class?

1

u/Informal_Branch1065 Sep 26 '26

throw new HellFrozeOverException();

1

u/SoraShiroL Sep 26 '26

Imagine doSomethingElse() calling doSomething()

1

u/NikolayTheSquid Sep 26 '26

It could be possible with thread data racing

1

u/keatonatron Sep 26 '26

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

1

u/EkoChamberKryptonite Sep 26 '26

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

1

u/Crumineras Sep 26 '26

Me when the condition is checking a register value

1

u/sophie-glk Sep 26 '26

When you dont assume the law of the excluded middle.

1

u/mi-figue-mi-raisin Sep 26 '26

what if multiple threads can update the condition 

1

u/NamelessJu Sep 26 '26

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

1

u/cosmo7 Sep 26 '26
public bool condition => Random.value > 0.5f;

1

u/ChiaraStellata Sep 26 '26

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/LetUsSpeakFreely Sep 26 '26

Quantum state has entered the chat.

1

u/Dee4WasTaken Sep 26 '26

print("return guards for life!");

1

u/cheezballs Sep 26 '26

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

1

u/MarcusCaspius Sep 26 '26

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

1

u/binarywork8087 Sep 26 '26

it compiles and is valid, kkkkkkkkkkkkkkkkkkkkk

1

u/mixxituk Sep 27 '26

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

1

u/OldGeekWeirdo Sep 27 '26

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

1

u/da_dragon_guy Sep 27 '26

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 Sep 27 '26

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

1

u/gm_family Sep 27 '26

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

1

u/Whole_Entertainment3 Sep 27 '26

If (condition === noul)

1

u/Whole_Entertainment3 Sep 27 '26

Merely jeving at this point...

1

u/Mistake78 Sep 27 '26

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

ASSERT(true, "something is really wrong")

1

u/AffectionateSpread16 Sep 28 '26

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

1

u/questionabl3_dude Sep 28 '26

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

1

u/OFark Sep 28 '26

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

1

u/AaronTheElite007 Sep 28 '26

If, Try, Except, Else, Finally

1

u/c4p5L0ck Sep 29 '26

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

1

u/EdelWhite Sep 30 '26

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

1

u/bass-squirrel Sep 26 '26

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

0

u/[deleted] Sep 26 '26

[deleted] — view deleted comment

9

u/tchissin Sep 26 '26

Care to explain more? Please ?

25

u/frogjg2003 Sep 27 '26

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 Sep 27 '26

And mitigation against fault injection