react-basic
react-basic copied to clipboard
Small improvement to memoize fn
Why is this an improvement?
Isn't it a bit cleaner?
Not to me. If arg is cached and nothing is to do, I'd like to get it out of my way first. That makes it more scalable as all changes would go below.
Effectively the same thing is happening as you've described: if the condition test results in false, there is nothing to do, the cached statement is returned, and you're on your merry way.
Unless there are concrete plans for this function to grow beyond its 3 lines of logic, this indeed is a cleaner version that doesn't repeat code (return), and scalability is a premature optimization.
Why would scalability be an issue with this improvement?
The original code requires less cognitive load when reading. Once you get one condition out of the way with an early return
, you avoid having to keep two conditions simultaneously in your head while reading the rest of the function (and making sure you understand it and it is bug-free). So "cleaner" (i.e. less repetitive) doesn't imply "easier to read" and "easier to debug".