Live data from Hacker News

Avoid Else, Return Early (2013)

blog.timoxley.com

21–30 of 601 posts

Re: Avoid Else, Return Early (2013)

#21
post #3

"removing a whole line and more braces" - this is really fighting the wrong enemy. Code should be written in a way it is more readable, not shorter.

I think the point OP’s trying to make is that reducing indentation levels makes code more readable.

I don’t agree with him about removing the braces, though. That way madness lies.

Re: Avoid Else, Return Early (2013)

#22
I think there's a lot of value to returning early out of functions when you hit an error condition. There's not much reason to go further if you're just completely hosed. (e.g. malformed arguments).

However, I don't think that coders should shy away from built-up result values for return. It makes the code more friendly to things like copying (I can feel the boos and hisses now), block restructuring, #ifdef'ing (or some equivalent), short-circuiting, and debugging. If you have one easy breakpoint to set that still has a stack so you can peek at a result, it's pretty darned convenient.

So, yes, sure, tend to error out first (this is not news), but don't just litter returns all over the place in code that's going to have to be used and modified by lots of people. The goals for practical code should revolve around the useful lifecycle of that code. Make it readable, portable, mutable, ibleable, ableible, etc.

In many cases, carrying a return value on the stack is a decent way to do this. In some contexts, particularly where branching is expensive, it can have performance implications as well.

Re: Avoid Else, Return Early (2013)

#23
This refactoring is so simple and obvious I don't even understand why some people don't write like this by default. I've had colleagues comment my PR on my "failed validation, return early" code saying "we generally strive to maintain one single point of return". I mean, why would you want only one return even?

Re: Avoid Else, Return Early (2013)

#24
I’ve always been a fan of refactoring deeply nested if/else into a more linear less nested sequence of if/return statements.

If the function spec is a sequence of complex AND checks which must be satisfied in order to do work, an unnested sequence of “if (!x) return” statements are logically equivalent, have less indentation, and I think it’s much cleaner and easier to read comment blocks above each IF explaining the reason why each check is there.

I also really like seeing the main body of the function which is doing the real work at be bottom without any nesting/conditionals around it at all. It helps you find it if it’s always down there at the end, rather than in the middle indented way to the right.

Re: Avoid Else, Return Early (2013)

#25
post #7

Avoid rules, use your judgment.

That's awful advice. Obviously _someone_ has to use their judgement to _create_ the rules. But if you have a large codebase that lots of people are interacting with, letting everyone "use their judgement" is going to create an unreadable mess of code, because you're going to have different patterns and approaches all over the place. It would be like writing a book and one paragraph is in English, the next in Spanish,…

Dogma

Re: Avoid Else, Return Early (2013)

#26
post #7

Avoid rules, use your judgment.

"Unless he is certain of doing as well, he will probably do best to follow the rules." - Strunk, The Elements of Style

Your advice is unfortunately common but misguided. Without a firm and intuitive grasp of "rules" and why they exist, one cannot build a framework for making decisions with which one can use their judgment. On top of that, most programmers aren't advanced enough to have more reliable judgment than "the rules" and few working groups can possibly survive a bunch of programmers "using their judgment" all over a nontrivial codebase even when those programmers are that advanced.

Re: Avoid Else, Return Early (2013)

#27
post #19

I switched to using this style in the last few years, but it only works well where the code is reasonably good quality otherwise, in particular with short methods and classes. If you're dealing with legacy code with massive procedural style methods having return statements scattered around makes things even more confusing.

Since it forces you to write short methods everything you add or change on the existing code will automatically improve the code quality. Also depends on your refactoring tools, C# with ReSharper can reliably refactor without side-effects.

Re: Avoid Else, Return Early (2013)

#28
Depends on different factors, you can't just say one is better than the other. E.g. a function call itself might be quite expensive.

It's important to know both patterns though, so you are able to choose depending on your situation.

Re: Avoid Else, Return Early (2013)

#29
This often becomes a non-issue if your functions are short anyway. Having this discussion can sometimes be a code smell in itself.

Obviously it's impossible to completely generalise over, but sometimes it's worth asking yourself if the function is doing too much. In saying that though, early returns are great for input validation.

Re: Avoid Else, Return Early (2013)

#30
post #21
post #3

"removing a whole line and more braces" - this is really fighting the wrong enemy. Code should be written in a way it is more readable, not shorter.

I think the point OP’s trying to make is that reducing indentation levels makes code more readable. I don’t agree with him about removing the braces, though. That way madness lies.

This particular refactor (changing `if (err) { return ... }` to `if (err) return ...`) didn't actually reduce indentation levels though. It only reduced the number of lines.
Post reply on HN