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.
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
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.
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.
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!
Getters that modify, multi threading, timing based conditionals, external services that can fail, database idempotency... There's lots of reasons impossible is possible here
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
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.
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
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.
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.
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…
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.
738
u/salvoilmiosi Sep 26 '26
condition could be a property with a getter with side effects
341
u/LordAmir5 Sep 26 '26
Wait until you learn about multi threading.
164
u/YellowBunnyReddit Sep 26 '26
You just assumed the law of the excluded middle.
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?
25
u/white_equatorial Sep 26 '26
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!
35
u/weshuiz13 Sep 26 '26
People dont know what a "else if" these days?
9
u/GPSProlapse Sep 27 '26
BTW, that is not an impossible state, multithreading exists
9
u/budius333 Sep 27 '26
Imagine if the compiler could tell you that's stupid
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.
6
u/ExtraTNT Sep 26 '26
So oop this can throw…
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 ?
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.
9
u/JAXxXTheRipper Sep 26 '26
This is more r/programminghorror than anything.
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.
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/the_horse_gamer Sep 26 '26
never assume law of the excluded middle!
6
u/Havatchee Sep 26 '26
if(!condition){doSomethingElse();return;}doSomething();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
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/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
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/[deleted] 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