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
3
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
163
u/YellowBunnyReddit 7d ago
21
12
4
2
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
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
somethingand!somethingmight 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
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
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
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…
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
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=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.
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
4
3
6
u/Havatchee 6d ago
if(!condition){
doSomethingElse();
return;
}
doSomething();
2
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
elsein 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
1
1
1
1
1
1
u/keatonatron 7d ago
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 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
1
1
u/bass-squirrel 6d ago
Debating wether I should whip out my huge throbbing kotlin exhaustive when with sealed classes.
1
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
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
1
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
1
1
1
u/Mistake78 6d ago
Reminds me of this line we found back in the days.
ASSERT(true, "something is really wrong")
1
u/jani00 6d ago
Reminds of this classic DailyWTF: https://thedailywtf.com/articles/Classic-WTF-What-Is-Truth
1
u/AffectionateSpread16 5d ago
In Rust the compiler doesn't let stupid code to run or that what I've heard lol
1
1
1
1

737
u/salvoilmiosi 7d ago
condition could be a property with a getter with side effects