I don’t get it. How do early returns not convey assumptions that can be made in the rest of the function?
> Returning in the start is as anti-pattern for me as goto in the middle. both of them leave you with lines of code that only a debug session (in mind or in action) can tell you what lines will be called.
This is really confusing to me. What do you mean by “in mind or in action”?
In any case, if you have runtime conditions you can’t tell what code will be executed in general just by looking at the code regardless of whether you’re looking at nested code or code with early returns.
> I will choose nested code any-day if that means i can simply follow what `if`s are relevant rather than guessing.
Are the conditions for early returns not relevant or something? What about them leaves you guessing?
If I'm extracting code to be available for more general use, I usually prefer to try to forget the preconditions of the source function and apply just the preconditions that are applicable for the now-standalone code. In that case, once I'm done with the extraction I'd need to ensure that the preconditions of the source function match the preconditions of the extracted code, and nesting doesn't really affect that; if anything, having the preconditions all up front would make things a bit easier by having everything in one place.
As for moving blocks of code around, I can't say I've had much need to move code across "precondition domains", for lack of a better term. Perhaps I just haven't run across the right situation yet.
Do you mind expanding a bit more on this? From what I understand, if you have early returns the only way you can get into the rest of the function is if all of the conditions pass, not any arbitrary combination of them.
Also, do you have an example of a more complex function with early functions that you find difficult to read?
That way there is no need to scroll up for preconditions, be it a nested tree or some returns at the start.
And when you move a small piece of code, you're told exactly what you should be thinking about, you need to decide what to do with that assert that represents an assumption.
This number is why nesting code is almost always heinous. Once the number of nested conditionals exceeds working memory, your monkey brain cannot hold the state and drops all the variables.
Deeply nested statements is my number one code smell, and can almost always be easily refactored.
[0] https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus...
1. You are guarding for errors or invalid states - you should use early returns or thrown exceptions because these are unexpected states.
2. Your business logic has to branch - you should use a nesting if (or various other options that aren't in scope here, like double dispatch, pattern matching or polymorphism).
Anyhow, with both approaches, nested or early return, the reader has to read the function from the beginning (or read back until the relevant part) to grasp when exactly a code portion will be executed. In both cases, all if's are relevant, there's no guessing.
if (product.hasPrice){ if (someOtherCondition){ Cart cart = cartService.getCartForCurrentUser(); cartService.addToCart(product,1); } }
to
if(canSellProduct(product)){ addToCurrentCart(product) }
But the article is a good approach to thinking about a multitude of issues. Even if we nitpick or disagree with specific points, the exercise of thinking through it all will certainly result in improved code.
I honestly cannot think of a reasonable argument for why constant values should not be in static constants or enums. For one, why would you want to re-type the value over and over? And two, it only takes one typo in one manually typed value to introduce a frustrating bug! It's such an easy bug to avoid and costs almost zero to do correctly! Honestly, devs manually typing values that are effectively consts is one of my biggest pet peeves, you're just creating easily avoidable problems for yourself and your team :).
My personal opinion is that values should be in constant values if:
- Its repeated more than once or referenced outside of the file
- The constant value is a "magic number" where the meaning of the value isn't obvious to outsider
If the value is used only once and the meaning of the value is clear, I think extracting a constant out is needless indirection.
For example:
if (flags & 45676 == 0) {}
is a magic number that definitely should be a constant, even if its only used once: if (flags & ROLE_ADMIN == 0) {}
However something like: pool.setDefaultTimeoutMillis(1000);
is perfectly obvious in context IMO
Returning in the start is as anti-pattern for me as goto in the middle. both of them leave you with lines of code that only a debug session (in mind or in action) can tell you what lines will be called. I will choose nested code any-day if that means i can simply follow what `if`s are relevant rather than guessing.