I always use If statement (In C#) as (1. Alternative);
if (IsSuccessed == true)
{
//
}
I know that there is no need to write “== true” as (2. Alternative));
if (IsSuccessed)
{
//
}
But, I use it because it is more readable and cause no performance issue. Of course, this is my choice and I know many software developers prefer first alternative. What is the best usage, and Why?
If the name of the boolean value makes it perfectly clear what it is, then I’d always opt for version 2. However, sometimes you’re stuck with a particularly obtuse variable name that you can’t change, at least, can’t change right now… Refactoring is all well and good, but I try and avoid refactoring too heavily when making functional changes to the code as well.
For example:
I’ve actually seen this particular example in production code and simplified it down to:
And I personally think that both examples are more readable (although arguably the first example may be on par with this one for difficulty of mental parsing) than:
Note: Yes, I know the variable is badly named, but changing it in the multitude of places that it was present was outside the scope of the change I was making due to the number of places if would affect.