During code review I discovered many places of our C# code that looks like this:
if(IsValid()) {
return true;
}
else {
return false;
}
<
Distraction
I know it's occurred in my code before and I can trace that back to when I was interrupted or not paying attention while coding (I solely blame SO!)
Ignorance
Not knowing the better way to do it. We take it for granted that all programmers think logically, but that's not the case. Some coders are going purely on patterns of what they've seen before:
If (integerA == integerB) { //do special stuff }
//Given integer equality; boolean equality ought to look the same...
If (isValid() == true ) { //do special stuff }
Momentum
That's how someone has always done it and therefore that's how they continue to do it.
I even sometimes see this some of the legacy code I maintain:
bool retValue;
if (IsValid())
{
retValue = true;
}
else
{
retValue = false;
}
return retValue;
Are some programmers paid by the character?
Yes, you should do it as you say. These people are doing these overly verbose things because they first learned it that way, perhaps in CS 101, and it never occurs to them to go outside the space of what they know works to see if there is a better, easier way.
This does speak to their competence. Good programmers need to be a little more thoughtful and a lot less hidebound.
I think return IsValid();
is perfectly valid and readable code.
BTW, I would certainly slap anyone who writes (IsValid() ? true : false
) in the face. It's unnecessarily complicated.
PS. This is what svn blame
is designed for.