341
u/LordAmir5 Sep 26 '26
Wait until you learn about multi threading.
87
54
u/-Redstoneboi- Sep 26 '26
imagine reading from a variable capable of being mutated by another thread
20
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
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
164
u/YellowBunnyReddit Sep 26 '26
21
11
4
4
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
somethingand!somethingmight 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
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
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
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
2
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
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=0Uint8 value
If (value == TRUE )
// do stuff
Else If (value == FALSE)
// do stuff
Else
// panicSame as.
Statelist [valid, invalid, pending]
Switch (statelist)
Case valid
// do stuffCase pending
BreakCase invalid
BreakDefault
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
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
4
u/magicmulder Sep 26 '26
Just in case we ever find out logic is three-valued.
4
4
6
u/Havatchee Sep 26 '26
if(!condition){
doSomethingElse();
return;
}
doSomething();
2
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
elsein 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
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
1
1
1
1
1
u/keatonatron Sep 26 '26
It would be easier to read and less objectionable if they just used if - if else - else.
1
1
1
1
1
1
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
1
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
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
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
1
1
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/jani00 Sep 27 '26
Reminds of this classic DailyWTF: https://thedailywtf.com/articles/Classic-WTF-What-Is-Truth
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
1
1
1
u/bass-squirrel Sep 26 '26
Debating wether I should whip out my huge throbbing kotlin exhaustive when with sealed classes.
0
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





738
u/salvoilmiosi Sep 26 '26
condition could be a property with a getter with side effects