| >a big function with many early returns And how would putting these early returns in a gigantic if condition or several increasingly indented if blocks fix these bugs? It will not fix any bugs since both forms are equivalent. >early returns are gotos All types of control flow can be represented with goto. >gotos are harder to follow Harder to follow than what? The majority of programmers are familiar with following gotos by using an abstraction over it such as using loops or if blocks. >You can't move easily (think copy/paste) code than has early returns If code is able to be moved to or be copied into another part of the codebase, this is a signal that the piece of code should be put in a function. If you are talking about using a subset of code following early returns, then you do run into the problem in not knowing what subset of early returns (aka preconditions) that are needed to bring be in a new function. This is the same with using large or nested if blocks. >Early returns are artifacts of the assembly jump instruction By this logic all control flow constructs are artifacts of that. Regardless whether or not you use early returns, you get equivalent assembly code after compilation. >it makes no sense to have jumps in [a high level] language I disagree. It makes perfect sense to have jumps in a high level language. A high level language should allow you to write abstractions over these jumps though. This way you can name and reuse patterns of jumps in your program.
In addition you can think of early returns as a high level construct too instead of a low level one. You can think of if blocks as a monad. If we take a look at the article and used nested if blocks you could think of the type of the function as If If List (we can imagine that there is an interpreter behind the scene to convert this into a plain List). Since If is a monad it means we can do the join operation. This would turn it into a single If List. The obvious way to do this transformation by hand to the source file it would be equivalent to merging both if statements into a single one. If the else statements would return different results you would need to make sure you return the value associated with how far into the combined condition you get. Doing it this way is a little awkward. Also remember that monads give us the bind operation too. If we manually transform the source into using the bind operation we actually get the same thing as using early returns. Using early returns is a much clearer implementation of handling the conceptional If monad. By using the If monad we are only having to deal with a single if statement / condition at a time. >Things becomes so much more expressive I beg to differ. They are equally expressive, but early returns allow you to mention the preconditions / special cases all at the beginning of your function. >Early returns is one of the big pillars of bad inexpressive entangled convoluted programming I'm not sure how early returns make something entangled. Since they can only be at the beginning of a function, they can't really be entangled in your whole function. >And what is people reason for using early returns? It makes less indentations Yes, this is a side effect of being a monad. It means we can focus on a single if statement at a time instead of focusing on a bunch of nested if statements at the same time. This is the same thing as when javascript developers realize that converting callbacks that call callbacks, etc into using promises reduces the nesting that is needed. In summary, I am not convinced from your argument that early returns are bad since they are an abstraction over assembly. |