← Back to context

Comment by Zelphyr

5 years ago

> There were conditionals that were impossible to hit

The number of times I've seen:

  if (my_var == 'some value' || true == false) { ... }

Why do people do this instead of commenting out the code?!

Because if you comment out the code you don't benefit any more from the compiler checking that the inner block is still legal, or from your IDE to do the refactoring operations you want. The code would bit rot much faster in a comment (or removed from the code base altogether).

The thing I really can't understand is why you would compare a boolean condition to `true` instead of checking the condition itself (in other words, writing `false` instead of `true == false`). Also, let me suggest to use `&&`, not `||`. And while we're at that, I would actually write:

    if (false && my_var == 'some value') { ... }

just in case operator `==` can have side effects or relevant computational cost in your language.

Just to clarify, what do you mean "instead of commenting out the code"? The above is equivalent to

  if (my_var == 'some value') { ... }

which is not the same as commenting it out entirely.

  • I think it may be a typo, and they meant "&&" instead of "||". That would be the equivalent of "if (false)", thus preventing that block of code from ever executing.

Why even comment out? We have version control for a reason

  • Version control is not immediately visible to future developers. You need to know that there is something to look for, particularly if it is a frequently changed file.

    • Really the only time a future developer will care about the old code is when there's a bug in the current code. (Or maybe an edge case like a request to "make it work like it used to in v1.0").

      Looking at history via "blame" is useful to see why a bug was introduced (was it fixing another bug, and if so, is your fix going to break that bug again?), and how long it's existed for.

      Leaving old code commented out doesn't help either with of those things. Unless maybe it's accompanied by lots of comments and date stamps, in which case you've not only re-invented a very crappy, half-baked version control system, but also made the code hard to read and work with.

      1 reply →

    • I kind of agree; I've never looked in a file's history to see if something had been solved previously. I don't recall looking in history more than a week back to restore some deleted code either. In practice, I just rewrite code instead, also with the mindset that I'm a better programmer today than I was yesterday, and that in my head at least, writing that bit of code is trivial.

      But overall, if commented code ends up in version control, it probably could have been removed.

  • I've been at enough companies that have decided to change version control without pulling over history that I have developed a trust issue with version control. It is a great tool when used correctly, but comments are even more resistance to certain kinds of corner cutting.

  • I guess I've been assuming they did that because they wanted to temporarily disable whatever was within the conditional block but, yeah, you're right. Just delete it if you don't want that to run. Either way, that code should never be committed.

I have used constructs like this in debugging / exploration phases (especially in languages without good debuggers, when print debugging is unavoidable or just faster). I'd be horribly embarrassed if they got committed. But... the snippet you posted isn't equivalent to a comment; `x or false` is equivalent to `x`. So it's actually equivalent to a commented-out short-circuit

    if (my_var == 'some value' || true == false) { ... } //if (my_var == 'some value') { ... }

    if (my_var == 'some value' || true == true) { ... } //if (true) { ... }

    if (my_var == 'some value' && true == false) { ... } //if (false) { ... }

That said... I would never use the comparisons `true == true` and `true == false`... that's just silly

  • > that's just silly

    Right? Wouldn't it be more readable to use just 'true' or 'false' instead of the comparisons, or is that not a feature in some languages? I don't understand what might be gained from the extra comparison, besides confusion.

    if (my_conditional || true) { etc }

That's not equivalent to commenting out the code block, it's just equivalent to

  if (my_var == 'some value') { ... }