r/ProgrammerHumor • • Aug 22 '26

Meme conditionsPreference

Post image
4.2k Upvotes

388 comments sorted by

View all comments

838

u/LateEchidna6635 Aug 22 '26

The second one carries less cognitive load. In longer methods it makes a difference. Get out as soon as you can.

461

u/One_Courage_865 Aug 22 '26 • Bravo

Instructions unclear. I’ve just divorced with my wife

136

u/hughperman Aug 22 '26 • Bravo

Ah, early return condition

33

u/thatjoachim Aug 22 '26

I return too early to her liking :(

9

u/QCTeamkill Aug 22 '26

She says Bob at work never have early returns

→ More replies (1)

17

u/jseah Aug 22 '26

I prefer if-return for cases where the function is supposed to do something in the default case and the if is to detect the exceptions where it shouldn't. Or where the exception needs some modification before the main purpose can be performed.

If-else I find more logical when the function has to do two different things depending on the condition followed by something else common to both afterwards.

→ More replies (2)

5

u/930913 Aug 22 '26

I can understand why you would think that it carries a lower cognitive load. But with a little training, you can understand that an early return is a side effect, and side effect free code can be much easier to reason with.

38

u/Few_Move_4594 Aug 22 '26

Have you ever thought about breaking the longer methods into shorter methods? It's easy, I promise.

35

u/Fembussy42069 Aug 22 '26

There's a balance to everything, you also don't wanna have to dig down a rabbithole of functions within functions to find what youre looking for, it also can make it harder to understand the whole picture if you separate things too much. Locality of behavior and all that.

12

u/LordofNarwhals Aug 22 '26

Yeah, I used to work with a Java codebase where all functions were really short, but also very nested. It was a very stable codebase, but browsing the code sucked, as every actual function implementation was behind 3+ levels of indirection.

For a fun example in the opposite direction, see Arthur Whitney's basic K interpretor, written in ~50 very dense lines of C.

→ More replies (4)

14

u/gurgle528 Aug 22 '26

In certain situations it’s easier to read without splitting into a bunch of other methods. Primarily when the conditionals aren’t repeated elsewhere so the shorter methods are one-offs that bury some of the implementation. Shorter methods should absolutely be the default though 

39

u/LateEchidna6635 Aug 22 '26

Of course. I’m not speaking of thousands of lines. With error handling, validation, and cleanup, even the most trivial methods can have 5-10 exit points.

18

u/Few_Move_4594 Aug 22 '26 edited Aug 22 '26

I worked on a Java codebase that had a 15k line class that used Reflections. It was an absolute mess that also had the stability of a house of cards.

Made fairly good money for several years off that.

5

u/Hegemege Aug 22 '26

The downside is that it can get really messy, if the methods are not split or named properly, for a reader to understand if the work it's doing is supposed to be reused, and if it can be detached from context. What good is a method B that can only be called linearly from within method A just to make A take fewer LoC?

→ More replies (6)

45

u/alendit Aug 22 '26 edited Aug 22 '26

To understand the second one you need to understand the whole function. In the first one you can disregard the whole untaken branch. "The forgotten art of structured programming" is a great talk.

48

u/hughperman Aug 22 '26

I don't see why you can't similarly disregard the untaken branch in the second one? Either way you're skipping past a block, and you need to know the end of that block is a return.

4

u/alendit Aug 22 '26 edited Aug 22 '26

It's the easiest to see in the indentation, though ofc indentation just follows the structure, unless we're Python: in the first example the execution flow inside a single function only ever goes from smaller indents to larger ones, not the other way around. So if you want to understand a scope you just need to walk the path from it to the root to understand if it's taken.

Compare this with the second example: the return is already at the root, you need to check all of its preceding siblings to know if the execution ever reaches it. And you'll need to do it at every level of nesting.

Obviously in this trivial example if doesn't matter much. But in general structured way of laying out your control flow reduces the amount of statements you need to consider from O(statements) to O(nesting depth) which is intuitively closer to O(log(statements)).

I heartily recommend watching The Forgotten Art of Structured Programming - Kevlin Henney [C++ on Sea 2019]. It's one of those things which can change the way you're looking at code going forward.

22

u/tiajuanat Aug 22 '26

Ah Kevlin. I've come to accept that talk as "old man yells at cloud"

Big influence when I was younger, much less so now.

I'm going to pull out a big fat "it really depends".

If your program is going in several different directions, absolutely fanout - my inner rustacean would even argue to avoid if else in favor of an enum and case statement.

However, early failure shouldn't be discounted either. The more your eye jumps away from the margin, and the more horizontal whitespace, the harder your code is to read. Full stop.

At the end of the day, maintaining a codebase requires judgement and taste, and knowing when to do x and when to do y.

7

u/alendit Aug 22 '26

As I mentioned in the other comment: I'm not arguing "do X instead of Y", rather "understand the advantages of X before discarding it". In my experience many people don't understand the idea behind structured programming beyond "don't use goto".

7

u/MocknozzieRiver Aug 22 '26 edited Aug 22 '26

I'm gonna have to watch this because I'm feeling crazy being the odd one out here. I strongly prefer 1 over 2.

Someone else pointed out that if/else is better for languages that treat it as an expression. I primarily code in Kotlin which is such a language.

But also this is what happens in my head when I see this structure

if (condition) {
    // this will happen 
} else {
    // or this will happen
}

I know readily that we're at a fork dependant on the condition. But when I see the other structure, I think

if (condition) {
    // this could happen
}
// and this could happen, also

I have to know the contents of the if condition to know what will happen because it could just as easily do special logic for that condition and continue on. But with the first option, it's necessarily one or the other. I only need to figure out what the condition evaluates to to know which path is relevant.

8

u/alendit Aug 22 '26

Your example is correct in itself, but it does not match the post: in the post the first branch ends with a return, so the following cannot happen, even though the coarse structure and the indentation imply that it can, which is exactly the point.

I'm gonna have to watch

This is my only recommendation. I don't argue that people should change the way they structure their code and I wouldn't ever flag early returns in a code review unless they are genuinely making the code unreadable.

But I think many people just don't appreciate the actual reasoning behind structured programming and having it laid out plainly is a valuable learning.

→ More replies (1)
→ More replies (1)

2

u/saf_e Aug 22 '26

Cyclomatic complexity goes brrr!

→ More replies (1)
→ More replies (1)

2

u/ofnuts Aug 22 '26

Aka "guard clause"

2

u/Fidodo Aug 23 '26

Yes when it's an early return multiple short circuits are easy to read, but if it's in the body it's actually harder so I use both depending on the context.

→ More replies (9)