react-basic icon indicating copy to clipboard operation
react-basic copied to clipboard

Small improvement to memoize fn

Open mbonaci opened this issue 8 years ago • 6 comments

mbonaci avatar Oct 29 '16 11:10 mbonaci

Why is this an improvement?

dmitriz avatar Dec 07 '16 21:12 dmitriz

Isn't it a bit cleaner?

mbonaci avatar Dec 19 '16 12:12 mbonaci

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.

dmitriz avatar Dec 19 '16 12:12 dmitriz

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.

oogali avatar Jan 01 '17 13:01 oogali

Why would scalability be an issue with this improvement?

ulpian avatar Apr 06 '18 09:04 ulpian

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".

huyz avatar Apr 07 '18 22:04 huyz