31

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
  • 2
    Why not return condition? I am that lazy that I won't write that much useless code.
  • 7
    It's better than

    If (condition == false) {

    return true;

    } else if (condition == true) {

    return false;

    }
  • 0
    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.
  • 1
    @pmso if that's in a function called returnTrueIfNotCondition and you call that condition, you want it to return true if false.
  • 1
    @RAZERZ return !condition;?
  • 0
    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.
  • 1
    @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?
  • 1
    @Noobish and then return true, right? But that doesn't look like it would work in a lot of languages
  • 1
    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:
  • 0
    @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