5 ms·
Speaking only to JS is there any reason to write it any other way outside of being clever or as a lambda for singular use? I definitely prefer this version. (As
by 52-6F-62 6y ago
Speaking only to JS is there any reason to write it any other way outside of being clever or as a lambda for singular use? I definitely prefer this version. (Assuming any necessary runtime checks are included for a given project)
- chasd00 6y agothose extra newline characters slow down the page load :)
- tobyhinloopen 6y agoYou’re joking right
- beirut_bootleg 6y agoThere 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.