5

I have a method that receives two range endpoints - start of range and end of range and an integer.

It checks to see if the integer falls between the two end points and returns either the integer or the corresponding end point if the integer falls outside the boundary.

Example 1:

  • RangeStart = 0; RangeEnd = 10; Value = 5; Returns 5

Example 2:

  • RangeStart = 0; RangeEnd = 10; Value = -4; Returns 0

Example 3:

  • RangeStart = 0; RangeEnd = 10; Value = 23; Returns 10

Question: What should I call a method that does that? I had called it IntWithinRange, but I don't think I like that.

Any ideas?

7 Answers 7

11

How about ConstrictToRange / LimitToRange / ConfineToRange? Something of this form would be seem to convey the meaning quite succinctly.

4
  • I like that, it's simple and it's fairly obvious Jul 16, 2009 at 21:26
  • 2
    Confine is another good synonym, as another poster points out. They're all equally good in my view.
    – Noldorin
    Jul 16, 2009 at 21:36
  • I ended up picking ConfineToRange and making it an extension of integer so that I can do things like MyValue.ConfineToRange(StartRange, EndRange); Thanks for the inspiration. Jul 16, 2009 at 22:03
  • You're welcome. And yeah, implementing it as an extension method is a good idea I think.
    – Noldorin
    Jul 16, 2009 at 22:18
10

I've seen it called Clamp().

And that's what M$ calls it.

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.mathhelper.clamp.aspx

7
  • 1
    I guess if that's what Microsoft calls it, it must be right :P Jul 16, 2009 at 21:29
  • 1
    Clamp is what i thought of too!
    – Blindy
    Jul 16, 2009 at 21:37
  • Clamp is simple, but doesn't have as obvious a meaning as the names I suggested IMO.
    – Noldorin
    Jul 16, 2009 at 21:57
  • 1
    I believe that's also the mathematical term as well. Jul 16, 2009 at 22:06
  • Names are weird. Imagine if you had to name sin() and cos() functions. Some might suggest OppositeOverHypotenuse()...
    – Ray
    Jul 16, 2009 at 22:08
7
confine_to(start, end, value)
1

GetBoundedValue?

1

normalize

1
  • In digital music this kind of process is called Quantizing - so I think I like Normalize... Jul 16, 2009 at 21:25
1

A short one: Squeeze().

0

BoundedValue() or GetBoundedValue()

Some of the other names sound to me like you're modifying something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.