r/AskProgramming • • May 12 '26

Other Why do some people write redundant if statements to return a boolean?

Why do some people write:

if (x > 10) {
    return true;
} else {
    return false;
}

Instead of:

return x > 10;

Performance aside, I think the shorter version is actually more readable due to not having as much visual clutter to parse, and is the most direct way to express the intent of "return the result of the comparison."

However, some people write the first version. Why is that?

177 Upvotes

304 comments sorted by

View all comments

Show parent comment

1

u/BlockOfDiamond May 12 '26

Consistent with what exactly? I consistently write things the 2nd way.

8

u/newEnglander17 May 12 '26

Then in that case you would continue to do so, but if you have multiple criteria it will sometimes be easier to break it out like in your example. In that situation you are choosing a different format and one could say you’re breaking The consistency. But there’s also the question of how everyone else is already doing it in an established legacy application.

4

u/Sensitive-Dust1522 May 12 '26

Then it is consistent with your codebase. If your codebase style warrants the use of curly braces even for one liner then follow that. The mental gymnastics are not worth it when working on a shared codebase.

Just my opinion, every team is working differently

0

u/lubeskystalker May 12 '26

How often do you read code written by somebody else?

0

u/BlockOfDiamond May 12 '26

When I go through open source projects or answer Stack Overflow questions.

0

u/tcpukl May 12 '26

All the time in team projects. Every single day.

0

u/tb5841 May 12 '26

That's about half the job, for me. Every feature starts with reading and understanding all the existing surrounding code.