Keep Those Boolean Conditionals Simple

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;
}

Level up with The .NET Stacks Newsletter

Consider subscribing to The .NET Stacks, my weekly newsletter. I write about news and trends, interview community leaders, and catch you up fast.

    I don't do spam and will never share your address. Unsubscribe at any time.