markdown 在PHP中编写`clamp`函数的各种风格的比较

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 在PHP中编写`clamp`函数的各种风格的比较相关的知识,希望对你有一定的参考价值。

<?php

function clamp($value, $min, $max)
{
    if ($value < $min) {
        return $min;
    } elseif ($value > $max) {
        return $max;
    } else {
        return $value;
    }      
}
<?php

function clamp($value, $min, $max)
{
    if ($value < $min) return $min;
    if ($value > $max) return $max;
    return $value;
}
<?php

function clamp($value, $min, $max)
{
    if ($value < $min) {
        return $min;
    }
  
    if ($value > $max) {
        return $max;
    }

    return $value;
}
<?php

function clamp($value, $min, $max)
{
    if ($value < $min) {
        $value = $min;
    } elseif ($value > $max) {
        $value = $max;
    }      

    return $value;
}
<?php

function clamp($value, $min, $max)
{
    return min($max, max($min, $value));
}
<?php

function clamp($value, $min, $max)
{
    if ($value < $min) {
        $value = $min;
    }
  
    if ($value > $max) {
        $value = $max;
    }

    return $value;
}
<?php

function clamp($value, $min, $max)
{
    return $value < $min ? $min : ( $value > $max ? $max : $value );
}
## Introduction

PHP does not have a native "clamp" (or range constraint) function<sup>(1)</sup>.

As always, there are several ways to resolve this problem.

This repo contains several variations that all achieve the same thing: clamp a given value between a given maximum and minimum range.<sup>(2)</sup>

The question is: _which one has your preference and why?_

It is important to note that there is no right or wrong answer, all of the implementations are fully functional.
the goal is not to see which variation is more popular but to gain an understanding of _why_ a developer prefers one over the other.

## Concerns

When judging code, developers tend to look at the following things<sup>(3)</sup>:

- Amount of code
- Code complexity
- Execution speed
- Low fault tolerance (being bug free)
- Readability

Of these, only "Readability" is subjective. Complexity, fault tolerance, speed and size can all be measured.

- Full details on ammount of code [can be found here](https://gist.github.com/Potherca/0c10a67b12f553a13ebe62f208e97202).
- Full details on execution speed [can be found here](https://gist.github.com/Potherca/b9db0801cc0257715361361c9a05ec78).
- Full details on code complexity [can be found here](https://gist.github.com/Potherca/8379dbf1d3db90ed07665e7e47278e7a).

## Scope

To reduce the feature scope to the absolute minimum, the code in the functions is assumed to be low-level code.
Validation, type coersion, etc are considered out of scope for the sake of this discussion.

At the point `clamp` is called:

- `$value`, `$min` and `$max` are all assumed to be integers
- `$min` is assumed to be smaller than `$max`

---

**Footnotes**

1. See [this issue](https://bugs.php.net/bug.php?id=69862)
2. To avoid the order of the files causing a bias, the filenames contain a partial MD5 hash of their content.
3. Let me know if I missed anything

<!--

@TODO: Add "ruby style"

/**
 * @ParamProviders({"provideValues"})
 */
public function bench_($value, $min, $max)
{
    $range = [$min, $value, $max];

    sort($range, SORT_NUMERIC);

    return $range[1];
}

-->

以上是关于markdown 在PHP中编写`clamp`函数的各种风格的比较的主要内容,如果未能解决你的问题,请参考以下文章

markdown 用于比较在PHP Raw中编写`clamp`函数的各种样式的代码量

markdown 代码复杂性报告,用于比较在PHP Raw Raw中编写`clamp`函数的各种样式

Clamp函数

CSS中 min() max() clamp()函数及使用场景详解

CryptoJS:key.clamp 不是一个函数

#yyds干货盘点#CSS的clamp()函数实现响应式布局