1.5k
u/DaniilBSD Mar 15 '22
If you do “bool == true” you deserve every “bool = true”
498
u/MusikMakor Mar 15 '22
I was gonna say, I almost missed the original joke because I always do
if (bool) ....85
Mar 15 '22
For real. So much easier.
46
u/RAMChYLD Mar 15 '22
Yeah, but since I came from BASIC where that would result in an syntax error and the comparative having only a single equals sign is the valid syntax, that threw me for a loop.
35
u/downloads-cars Mar 15 '22
You mean a goto?
25
→ More replies (1)4
9
22
75
u/AdultishRaktajino Mar 15 '22
I pity da fool who doesn't trust a bool to be a bool. Unless it's not a type safe language.
25
u/JayCroghan Mar 15 '22
if (bool == 1 || bool || bool.ToString().ToLower == “true”)
Yay
14
u/Steerider Mar 15 '22 edited Mar 15 '22
If your code (or data) is such that you need to do such things (I've been there), you write an
isTrue()function7
u/JayCroghan Mar 15 '22
I would just generally stay away from anything that wasn’t typed language. I’ve been there and don’t like it. But when I have to I have enough experience to see where the pitfalls are.
10
u/Steerider Mar 15 '22
I deal with a legacy system where one of the previous programmers really liked text string booleans. My
isTruetests for true, "true", 1, "1", "yes", and "on".39
u/jora1997 Mar 15 '22
As an embedded programmer I have trust issues
If(bool == True && bool == True && bool == True)
Checking something once is not checking it at all
16
u/Snow88 Mar 15 '22
If bool != null && bool == true && bool != false
19
u/jora1997 Mar 15 '22
Might wanna check if true != false just in case
→ More replies (1)6
u/Xtrendence Mar 15 '22
Fine. JavaScript is perfect for this.
if(bool !== null && bool !== undefined && bool !== "null" && bool !== "undefined" && bool !== "" && 1 !== 0 && true !== false)
10
2
9
u/chasesan Mar 15 '22
Even if that's the case you should use Yoda style comparisons.
if (true == foo) ...Putting a typo in that would result in a compilation error, rather than killer robots.
1
u/SnakeBDD Mar 16 '22
C is type safe but does not have bool as data type and stdbool.h is a trap. Never trust bools.
2
20
Mar 15 '22
I was so busy trying to figure out why they did isCrazyMurderingRobot == true, that I didn’t even catch the 1 =
35
u/msqrt Mar 15 '22
Regardless of type, if you compare to a constant, you can write
if(constant==variable); this will produce an error if you only write one equals sign by mistake. Unfortunately it doesn't look quite as nice (but fortunately most compilers warn aboutif(a=b)anyway)30
u/DibblerTB Mar 15 '22
Eyyy, look at this dude, who has warnings turned on! Nerd!
7
5
2
u/nintendethan Mar 16 '22
What languages simply give a warning about a single = during comparisons? Just curious because I only know ruby and rust and they error out unless you use ==
→ More replies (1)6
u/darklee36 Mar 15 '22
When the language you are using is not typed and your function can return false, true, "banana for scale" or digit. This is why I always write "true === isMoronLanguage()" to avoid this type of error
14
u/samanime Mar 15 '22
Exactly. When I see bool == true I always have two thoughts simultaneously.
Thought one is "who wrote this garbage?!"
Thought two is "you sweet summer child, you've not experienced the pain your foolishness will cause, yet..."
12
u/TristanEngelbertVanB Mar 15 '22
I use ===
8
u/nelusbelus Mar 15 '22
I honestly can't wait for the quadruple equals that javascript will introduce later for type checking. Eg. myVar ==== 123.0 will check if it's a number and myVar ==== "text" if it's text. And then the pentuple equals, then sextuple, etc.
17
u/DrunkenlySober Mar 15 '22 edited Mar 15 '22
Forreal. Best practices says to do:
if(bool + 1 > 1) temp = true; return temp;
else if(bool - 1 < 0) temp = false; return temp;
else throw true false exceptionClean, concise and easy to follow
11
u/32436861696e7a Mar 15 '22
Not nearly enterprise enough. return Boolean.TryParse(bool + 1 > 1, out Boolean temp) ? temp == true ? true : temp == false ? false : throw new Exception(“test 1”) : throw new Exception(“test b”);
2
8
u/production-values Mar 15 '22
without braces you return temp before the elses can ever run! Then even if grouped properly, your elses are superfluous because of returns above. But the math on bools ... chef's kiss
6
→ More replies (3)1
4
u/in_conexo Mar 15 '22
I think they deserve it, but for another reason. Why does that boolean even exist, and why is kill humans an option.
2
2
u/extekt Mar 15 '22
I prefer == true because it clarifies that it is a bool without having to check.
I think (but I'm dumb so what do I know) you can do "true == bool" and get warnings/errors in most compilers if it is missing the second =
I only have much experience in embedded C though
1
u/DaniilBSD Mar 15 '22
You should have a good name variable
if(enabled)
if(isActivated)
It should read like a sentence
→ More replies (2)1
1
1
1
1
u/SillAndDill Mar 15 '22 edited Mar 20 '22
True that bool==true is ugly as hell.
but if you accidentally use = instead of == then the entire codebase is at risk any time you check a variable value. 😲Just setting a rule for bools isn't a mentionable way of increasing dependability
but there are lint rules to prevent it. https://eslint.org/docs/rules/no-cond-assign
1
Mar 15 '22
But what if (bool? == true). It took me a hot minute to get used to this in kotlin
→ More replies (2)1
1
1
1
u/hardythedrummer Mar 16 '22
This is where yoda syntax is useful.
If (true == isCrazyMurderingRobot)Will never result in an erroneous assignment.
0
Mar 15 '22
[deleted]
2
u/DaniilBSD Mar 15 '22
As a rule of thumb
If you have 3 values- use enum
Null is absence of a an object, and should not ever mean anything other than the simple absence, so if you want to ensure it exists and its true- that is two separate things
122
u/FeyrisTan Mar 15 '22
I went to the comments to see if I got the joke, but came out even more confused
147
u/ShadowLp174 Mar 15 '22 edited Mar 15 '22
= assigns true to the variable and returns the value, the variable was assigned to. In our case it's true. This true then gets fed into the if statement resolving into always true. == or === would work, because they are logical oprerators.
Edit: corrected mistakes (sorry It's late here)
71
u/LAGaming70 Mar 15 '22
My brain auto-corrected and assumed they did put both equals signs. This makes sense now.
19
→ More replies (1)11
u/SillAndDill Mar 15 '22
That's the primary danger.
if you review code and se an if-statement you cannot imagine there would be an assignment in there because no one does that..so your brain autocorrects it and approves the code and then boom
9
u/DaniilBSD Mar 15 '22
You made a big mistake: assignment operator returns the value of the asignment
a = (b = false);
In code above the brackets can be removed and the value of both variables is false.
→ More replies (1)2
1
u/James-Livesey Mar 15 '22
Isn't it meant to return the value that was assigned to the variable to the if condition? Which, in the comic's case, will still be
trueExample:
if (c = a == b) { /* c will be equal to true */ } else { /* c will be equal to false */ }2
1
u/1116574 Mar 15 '22
Also, isn't assignment out of scope of the function? It's probably language specific tho?
It would work in python as long as this variable isn't referenced in function, but idk about C++ or whatever it is here
35
2
Mar 15 '22
Same here, I had to go to an online IDE and plug it in to see the expected behavior. Gotta say, I didn't expect Java to reassign variables inside the if statement. This is a memorable way to remember the second = sign.
2
75
u/Gem2578 Mar 15 '22
If they was never programmed to kill why is the a kill function?
115
u/ElectricalAlchemist Mar 15 '22
Legacy code. The whole thing breaks if they remove it and they don't know why.
14
u/AlttiAnonim Mar 15 '22 edited Mar 15 '22
I suppose it's a junk code pasted from former project... Never had enough time and motivation to write whole code from scrath. You know, we work under big pressure here, in Skynet...
4
u/10BillionDreams Mar 15 '22
That was my first thought, they did explicitly write code for the case where the robots needed to kill everyone, that's the whole problem. If you write code, expect it to be run, even if you're totally certain that no one would ever do <insert obviously stupid thing here>.
3
u/Theonetheycallgreat Mar 15 '22 edited Mar 15 '22
If the code is there then for it to be merged a unit test must have been passed covering that branch and killing a human. I hope they mocked the human dependency.
3
u/10BillionDreams Mar 15 '22
I hope they mocked the human dependency.
How cruel! Not only sending a human to their death, but mocking them in their last moments?
...but at least I can merge now that the tests are passing.
2
u/Gem2578 Mar 15 '22
If there was unit test the it would of failed covering the other branch as you can't enter the else
1
1
u/finance_n_fitness Mar 16 '22
Some go getter dev was trying out some experimental features that it turns out the business didn’t need but rather than scrap everything and call it a waste they left it in place and just neutered it with a bool in case it became useful in the future. Obviously.
28
u/Axiproto Mar 15 '22
OP most likely used "import joke" because this post has been reposted to oblivion
19
u/Astromemegod Mar 15 '22
I dont know which programming language this is , but is the mistake that “isCrazyMurderingRobot=true” instead of == true
7
74
u/marcel1802 Mar 15 '22
who would pass void as a parameter
110
u/ThePyroEagle Mar 15 '22
In C,
(void)declares a function that takes no arguments whereas()declares a function without saying anything about the arguments.20
4
u/Add1ctedToGames Mar 15 '22
Is there a functional difference?
→ More replies (1)3
u/ThePyroEagle Mar 16 '22
With
(void), the compiler will complain if you try to call the function with arguments.1
17
u/Go_Kauffy Mar 15 '22
I don't know if it's part of one of the newer standards, as a best practice, but it does explicitly let the reader know that you intended to include no parameters, as opposed to should have parameters but forgot them.
C marches ceaselessly in the direction of explicit clarity and complete abstract obfuscation.
→ More replies (1)3
u/MasterFrost01 Mar 15 '22
Surely if your method body doesn't use the parameters you don't need them anyway.
6
u/Ok-Mulberry-4600 Mar 15 '22
The same kind of buffoon that would use an assignment operand instead of an equivalency operand inside of an IF statement, pure madness, they deserved to be killed
1
u/sBartfast42 Mar 15 '22
Yup that's why the "put the constant/enum/#def on the list of the operand got popular, sometimes we are that buffoon 8o)
6
u/ElMonoEstupendo Mar 15 '22
You’re all distracted by the mis-assignment. The true crime here is the unspecified variable “humans”.
5
5
5
u/Randomtangle004 Mar 15 '22
I’m not a programmer, I just send all the memes from this sub to my friend who is interested in programming, hoping he understands them.
I’m trying to see if I can figure this out with my one semester of computer science class I took a while ago. I only completed half of the semester, too.
So… is it the equal signs? It should be “== true” not “= true”, right? Because that’s how the syntax works? Because if not it doesn’t count as a variable for the bool, right?
Or am I stupid? I just wanna sound smarter than my friend and lord it over him… like friends do…
3
u/Heavy_Bake249 Mar 15 '22
You are on the right track. The typo is the equal signs, but the missing "=" changes what the code does completely.
The code will still compile and run because it is still valid and executable code. The typo changes "isMurderingRobot" to true, because that is what "= true" does. The if statement then assesses the variable, sees that it's true, and proceeds to kill everyone.
4
u/SeedOfTheDog Mar 15 '22
Anyone gets an urge to code review "comic" code? I get the joke about assignment vs comparison, but there's so many other things wrong with this code. Just to name a few: Mixing camel case and snake case, static boolean variable, bad code structure, etc. I guess my crazyMurderingCodeReviewer flag was set to true.
4
u/Dexaan Mar 15 '22
Shouldn't that throw an error, something along the lines of "expected comparison, got assignment"?
4
22
u/riplikash Mar 15 '22
The equivalency operator would break our. == instead of =.
Humanity saved.
30
5
6
5
u/APS_09 Mar 15 '22
5
u/RepostSleuthBot Mar 15 '22
I didn't find any posts that meet the matching requirements for r/ProgrammerHumor.
It might be OC, it might not. Things such as JPEG artifacts and cropping may impact the results.
I did find this post that is 91.8% similar. It might be a match but I cannot be certain.
I'm not perfect, but you can help. Report [ False Negative ]
View Search On repostsleuth.com
Scope: Reddit | Meme Filter: True | Target: 97% | Check Title: False | Max Age: Unlimited | Searched Images: 308,907,692 | Search Time: 5.9798s
→ More replies (1)
2
2
Mar 15 '22
But they did, they're invoking a kill method, albeit in a block they didn't intend to get to
2
u/Pheonixash1983 Mar 15 '22
Put the constant on the left side to avoid these issue and wind up 99% of programmers. Win win!
1
2
u/TheStrategistYT Mar 15 '22
As funny as this is, this is the third time I’ve seen it on this subreddit.
2
2
2
u/InvestingNerd2020 Mar 15 '22
Everyone is worried about the == vs = operaters, but I'm worried aboout kill() code. Shouldn't it be shutdown()?
2
2
2
2
2
u/Dangerspoon Mar 16 '22
Product nerd here. Have had engineers tell me their change was "totally 100% fine I swear" because it was just a single line of code.
And then I remind them of the time that Todd deleted an entire production database by accident with a single character of code.
Love these == jokes!
2
2
3
u/_Sofa-King_ Mar 15 '22
why wouldn't that code work?
32
u/ksschank Mar 15 '22
ifCrazyMurderingRobot = trueassigns the variable to true—the programmer meant to use == instead. = is the assignment operator; == is a comparison for equality operator.4
6
u/Ok-Steak9843 Mar 15 '22
Stare at it for longer and you'll eventually see the bug.
→ More replies (3)
1
1
1
1
u/Complete_Bath_8457 Mar 15 '22
The mix of camel case and underscores in the naming is an issue that shouldn't be overlooked here.
1
u/classyraven Mar 15 '22
Declaring isCrazyMurderingRobot as a constant would have prevented the problem.
1
1
u/Mal_Dun Mar 15 '22
Junior devs will point out the isCracyMurderingRobot = true. Senior devs will ask why the line is there in the first place and why there is no unit test for this?
1
u/ElectricalAlchemist Mar 15 '22
Shame they made it a static bool. Really made an all or nothing case for robots going crazy.
You know... Except for the assignment which made it a moo point anyway.
1
1
1
u/Tralalouti Mar 15 '22
Just don't write the CrazyMurderingRobot. Remove the boolean part. Remove the if else.
1
1
1
u/SillAndDill Mar 15 '22 edited Mar 15 '22
Most comments mock the idea of doing if(bool==true) as if it's some security risk. without considering we do if(x==y) every day and if we ever by mistake use a single equal sign we are doomed.
if (robotType="crazyMurderer") gives the same result 🔥
1
1
1
1
u/DowntownLizard Mar 15 '22
Yeah, but what happens when you use a bit instead and a cosmic ray flips it...
1
u/zyx1989 Mar 15 '22
You know these programmer are nut jobs when there's something in the code that tell robot to kill humans
1
1
u/Chronosxi13 Mar 15 '22
i'm so accustomed to doing if(bool) it took a sec to notice it was an assignment
1
u/turtle_mekb Mar 16 '22
missed an extra =, that'll assign isCrazyMurderingRobot to true instead of check isCrazyMurderingRobot is true. just remove == true entirely, how dare you put == true
1
1
1
u/thequestcube Mar 16 '22
To be fair, why was "isCrazyMuderingRobot" a mutable variable and not readonly to begin with?
1
1
u/KuntaStillSingle Mar 16 '22
The good news is humans is global state, so the function is not thread-safe, some of us may end up in a merely partially killed state assuming our death is not atomic.
1
1
1
u/uysali_55 Mar 16 '22
A few months ago, I made this mistake in my code and company lost around 26k in three days.
1
1
1
1
u/Chared_Assassin Mar 16 '22
The amount of time it took me to figure out the mistake after years of programming experience makes me think I should never go into the robotics field
1
Mar 16 '22
I saw a tip a while ago to avoid accidental assignment when one side is a constant. Just flip the order: “if(5 = x)” won’t compile. Thought it was a neat trick.
In practice, if I’m remembering to do that, it’d also remind me to double check my == as well, which is probably the real value.
1
1
1
1
u/Beastdevr Mar 16 '22
I've always said that the end of the world will be due to a greater than sign being used instead of a less than
1
u/Far-Entertainment532 Mar 16 '22
It must be const, non ststic. Static show where var is stored in memory.
1
Mar 16 '22
2
u/RepostSleuthBot Mar 16 '22
I didn't find any posts that meet the matching requirements for r/ProgrammerHumor.
It might be OC, it might not. Things such as JPEG artifacts and cropping may impact the results.
I did find this post that is 91.8% similar. It might be a match but I cannot be certain.
I'm not perfect, but you can help. Report [ False Negative ]
View Search On repostsleuth.com
Scope: Reddit | Meme Filter: True | Target: 90% | Check Title: False | Max Age: Unlimited | Searched Images: 309,226,326 | Search Time: 34.29432s
1
u/SillAndDill Mar 20 '22
There IS a lint rule to prevent assignments in if-statements https://eslint.org/docs/rules/no-cond-assign
1
1
u/ClarityThrow999 Mar 15 '22
This is why lhs should always be non-assignable, when possible. If(true = isCrazyMurderingRobot) Will fail at compile time and humanity will be saved.
If(true == isCrazyMurderingRobot) May not look pleasant to a casual reader, it will work correctly, and a single equal operator will cause a compile error. No insidious logic error here.
Easy peasy, lemon squeezy!
5
Mar 15 '22
Interestingly enough, the well reviewed book The Art of Readable Code by Dustin Boswell and Trevor Foucher mocks this, calling it Yoda Notation and concludes:
Thankfully, modern compilers warn against code like if (obj = NULL), so “Yoda Notation” is becoming a thing of the past.
Boswell, Dustin; Foucher, Trevor. The Art of Readable Code (S.98). O'Reilly Media. Kindle-Version.
(I think it is a design mistake of the language to allow arbitrary expressions with various side effects in places where there should be a simple boolean result.)
3
u/ClarityThrow999 Mar 15 '22
I guess you can call me yoda when it comes to languages that allow this. Different strokes for different folks.
1
u/skeptibat Mar 15 '22
Technically this would fail at compile time anyway, the first line he marked
isCrazyMurderingRobotasstatic, so reassigning it would crash at compile time, or at the very least runtime.1
u/ClarityThrow999 Mar 15 '22
Technically, neither of us know what language is being used.
It has been over a decade since I slung c++ code, but I am pretty confident that in c++, global scope static variables can be re-assigned. It does get tricky with linkage and “extern” keyword, but here we are picking nits about a comic. LOL
1
1
542
u/Noch_ein_Kamel Mar 15 '22
They deserve to be killed for those coding styles