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 :slight_smile:

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 :slight_smile:

ps. what are you? :slight_smile:


Posted via http://www.ruby-forum.com/.

Cheers

[1] http://www.goodreads.com/book/show/8677004-the-art-of-readable-code

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

How about

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
about the principle mostly :slight_smile:

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 :wink:

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

haha good one :wink:

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

  • performance
  • readability
  • shortness

Without knowing the goal the question is incomplete and cannot be
answered as such.

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 :slight_smile:

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 :slight_smile:

Are you? :slight_smile:


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
read
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs