Just wanted to share this code gem

You never need to compare to true.

I spotted this in some ASP.NET today:


if (confirm("Are you sure?")==true)
return true;
else
return false;

You never need to compare something to true though:


if (confirm("Are you sure?"))
return true;
else
return false;

And the whole if-boolean-then-boolean construct can be refactored out:


return confirm("Are you sure?")

It’s less to maintain, less to look at, and simpler to understand. That is all.

Leave a Reply

Your email address will not be published. Required fields are marked *