Re: [RFC] Add clamp function
| From: | Kyle Katarn | Date: | Sun, 24 Aug 2025 14:27:15 +0000 |
| Subject: | Re: [RFC] Add clamp function | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128542@lists.php.net to get a copy of this message | ||
2025-08-22 at 22:29, Morgan <Weedpacket@varteg.nz> wrote:
> On 2025-08-23 05:29, Kyle Katarn wrote:
> >
> > Also from my point of view, min($max, max($min, $value)) has a counter-
> > intuitive reading when used to clamp a value.
>
> Well, that's trivial to deal with:
>
> min(max($min, $value), $max));
>
It's not just about the order of parameters, it's still not intuitive to
call max() with $min and min() with $max, you still have to think about
what it does step by step to make sense out of it, while clamp($value, min:
$min, $max) or clamp(min: $min, value: $value, max: $max) will provide a
syntax that immediately make sense for next reader.
Like the author of the RFC v1 also mentioned, min(max()) is inefficient
(see the inner complexity of those functions).