# manual-clamp

Prefer clamp over nested bound comparisons

Nested comparisons that select the lower bound, upper bound, or original value manually clamp a number.
Instead, you SHOULD call `.clamp()` on the value.

`.clamp()` rejects inverted bounds and NaN bounds.

- Category: style
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
function bounded(value: int32, minimum: int32, maximum: int32): int32 {
    return value < minimum ? minimum : value > maximum ? maximum : value;
}
```

## Accepted

```ds title="main.ds"
function bounded(value: int32, minimum: int32, maximum: int32): int32 {
    return value.clamp(minimum, maximum);
}
```

## Prior art

- [Clippy · manual_clamp](https://rust-lang.github.io/rust-clippy/master/index.html#manual_clamp)

[language/linter/src/rules/style/manual_clamp.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/manual_clamp.rs#L8)
