Keep Those Boolean Conditionals Simple

less than 1 minute read

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

Tags: ,

Updated:



Level up with The .NET Stacks Newsletter

If you enjoy my content, consider subscribing to The .NET Stacks, my weekly newsletter. It isn't a link blast! I go in-depth on news and trends, interview leaders in the community, and allow you to catch up with one resource.

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

    Leave a comment