How to name a clamp function that only clamps from one side?
Posted
by
dog_funtom
on Programmers
See other posts from Programmers
or by dog_funtom
Published on 2014-08-19T21:39:49Z
Indexed on
2014/08/19
22:31 UTC
Read the original article
Hit count: 181
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);
© Programmers or respective owner