Open tiagojdf opened 6 years ago
But in your example I feel you do just the opposite.
I'm not sure I see why it's "opposite"?
I can definitely agree that there's more logic in the function with all those extra conditionals. But the point I'm actually making is that the more verbose and explicit logic is easier to trace through than the implicit "oops I already exited, none of this runs" kind of logic of early-return.
I think a nicer example to make your point would be edge cases, where dealing with the edge case before the main logic can make it harder to understand
This is an interesting point, but it could go both ways. Typically, I am more likely to appreciate an early-return in the case where it's the sole exception and where it's the very first thing in the function. What I dislike most is that early-return might be anywhere strewn throughout.
I think you make a very solid point in that section when you say that:
But in your example I feel you do just the opposite. You add an extra variable that I need to keep track just to know if I should still execute the code to come. You say that:
I am not saying that you are wrong when you say that returning too often can make the function harder to read, but I would argue that in this example you actually do have all those points where you do need to return.
I think a nicer example to make your point would be edge cases, where dealing with the edge case before the main logic can make it harder to understand what it is you are trying to achieve with the function. (can't think of an actual example).
I would love if you could provide me with a clearer example :smiley: