In my experience, if (flag==true) is bad practice.
The first argument is academic:
If you have a bool flag , it is either true or false .
Now expression
(flag==true)
again, true or false - it is not more expressive, only redundant - flag cannot get "more truthful" or "more false" than it already is. This would be “clearer” only if it is not obvious. flag is logical, but there is a standard way to fix what works for all types: choose the best name.
Stretching this for no reason, the following would be “even better”:
((flag==true)==true)
The second argument is pragmatic and platform specific.
C and earlier versions of C ++ did not have a real "bool" type, so there are various conventions for flags, the most common of which is that nonzero is true. Often, an API returns a BOOL type based on an integer, but does not ensure that the return value is 0 or 1.
Some environments use the following definitions:
#define FALSE 0 #define TRUE (!FALSE)
good luck with if ((1==1) == TRUE)
In addition, some platforms use different values - for example, VARIANT_BOOL for VB interop is short , and VARIANT_TRUE is -1 .
When mixing libraries using these definitions, explicit comparisons to true can easily be a mistake disguised as good intentions. So, no need.
peterchen Jul 04 '09 at 20:06 2009-07-04 20:06
source share