3 ms·
I try to structure functions and validations like this in a early-return list at the top of a function. if(val <= someconstant) return; //not valid
by claytongulick 1y ago
I try to structure functions and validations like this in a early-return list at the top of a function.
if(val <= someconstant)
return; //not valid
if(!(condition2 || condition3))
return; //not allowed
...
The author mentions this technique as well.
I find it particularly useful in controller API functions because it makes the code a lot more auditable (any time I see the same set of conditions repeating a lot, I consider whether they are a good candidate for middleware).
I try to explain this to newer developers and they just don't get it, or give me eyerolls.
Maybe sending them this article will help.