9

So I've been maintaining our company's web products for a few years now with a great senior dev, but why would it ever make sense to have a

bool somebool = returnsBool();
if (somebool == true)
...

WTF?!?! I still fine them in the code to this day.

Comments
  • 7
    Clearly this is remnants of an attempt to solve the grand theorum of truthiness
  • 1
    I remember doing this when I was a supern00b.

    Is there something wrong with:

    if (returnsBool()) {
    //...
    }

    ?

    I suppose the only possible reason to use this:

    bool somebool = returnsBool();
    if (somebool) {
    //...
    }

    ... is if the result of retunsBool() will be used many times and the runtime of that method is not quite the fastest. So store the returned bool in a var to avoid multiple calls. Especially if that result isn't subject to change during runtime. Otherwise... WHY
  • 4
    @corscheid also "somebool == true" is NEVER justifyiable
  • 4
    I have seen it many times even from devs that should know better so I just write it of as temporary mental meltdown ;)
  • 0
    I have to admit I just wrote the extract thing yesterday, I was helping a friend with his arduino project, he was really confused when I was calling a function within an if. So I had to sin
  • 0
Add Comment