70

Everytime I see
if (x.getSomeBoolean() == true) ...

I die a little bit inside.

Comments
  • 3
    if(x.booleanValue == (1 == 1))
  • 4
    if (x.booleanValue != (x.booleanValue == (1 ==2)))
  • 6
    Worst part is that in js and python, this can make sense
  • 3
    @eldamir in python it's capital T in True
  • 8
    I've been programming for years and I still do this. For me, it indicates that the value you're testing is specifically boolean. Since any non-zero value also evaluates to true, you can improve readability by doing this.
  • 0
    @divs didn't mean the syntax but the semantic construct
  • 1
    I've seen this recently. Drove me nuts...
  • 1
    This may seem silly but I try to avoid if-statements. Often there is actually no need to use them, as you can index into tables or make function calls instead. If you do that you can cache results and only recompute values when necessary. If you are not computing values, then you are instead relying on side effects, and although there are times when it's necessary, I've found that most often there is a better way to get efficient and simple code. You just need some library to manage the constructs you need for such data flow.
  • 0
    What if the boolean is nullable?
  • 0
    @eldamir yeah I got your point but it was a bit difficult for me as I was migrating from c++ and I made mistakes
  • 5
    If the code is a victim to bad naming, it's best to do this actually.

    For example:

    if(getFinished())

    Doesn't say anything. It's more informative to write

    if(getFinished()==true)

    even though it's redundant. It communicates type better and lets you understand what's going on very easily just by reading this line of code.

    A good practice, for me at least, is to have variables, getters, answer a question:

    if(isFinished())

    That says it all. Or if you really insist on using 'get':

    if(getIsFinished())

    Then surely, skip the "==true"
  • 2
    I work with elastic search (Java full text search engine) and they always do this:

    if(someBool == false) { ...

    I havent seen a single ! so far. I don't know why this annoys me so much
  • 1
    @uniquesmash it's more to read so it's worse
  • 1
    If in javascript, "if (x === true)" is very useful, and has a different meaning than "if (x)", i.e. "if (x == true)"
  • 2
    What I have seen is (arguably) worse

    x == y ? true : false
  • 1
    @rubyist haha agreed. Why would anyone do that? Welcome to devRant! :)
  • 0
    DOCTYPE = false;

    <!DOCTYPE html>
  • 1
    I only came here wondering how many statements would it require to terminate a life, but the comments became so relevant in terms of different scenarios in which it may be relevant.

    I really enjoy this community, always sharing knowledge.
  • 0
  • 3
    @devmg In JavaScript if (x) is not the same as if (x == true). Consider x = "test". It's truthy, but not == true.
  • 1
    @devios1 oh, always thought it'd try to parse the string too. Great catch, thanks!
Add Comment