views:

187

answers:

8

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?

+11  A: 

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

Noldorin
I like that, it's simple and it's fairly obvious
BenAlabaster
I like LimitToRange best
Chris Simpson
`Confine` is another good synonym, as another poster points out. They're all equally good in my view.
Noldorin
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.
BenAlabaster
You're welcome. And yeah, implementing it as an extension method is a good idea I think.
Noldorin
+1  A: 

GetBoundedValue?

Eliseo Ocampos
+10  A: 

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

Ray
I guess if that's what Microsoft calls it, it must be right :P
BenAlabaster
Clamp is what i thought of too!
Blindy
Clamp is simple, but doesn't have as obvious a meaning as the names I suggested IMO.
Noldorin
I believe that's also the mathematical term as well.
Jeremy Powell
Names are weird. Imagine if you had to name sin() and cos() functions. Some might suggest OppositeOverHypotenuse()...
Ray
Yeah, it's all about what's widely understood.
Noldorin
@Jeremy: Never come across it in maths. I would think it's just restricted to programming.
Noldorin
A: 

CheckRange seems sufficient to me

Jack Marchetti
+7  A: 
confine_to(start, end, value)
Stephan202
+1  A: 

normalize

dfa
In digital music this kind of process is called Quantizing - so I think I like Normalize...
BenAlabaster
+1  A: 

A short one: Squeeze().

balpha
A: 

BoundedValue() or GetBoundedValue()

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

Darryl