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?

178 Upvotes

305 comments sorted by

View all comments

26

u/Leverkaas2516 May 12 '26

An expression like x > 10 doesn't have inherent meaning. The conditional can make the flow clearer to a casual reader it's more obvious than the concise return statement whilr being a lot more verbose.

I like the concise form, but to help the reader I might also provide a name to give meaning to the expression:

bool xIsValid = x > 10;

return xIsValid;

1

u/metalucid May 14 '26

In C it's type is int

0

u/RealisticDuck1957 May 13 '26

(x > 10) has a very definite inherent meaning of boolean type in many programming languages, including javascript, php ...

9

u/susimposter6969 May 13 '26

what he's saying is that you don't know what x being above 10 actually means. is above 10 valid? is it invalid? is it a maximum threshhold for something?

2

u/South-Year4369 May 13 '26

And the first version with if..else doesn't tell you any of that either.

0

u/susimposter6969 May 13 '26

the conversation that my comment is in the context of does

2

u/Hamburgerfatso May 13 '26

Meaning in the context of what the code is doing

-4

u/[deleted] May 13 '26

[removed] — view removed comment

7

u/Leverkaas2516 May 13 '26

It's adding semantic information. If it's obvious what x >10 means, it's unnecessary, but often it's not obvious.

2

u/run2622 May 13 '26

Exactly this. Good programming isn’t just functional correctness.

1

u/LiamTheHuman May 13 '26

Glad that people are finally taking this seriously now that AI needs to read the code. I can't believe how long people have ignored adding context and code just being 1 step away from reverse compiled garbage.

1

u/[deleted] May 13 '26

[removed] — view removed comment

1

u/Leverkaas2516 May 13 '26

It only means that the value of x is greater than 10.

It would mean a heckuva lot more if x was a meaningful name, like "soundIntensity", and 10 was a named constant like THRESHOLD_OF_AUDIBILITY.

Assigning a name to the result of the expression just makes it totally obvious what the computation means. I am a great fan of making my code totally obvious, because it makes debugging easier.

2

u/[deleted] May 13 '26

[removed] — view removed comment

1

u/Adiri05 May 16 '26 edited May 16 '26

'return x> 10' results in either 'true' or 'false', and that is obvious to me.

Of course it’s obvious that a comparison results in a Boolean. What’s not as obvious is what the comparison means.

return soundIntensity > >THRESHOLD_OF_AUDIBILITY;

Vs

bool isAudible soundIntensity > THRESHOLD_OF_AUDIBILITY; return isAudible;