32

I have previously seen this in a production code base. The same code base included nested if statements (20+ conditions)...

If (condition == false) {
return false;
} else if (condition == true) {
return true;
}

Comments
  • 3
    Why not return condition? I am that lazy that I won't write that much useless code.
  • 8
    It's better than

    If (condition == false) {

    return true;

    } else if (condition == true) {

    return false;

    }
  • 1
    I don't understand why that's bad, was it in a function? If so, the return true/false might then be used later if you understand me right.
  • 2
    @pmso if that's in a function called returnTrueIfNotCondition and you call that condition, you want it to return true if false.
  • 2
    @RAZERZ return !condition;?
  • 1
    @RAZERZ return !condition;
    It doesn't really make sense to use if statements for this imo
  • 1
    In untyped languages, it is possible to have none (python), undefined (JavaScript) or something entirely different(like a string)

    Although it depends on the situation and language.
  • 2
    @Noobish for example, if you want to check if user input contains a string and it does, you would want it to return true from the function, or?
  • 2
    @RAZERZ if(scanner.hasNext())
  • 2
    @Noobish and then return true, right? But that doesn't look like it would work in a lot of languages
  • 2
    @RAZERZ yeah, I guess it depends on the language. I mostly work in Java so I'm not too sure about other languages tbh
  • 3
    @RAZERZ Actually I'm dumb just return scanner.hasNext(); still no need for if statement
  • 2
    The condition was a simple boolean.
    The language C#.
    It was indeed in a function and I'm not sure why they couldn't simply write:

    Return condition:
  • 1
    @BigAlan09 no reason whatsoever.
    It may have happened over time though (multiple refactoring and people not quite checking the whole code)
  • 0
    Just use a simple switch statement...
Add Comment