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.
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?