Why use boolean literals in logical tests?
Ok, I've seen this enough to assume there is some benefit or reason for this style.
When a condition must be tested in code it is not that uncommon for someone to have
code like this:
Code:
if ( var == true)
//operation
My question is this. Why not just use code like this:
Code:
if (var)
//operation
The clarity seems good for negating a condition better than using the "not" operator "!".
I think that:
is more readable than:
But of course it would be really confusing to see:
Code:
if ( (var == false) == true )
Which isn't too different IMO from
I mean "true" is the only condition that an "if" statement will trigger off of so to me it doesn't add too much value to clarify that.
I believe that some classes taught like "C" encourage explicit testing because the C language treats *anything* but zero as true which might not be logically what you are trying to do and there is no "bool" object which can only be true or false.
In C if a "TRUE" constant is defined as 1 and "FALSE" as 0 then testing for equality against the constant "TRUE" is more deterministic in many cases than using boolean logic. In C# and Java I haven't seen this *as much*.
Where library designers have provided good method names such as "File.exists" it looks particularly akward to include the "==true" test.
Does anyone know of any differences (in C#) between the two styles?
David