▲ | chii 15 hours ago | ||||||||||||||||||||||||||||||||||||||||
> you could just have done ... i disagree with this particular example - it's actually a good use case for being a utility maths function in a library. I mean, the very `min()` and `max()` function you used in the illustration could also have been used as an example, if you use the same logic! | |||||||||||||||||||||||||||||||||||||||||
▲ | mewpmewp2 15 hours ago | parent [-] | ||||||||||||||||||||||||||||||||||||||||
I'm not native English, but I've done coding for way more than 10+ years, yet I would be able to understand and read quicker Math.min(Math.max(value, min), max) compared to registering in my brain what exactly "clamp" does. In terms of DRY and cleanliness, yes "clamp" sounds awesome. But in terms of practicality and quick understanding? Math.min and Math.max there is such a frequent pattern that brain immediately understands what is being tried to do there, as opposed to exact meaning of "clamp" to me. It may be just me though, clamp is not a word I frequently hear in English, and I see it in code sometimes, but not frequently enough to consciously register what it does as fast in my brain. Despite seeing code for probably 8h+ a day for the past 10 years. If it was in std lib of JS, maybe then it would be better. Like there is some sort of balance line on how frequently something is used and whether it's REALLY worth to abstract it. If it's in stdlib, use it, if it's not use it if truly it's pretty much always used. | |||||||||||||||||||||||||||||||||||||||||
|