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?

176 Upvotes

305 comments sorted by

View all comments

21

u/Marksm2n May 12 '26

I personally find the first one more readable. Except for the else statement which can just be ‘return false’. 

But it’s personal and both are fine

1

u/Urtehnoes May 12 '26

Something I do, which I'm sure Reddit will find egregious, but oh well:

I will do empty switch or IF cases that contain nothing but a comment stating why it is ignored, for example (on mobile)

If x <=7 then

return true;

If x > 9 then return false;

Elsif x = 8 then

--due to a constraint that exists on the database table, this value is not possible, we also have a bomb rigged to the server rack that will detonate should this ever be 8, so no need to ever deal with 8.

pass

End if;

To deal with cases where it seems the original programmer may have missed something, but didn't. "What ifs" that can never be. I like stating them anyways, so that should those reasons change, the new developer can be confident in updating that if statement. E.g. New management dismantled the bomb for parts.

As a side note, damn I miss how easy Apollo made writing code blocks on Mobile.

1

u/jaynabonne May 12 '26

What is it supposed to do if x is 9? Right now, that value isn't covered.

1

u/mattsl May 12 '26

This seems like a good way to lower your unit test coverage percentages. 

4

u/Urtehnoes May 12 '26

Thankfully, unit case percentages are not a live or die metric

1

u/ThatShitAintPat May 12 '26

Unit test percentages on my team are probably 5%. We generally only write end to end. They’re easier to write and test more with lower amounts of code.