Keep Those Boolean Conditionals Simple

In this post, let's talk about not overthinking booleans.

Dave Brock
Dave Brock

When working on legacy projects (or even new ones!) you are bound to come across code that is interesting—and trust me, your colleagues will say the same about your code from time to time!

There’s always a balance between terseness and readability, and sometimes you can go overboard. Here’s one I see sometimes that I don’t particularly enjoy.

// C# syntax
public bool CheckABoolean(int a)
{
    if (a > 42)
        return true;
    else
        return false;
}

Instead, do this. Your eyes and your colleagues will thank you.

// C# syntax
public bool CheckABoolean(int a)
{
    return a > 42;
}
CSharpQuick Tips

Consider subscribing to The .NET Stacks, my free weekly newsletter. I write about news and trends, interview community leaders, and catch you up fast. (No spam, ever, and unsubscribe whenever you want.)


    Consider subscribing to The .NET Stacks, my free weekly newsletter. I write about news and trends, interview community leaders, and catch you up fast. (No spam, ever, and unsubscribe whenever you want.)