posted 10/25/2010 by RobertHarris - Views: [1697]
Why the if( isValid == true )? The sample code snippet below is very common, but why?
bool isValid = false;
if( isValid == true )
{
// Do some processing...
}
else
// Do some other processing...
}Understand that because of the type of the variable, there is no need for the "== true" or "== false" comparison. As the evaluation of the "if" must resolve to a boolean result and in conjunction with the value of boolean variable the runtime will route the execution to the appropriate conditional block without the need for the boolean comparison. Now I understand for the beginning programmer the verbosity of the statement may be needed for better readability. However, for a mid-level or higher developer, the verbosity shouldn't be needed. This also leads me to the next point...Variable naming convention. From a personal standpoint, I like prefixing a bool variable with the "is" moniker. Why? Well "is" should signal/represent a yes/no response to the question being asked, which is the expectation of the "if". This makes for better clarity of the variable’s purpose during code reviews, debugging, and cuts down the verbosity. Which brings me full circle. Why the if( isValid == true )?
Understand that because of the type of the variable, there is no need for the "== true" or "== false" comparison. As the evaluation of the "if" must resolve to a boolean result and in conjunction with the value of boolean variable the runtime will route the execution to the appropriate conditional block without the need for the boolean comparison. Now I understand for the beginning programmer the verbosity of the statement may be needed for better readability. However, for a mid-level or higher developer, the verbosity shouldn't be needed. This also leads me to the next point...Variable naming convention. From a personal standpoint, I like prefixing a bool variable with the "is" moniker. Why? Well "is" should signal/represent a yes/no response to the question being asked, which is the expectation of the "if". This makes for better clarity of the variable’s purpose during code reviews, debugging, and cuts down the verbosity. Which brings me full circle. Why the if( isValid == true )?
I like it. I would have to say the verbose coding is due to readability. Sure... a mid or senior level coder can see the obvious, but what happens when the only coder left standing is one with little experience? This increased readability increases maintainability, which in most cases just makes sense. Good post Rob!
Aahhhh, good point Brian. But, if the only developer left standing has little experience, then the company that employs the inexperienced has bigger issues. Also should a developer with little experience be set free in developing a piece of software without the guidance of someone with more experience? That should never take place. Secondly, code reviews for an inexperienced developer should be done regularly so that he or she can improve their development style, personality and skillsets. Also, in a development environment there should be some standards set in place for the inexperienced developer to refer to as a guide...i.e. variable naming convention, casing, etc. So, if all of those things are in place then the need for verbosity should be alleviated.
Also, not to mention the understanding of data types are essential to development. It's like knowing that a screwdriver's purpose is to assist the user in turning a screw in or out. Not to be used as a chisel.
I concur on all points.