How to name a clamp function that only clamps from one side?
- by dog_funtom
Clamp() is a function that ensures that provided variable is in provided range. You can find such function in .NET, in Unity, and probably anywhere.
While it is useful, I often need to clamp my value from one side only. For example, to ensure that float is always non-negative or always positive (like radius value from inspector).
I used names ClampFromAbove() and ClampFromBelow(), but I wonder if such names is good or even grammatically valid in programming-English.
Also, it probably make sense to distinguish non-negative case too. How'd you name such function? Something like EnsureNonNegative()?
My intention is creating pair of extension methods and use them like this:
var normalizedRadius = originalRadius.ClampFromBelow(0.0001);
var distance = someVector.Magnitude.ClampFromAbove(maxDistance);