# Best coding for limiting a value?

A) result=value<min ? min : (value > max ? max : value)

B)
result=value
if value < min then result= min end
if value > max then result= max end

C)
[min,max,value].sort[1]

(nota: I am not a student, and neither a teacher

On 09/04/2012 10:13 PM, Regis dāAubarede wrote:

A) result=value<min ? min : (value > max ? max : value)

B)
result=value
if value < min then result= min end
if value > max then result= max end

C)
[min,max,value].sort[1]

I like your option C best.

There is also: [[max, value].min, min].max

Hello,

Since all 3 methods do the same, itās a matter of taste.

In the book the āArt of readable codeā by Dustin Boswell &Trevor
Foucher the reader will certainly choose the (B) as itās easier to read
immediately by a 3rd party.

If you have many questions like this one, you should really read a book
like this one.

On 4 Ī£ĪµĻ 2012, at 23:13 , Regis dāAubarede [email protected] wrote:

(nota: i am not a student, and neither a teacher

ps. what are you?

ā
Posted via http://www.ruby-forum.com/.

Cheers

Panagiotis A.

On Sep 4, 2012, at 13:24 , Panagiotis A. [email protected]
wrote:

In the book the āArt of readable codeā by Dustin Boswell &Trevor Foucher the
reader will certainly choose the (B) as itās easier to read immediately by a 3rd
party.

If you have many questions like this one, you should really read a book like
this one.

I disagree. I have to look at the code in B and reason about it. With C,
I donāt. It becomes obvious what it wants. In ruby, I generally find the
shortest solution (with the least amount of syntax and normal
whitespaceānot golfing) is the clearest and therefore the best.

On 09/05/2012 08:20 AM, Lars H. wrote:

I like your option C best.

There is also: [[max, value].min, min].max

1.9.3p125 :001 > foo = 3
=> 3
1.9.3p125 :002 > MIN=0
=> 0
1.9.3p125 :003 > MAX=10
=> 10
1.9.3p125 :004 > foo < MIN && MIN || foo > MAX && MAX || foo
=> 3
1.9.3p125 :005 > foo = 11
=> 11
1.9.3p125 :006 > foo < MIN && MIN || foo > MAX && MAX || foo
=> 10
1.9.3p125 :007 > foo = -1
=> -1
1.9.3p125 :008 > foo < MIN && MIN || foo > MAX && MAX || foo
=> 0

Sam

Hello,

On 4 Ī£ĪµĻ 2012, at 23:27 , Ryan D. [email protected] wrote:

On Sep 4, 2012, at 13:24 , Panagiotis A. [email protected] wrote:

In the book the āArt of readable codeā by Dustin Boswell &Trevor Foucher the
reader will certainly choose the (B) as itās easier to read immediately by a 3rd
party.

If you have many questions like this one, you should really read a book like
this one.

I disagree. I have to look at the code in B and reason about it. With C, I
donāt. It becomes obvious what it wants. In ruby, I generally find the shortest
solution (with the least amount of syntax and normal whitespaceānot golfing) is
the clearest and therefore the best.

Well to tell you the truth I didnāt pay too much attention, but yes
youāre right. The third solutions is the most easily readable, although
I never used it it was absolutely clear what it does. I was arguing

regards

Panagiotis A.

On Tue, Sep 4, 2012 at 10:13 PM, Regis dāAubarede [email protected]
wrote:

A) result=value<min ? min : (value > max ? max : value)

B)
result=value
if value < min then result= min end
if value > max then result= max end

C)
[min,max,value].sort[1]

ābestā according to what metric?

Cheers

robert

Regis dāAubarede wrote in post #1074698:

B)
result=value
if value < min then result= min end
if value > max then result= max end

You can avoid the temporary variable by using the return value from āifā
and ācaseā expressions.

case
when value < min
min
when value > max
max
else
value
end

This scrunches to:

case when value < min: min; when value > max: max; else value; end

(I have a vague idea that something changed about using colon in if/case

• but Iāve tested this with 1.8.7 and it works)

Thereās also the modifier form of if:

result = value
result = min if value < min
result = max if value > max

Well they all work and return the same result so from that point of
view they are as good as each other butā¦

1. You can work out what it is doing, but you have to stop and think a
bit which breaks the flow when reading code
2. Verbose but clear, anyone should be able to read this without
breaking a sweat
3. Huh? Well it works but to be honest I had to write some tests to
make sure that it would work

So in conclusion 1 and 2 just need a good method name and you are good
to go. I would not want to see 3 inline, its neat but not explicit. It
is almost magical in its behaviour. But perhaps that is more a
reflection of my level of skill but I would not want to inherit code
that contained stuff like this.

Performance wise? Probably nothing significant. 1 feels like it would
be the fastest but not by any margin worth worrying about.

haha good one

On Wed, Sep 5, 2012 at 11:46 AM, Nathan Weldegorges
[email protected] wrote:

haha good one

And it wasnāt even intended as joke. I can think of various goals
which will prompt different responses, e.g.

• performance
• shortness

Without knowing the goal the question is incomplete and cannot be

Kind regards

robert

On Sep 4, 2012, at 10:13 PM, Regis dāAubarede wrote:

A) result=value<min ? min : (value > max ? max : value)

B)
result=value
if value < min then result= min end
if value > max then result= max end

C)
[min,max,value].sort[1]

For what its worth, Iāve often used a variation of A in Ruby like this:

[[lower, value].max, upper].min

It will obviously not be faster but perhaps easier to understand for
some.

Overall I think Iād prefer solution C now. Having said that, an
Array#median method would be great in this case:

[min, max, value].median

Robert K. wrote in post #1074749:

ābestā according to what metric?

Romeo: I love you !
Juliet: Ok, but what metrics ?

sorry

Sylvester K. wrote in post #1074778:

Having said that, an
Array#median method would be great in this case:

or Range#clip

Oh I know it wasnāt but I turned it into one as a pun could have been
taken. Sorry if you think I donāt take learning and programming
seriously
amongst others as I do.

On 5 Ī£ĪµĻ 2012, at 20:26 , Regis dāAubarede [email protected] wrote:

Robert K. wrote in post #1074749:

ābestā according to what metric?

Romeo: I love you !
Juliet: Ok, but what metrics ?

Enough to commit suicide.

sorry

Are you?

ā
Posted via http://www.ruby-forum.com/.

Panagiotis A.

On Tue, Sep 4, 2012 at 3:13 PM, Regis dāAubarede
[email protected]wrote:

I like (B) best. Initially I liked Ā©, b/c itās easiest to parse. But I
read it as āselect the middle valueā Then I figured I should actually
the others, and realized it was āselect the closest value in rangeā. But
+1
to what Peter H. said about picking a good name will make it
largely
irrelevant which one you choose (i.e. donāt just drop this into the
middle
of some code, wrap it in a descriptive method name).

def closest_in_bounds(value)
return min if value < min_value
return max if max_value < value
value
end

This does make the assumption that your min and max are methods, which
assumes youāre already following an OO style of writing, but based on
your
initial examples, itās difficult to tell if this is the case.