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?
I don't like the first option. Not only is it redundant, but a simple typo will introduce a bug.
Consider this
bool b = false;
if (b = true) {
Console.WriteLine("true");
}
Obviously the code will output "true" but that was probably not the intention of the programmer.
Fortunately tools like Resharper warns against this, but it compiles with the default settings (*).
Using the bool directly will remove the issue entirely.
(*) To be fair, VS also warns against this and if you turn on Warnings as errors it won't even compile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With