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:



Thank you for reading!


If you enjoy my content, consider subscribing to my weekly (and free!) newsletter called The .NET Stacks. I provide my perspective on .NET happenings and trends, interview folks in the community, and give you the resources you need to make you your best.


Buy me a coffeeBuy me a coffee

Leave a comment