Search code examples
c#language-agnosticcoding-styleboolean

Boolean types


During code review I discovered many places of our C# code that looks like this:

if(IsValid()) {
     return true;
}
else {
     return false;
}

or even "better":

return (IsValid()? true : false);

I always wondered why not just write the code like this:

return IsValid();

This is the way I would write this code. I ain't questioning the skills of the developers, but maybe trying to look into the developer's soul. Why would a developer favor more complex code and not a more simple and intuitive? Or maybe the reason is that it is hard to accept the Boolean type as the first-class citizen?


Solution

  • 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.