Friday, March 13, 2009

NEVER compare against TRUE or FALSE or True or False in an "if" statement.

i am using the VS 2008 C++ Compiler, just if you need to know. 
Well i do a call on PathIsDirectory and if i check it against 
a "if(PathIsDirectory(szDir)== TRUE)" i get a FALSE and if 
i just do this (the szDir is valid!) if(PathIsDirectory(szDir))" i get 
a TRUE. So my conclusion here is, that the declaration for this 
function like: 

LWSTDAPI_(BOOL) PathIsDirectoryA(__in LPCSTR pszPath); 
LWSTDAPI_(BOOL) PathIsDirectoryW(__in LPCWSTR pszPath); 

is taking it strictly agains this from MSDN: 

"Return Value 
Returns TRUE if the path is a valid directory, or FALSE otherwise. " 

WRONG! 

---------------------------------------------------------------------------------------------

Others have hinted at the solution, but I'd like to state this as a general 
"good programming" rule:  NEVER compare against TRUE or FALSE or True or 
False in an "if" statement.  Boolean constants should be used in assignment 
statements, and that's about it. 

You wouldn't write "if (SUCCEEDED(hr) == TRUE)"; this is the same thing. 
PathIsDirectory returns a boolean value that should be used in a boolean 
context: 
    if( PathIsDirectory(xxx) ) 
or 
    if( !PathIsDirectory(xxx) ) 

No comments: