3 ms·
There are many reasons to forego readability, especially when writing a library: performance, compatibility, requirements, interpreter/compiler optimizations or
by beirut_bootleg 6y ago
There are many reasons to forego readability, especially when writing a library: performance, compatibility, requirements, interpreter/compiler optimizations or even cyclomatic complexity.
In lodash's case it might even be all of the above, although I can't speak for the intentions of the authors since there are no comments to guide readers through the process.
Note GP's link points to what looks like the v3 branch. Check out the latest implementation of clamp, with a few less if statements, and what looks like a NaN check using strict equality if you want your mind blown. https://github.com/lodash/lodash/blob/86a852fe763935bb64c12589df5391fd7d3bb14d/clamp.js#L22-L23 https://github.com/lodash/lodash/blob/86a852fe763935bb64c125...
- robocat 6y agoReading that code it looks to me that: clamp(null) returns 0 clamp(undefined) returns NaN clamp(1, NaN, NaN) returns 0 clamp(1) returns 0 clamp(1, 5, NaN) returns 5 JavaScript is hard to write safe code for.