# minOf and maxOf versus coerceAtLeast and coerceAtMost

**URL:** https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258
**Category:** Libraries
**Created:** [February 3, 2017, 7:22pm UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258 "2017-02-03T19:22:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![xenomachina](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/xenomachina/32/9266_2.png) [@xenomachina](https://discuss.kotlinlang.org/u/xenomachina)
#### Post date: [February 3, 2017, 7:22pm UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258/1 "2017-02-03T19:22:32Z")

</div>

I was looking through the _What’s New in Kotlin 1.1_ page, and came across [minOf() and maxOf()](https://kotlinlang.org/docs/reference/whatsnew11.html#minof-and-maxof). The description there is pretty brief, but it sounds like these are essentially the same as `coerceAtMost` and `coerceAtLeast`, except they take an optional comparator, and they’re functions not methods.

I have to say I really prefer the `minOf`/`maxOf` names. Are there plans to deprecate `coerceAtLeast` and `coerceAtMost`?

---

<div class="post-metadata">

### Author: ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)
#### Post date: [February 3, 2017, 7:44pm UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258/2 "2017-02-03T19:44:01Z")

</div>

> [@xenomachina](#):
>
> Are there plans to deprecate coerceAtLeast and coerceAtMost?

No there are no such plans, because these functions cover different use cases.

`minOf/maxOf` are top level functions for finding the minimum or maximum of _two_ (or three) values.  
`val minSize = minOf(this.size, that.size)`

On the other hand `coerceAtLeast/AtMost` are extension functions that are used when you have _one_ value, and you want to ensure it’s within the given limits, for example:  
`val insertionPoint = index.coerceAtMost(lastIndex)`

Therefore `coerce*` functions help to express the intent more clearly.

---

<div class="post-metadata">

### Author: ![xenomachina](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/xenomachina/32/9266_2.png) [@xenomachina](https://discuss.kotlinlang.org/u/xenomachina)
#### Post date: [February 3, 2017, 9:19pm UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258/3 "2017-02-03T21:19:48Z")

</div>

I’m not really convinced that the coerceAtLeast/AtMost methods are much better expressing intent. Every time I’ve had to use the coerce methods I have a hard time figuring out which one I actually mean to use, even when I’m starting out with a value that I want to constrain to some limit(s). I never have that problem using “min” or “max” in other languages. Interestingly, I also don’t have that problem with `coerceIn(min, max)`, and have even considered doing things like this for clarity:

```
x.coerceIn(0, x)

```

I also always have a really hard time remembering the names of these methods in the first place. I think part of what keeps throwing me off is that [“coerce” normally means that it changes the type](https://en.wikibooks.org/wiki/Introduction_to_Programming_Languages/Coercion). I keep expecting these methods to be called “constrain” or maybe “[clamp](https://en.wikipedia.org/wiki/Clamping_(graphics))”.

The only advantage I see to the `coerceAtLeast` and `coerceAtMost` methods are that they make it obvious which object you’ll get when the two parameters are equal, whereas `minOf` and `maxOf` don’t (if they even guarantee one or the other).

---

<div class="post-metadata">

### Author: ![awaters](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/awaters/32/2232_2.png) [@awaters](https://discuss.kotlinlang.org/u/awaters)
#### Post date: [February 9, 2017, 3:03am UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258/4 "2017-02-09T03:03:33Z")

</div>

FTR I thought the same thing about the `coerce*` naming (that it sounds/feels like a type related thing) - we were also using `clamp` before they were added. Not sure if they can be changed at this point though unfortunately (though 1.1 not official yet so maybe they can be?)

---

<div class="post-metadata">

### Author: ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)
#### Post date: [February 9, 2017, 7:47am UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258/5 "2017-02-09T07:47:54Z")

</div>

> [@ilya.gorbunov](#):
>
> minOf/maxOf are top level functions for finding the minimum or maximum of two (or three) values.

Suggestion for more parameters:

```
inline fun <T:Comparable<T>> max(vararg values:T) = values.max()

```

---

<div class="post-metadata">

### Author: ![yay](https://avatars.discourse-cdn.com/v4/letter/y/9dc877/32.png) [@yay](https://discuss.kotlinlang.org/u/yay)
#### Post date: [December 27, 2017, 10:09am UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258/6 "2017-12-27T10:09:31Z")

</div>

Not knowing that there were such functions already in the Kotlin’s standard library, I implemented my own ‘clampMin’, ‘clampMax’ and ‘clamp’ extension functions, which is a common way to name something like this. Google ‘coerceAtLeast’ and ‘coerceAtMost’ however, and first page of results will refer to Kotlin. I personally prefer the ‘clamp\*’ naming convention. ‘coerceAtLeast’ name is too creative (for reasons not clear to me) and long-winded, IMO.

---

<div class="post-metadata">

### Author: ![stephendunn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stephendunn/32/6891_2.png) [@stephendunn](https://discuss.kotlinlang.org/u/stephendunn)
#### Post date: [February 9, 2020, 7:07pm UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258/7 "2020-02-09T19:07:04Z")

</div>

Agreed, “clamp” would be much clearer.

---

<div class="post-metadata">

### Author: ![ustamansangat](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ustamansangat/32/8460_2.png) [@ustamansangat](https://discuss.kotlinlang.org/u/ustamansangat)
#### Post date: [April 1, 2021, 9:13pm UTC](https://discuss.kotlinlang.org/t/minof-and-maxof-versus-coerceatleast-and-coerceatmost/2258/8 "2021-04-01T21:13:49Z")

</div>

Totally agree coerce makes me think of type coercion.
