Hi, let’s use early-return on this arguments stack:
function setAge(age) {
if (!age) {
return;
}
this.age = age;
}
This actually does not match the early-return pattern because the function is void. So indeed you can do a simple if(age)
and the whole thing is right and does not really break the early-return pattern.
About Mark Kegel, early-return does not mean 15 early-returns per function, so rather than a if too small, if odd, if too big, you should use if not valid calling an other function then you can still customize the exception to reflect the reason of the invalidity. This way, you also reduce function length and cyclomatic complexity, then you delegate the logic that is too noisy to be in your upper function.
Once you cleared those cases off the way, following other best practices (chunk the code into atomic pieces, auto-documented function names), then there are no longer so much reasons not to use early-return. ;)