Remix.run Logo
0x20cowboy 16 hours ago

I agree with the spirit of this, but I’d go one more step with the example:

export function clamp(value: number, min: number, max: number): number { return }

That is just adding an extra jump and entry on the callstack where you could just have done:

Math.min(Math.max(value, min), max);

Where you need it.

(The example used was probably just for illustration though)

pigbearpig 15 hours ago | parent | next [-]

The author does acknowledge that in the "How it Should Be" section.

chii 15 hours ago | parent | prev [-]

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

xmprt 15 hours ago | parent [-]

Maybe it's different since you're not native English but clamp is as intuitive for me as something like ceil or floor methods. It might not be as simple for someone who is ESL, probably because no major programming languages that I know of include clamp in their standard library like they do with min, max, ceil, floor.

I'm also coming from being a Go programmer where the native library is very barebones so a lot of custom utility methods like this are inevitable.

aero_code 14 hours ago | parent | next [-]

FYI, some major programming languages have clamp:

C++: https://en.cppreference.com/w/cpp/algorithm/clamp.html

.NET/C#: https://learn.microsoft.com/en-us/dotnet/api/system.math.cla...

Java: https://docs.oracle.com/en/java/javase/24/docs/api/java.base...

Ruby: https://ruby-doc.org/core-2.4.0/Comparable.html#method-i-cla...

Rust: https://doc.rust-lang.org/stable/std/primitive.f64.html#meth...

webstrand 14 hours ago | parent [-]

Even CSS has clamp. I have to periodically remind myself that Math.clamp is missing from the stdlib, it's much easier to read when you need to use a bunch. Math.min(lower, Math.max(value, upper)) is fine for a one-off, but when you need multiple instances of clamp in the same expression the extra parentheses become troublesome.

zahlman 11 hours ago | parent [-]

I do find it strange how Python's min and max are builtin and don't require an import, but 'clamp' isn't provided at all. When I look at https://stackoverflow.com/questions/4092528 I feel the lack of an "obvious way to do it", since the simple approach has variant spellings and there are always people trying to be clever about it.

TRiG_Ireland 3 hours ago | parent | prev [-]

I am a native English speaker, and I've never heard of "clamp" before. Admittedly, most of my programming experience is in PHP, and I've rarely done anything with much maths.