There is a colleague of mine who constantly writes:
if (someBool == true)
It drives me up the wall! Should I make a big deal of it or just drop it?
|
There is a colleague of mine who constantly writes:
It drives me up the wall! Should I make a big deal of it or just drop it? |
||||
| show 8 more comments |
This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: FAQ.
|
It's only redundant code, not life or death. However.... If it's happening a lot, it could be a problem with how
or
or
for example. |
|||||||||
|
|
When I see However, my perspective is skewed because I spent several summers in college teaching programming to kids, who frequently wrote expressions like this because they genuinely hadn't mastered the mental exercise of evaluating an expression. Once they grokked the concept, the redundancy became obvious. For an otherwise competent professional programmer, this is probably not the case. It's probably just a bad habit they developed in their early days of programming and never quite shook. But it would still scare me a bit if it was the first thing I saw someone do in an interview. |
|||||||||||||||||
|
|
That drives me crazy too, but I'd say mention the redundancy to them in a constructive way and then drop it even if they don't concur. Alternatively you could try this approach:
Them: Why would we do that? The second half of that statement is redundant, it would always evaluate to true. You: By George, you are right. Silly me. Let's just go with a version without all the redundancy then. How about?
|
|||||||||||||||||||||
|
|
I think that, if something so trivial is your biggest problem with your co-workers, you should consider yourself pretty lucky. |
|||||||||||||
|
|
You should definitely stop this bad habit. Gently... It's easy to forget to write the double equal signs, turning the code into:
In C# for example this will only produce a warning, not an error. So unless you treat warnings as errors the code will run, set the variable to true and always enter the condition. |
|||||||||||||||||||||
|
|
I agree with you, but I'm going to play devil's advocate here: Depending on the language and the variable's name, x==true is appropriate: Consider the following situation in a language with static typing and type coercion for integral types:
Someone reading this section of the code might not realize immediately that actionsAllowed is a boolean variable - it could also be an integer, representing the number of allowed actions. So by adding == true, it becomes clear that x is a boolean, and not an integer being coerced to a boolean:
|
|||||||||||||||||||||
|
|
What about nullable bools?
|
|||||||||||||||||
|
|
Typically, you don't want to make a big deal out of coding conventions unless said convention is somehow impeding the project in a significant way. I've seen many a heated argument escalate over things as small as code regions and leading underscores. That being said, I see no issue with adding If there is an established convention, I say follow it unless there is reason to change. However, it's not really worth raising a big stink about. |
|||||
|
|
Ack. I'm that guy. The shame, the shame. It's how I learned, and it's how I "auto-format" in my head. The only time I use Joel's prefered syntax is when the bool variable has a verb prefix like "is." I need the verb, be it "is," "can," "did," or else I need the == to provide the verb "equals." I might never break that habit, so I'll understand if you don't say 'hello' to me on the street. |
|||||
|
|
remind me of "boolean madness code", its like these
Instead of:
|
||||
|
|
|
Personally, I strongly dislike the way to say "not" in C based languages. That little exclamation mark is too easy to overlook. Hence I write it out in full:
After reading that for a while, I want symmetry too with
So consider it an artifact of C using |
|||||||||
|
|
It depends on the language, but it's usually a bad idea... In C, never do this. It's too easy to find a situation where the value you are testing is not false (non-zero), but also not equal to the single value defined as "true". In Ruby, do this only if you are absolutely certain that you want to fail on everything except Boolean true. In C++ and other static languages with a bool type, it's redundant, and can lead to programming errors when you mis-type |
|||||
|
|
You think that's bad? How about:
|
|||||
|
|
I prefer
or
too, but I'm afraid that bringing it up would piss people off, so my advice is to forget it. Sorry! |
|||||||||
|
|
you should tell him that he is doing it wrong. its if (true == someBool) { } if he ever forget one = he is in big trouble in his writing style. |
||||
|
|
|
How about
WHY IS THAT A STRING?! |
|||||
|
|
I write code like that! Here is why: "if bla == true" reads like a sentence, where as "if bla" does not in many cases. It just sounds wrong, when READING actual code. Also the compiler warns about assignments in if blocks, so there is really no danger in using == true. (confusing it with =) Also do guys that don't write "== true", use that "!()" for "== false"? I find it really ugly. And if you use "== false", it is only very consistent to also use "== true", instead of having two distinct ways of verifying truth. |
|||||
|
|
Remember you are working as part of a team, so you need to work these things out together. "Plays nice with others" is still an important personality trait even after elementary school :) |
||||
|
|
|
Generally will omit the '== true', but it's hardly worth even a minute discussing it unless you have it included in your team's coding standards. |
|||||
|
|
While I agree as a mainly C# developer, I can't say this is always the case. For instance, in Javascript, the === will perform type coalescence. So assuming var x = 3 then:
while
I guess that's different than == since even in JS I wouldn't use if(x == true) but just something to think about. This sort of touches on another point though which has come up in my office:
In C#, bool b; would be enough and would initalize b to false. However, it is more explicit to write the above line and anyway should be ripped out by the compiler during optimization. So I guess my point is it's not always so obvious what is and isn't good practice and a lot of it boils down to preference as well as language features/quirks. |
||||
|
|
|
Ah yes, but what if the variable is nullable? (bool?) Some languages (C#) will require and cast or comparison with 'true'.
|
||||
|
|
|
The young know the rules, but the old know the exceptions ;) In latest
If tristate is not a problem, then there usually should not be a reason to compare something to
Here Regardless of the language, any time you find yourself comparing something to |
||||
|
|
|
Such coding would have rubbed me the wrong way before too. Although your example identifier is named "someBool", using that coding style inadvertently on a variable which wasn't guaranteed to be a boolean could have unintended behavior. If the value of "someBool" isn't exactly "true" or "false", the result will be false. I encountered a very subtle bug this past year caused by such a coding style which was difficult to identify because one's eyes gloss over such constructs. You'd think, "How could it be wrong?" The same holds true for such well-understood expressions as "(var)" or "(!var)", that you read or code them without verifying their behavior. So I've introduced a couple of coding standards to reduce the existence of such bugs in the codebase and the likelihood that such subtle bugs will accidentally creep in sometime in the future.
By cleaning up code not conforming to the new style, I've identified and corrected a few more instances of such subtle bugs. |
|||||
|
|
Had to use this all the time in ActionScript 2 (admittedly now a dead language), because:
So it was almost always best to be specific. |
||||
|
|
|
I agree. It's a redundant construction, specially in strong typed languages. To add another misuse of booleans, I have found this kind of construction a bunch of times in Javascript, (specially at some spaghetti-like monster functions, as in 100+ lines):
It seems, that due to the lack of an strict type definition in this language, some programmers prefer not have to be messing around with the |
||||
|
|
|
I have a colleague who will have some code like this:
And then, for some sort of test/debugging, he will wish to not call this block so he'll change it to:
And then occasionally he'll change it to:
The worst thing is, this type of debugging has rubbed off on me on occasion and is really bad for other developers to read! |
||||
|
|
|
I'd say consistency is king in a codebase. So you should use the style, that is mostly used in your organization. Try to make your preferred style part of official company coding guidelines. If it's already in the guidelines, just follow it. (That being said, it would also really annoy me - but probably not enough to make a big deal out of it). |
||||
|
|
|
I prefer not placing the extra == true, but sometimes I accidentally include it and don't notice it. The person may not have noticed and accidentally placed them. I reread my code and sometimes notice that I placed the extra == true so I remove that == true. I sometimes don't notice it and would happily welcome someone telling me I placed it redundantly. |
||||
|
|
if (some_flag == true)but the implicitif (is_something)orif (has_something). Note the variable names. – FredOverflow Oct 18 '10 at 21:44someBool == trueis also boolean, so by the same logic it should beif ((someBool == true) == true). – Mike Seymour Oct 19 '10 at 0:00$var = (bool) some_expression. And in most cases, it won't even matter as PHP will do the necessary conversions dynamically. – waiwai933 Oct 19 '10 at 3:15