by Zelphyr · 5y ago · 0 comments
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?!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. if (my_var == 'some value') { ... }
which is not the same as commenting it out entirely.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.
Git blame also doesn't help when the history gets truncated for performance reasons.
But overall, if commented code ends up in version control, it probably could have been removed.
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 sillyRight? 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 }
if (my_var == 'some value') { ... }