Structuring: Early Returns(Meta)
In a nutshell, I think this whole disagreement is a distraction. Generally when I see this debate come up it is in the context of some functions that are trying to do too much. Chances are I would ask for that function to be broken down into smaller functions and given more meaningful names. Then by the time we get to that point it will be more obvious if an if/elseIf/else block is better or if early returns are better. Let’s look at an abstract view of this, assuming the function has already been distilled into smaller functions.
What I want to demonstrate here is: IF you need your “Happy Path” protected by a “Positive Test” AND you are interested in logging “Unknown Unhappy Paths” then you should use if/else clauses, otherwise you might be better off with a simple early return.tags: Programing - CodingStyle - ItDepends