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:

ยทยทยท

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

I like your option C best.

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

ยทยทยท

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]

--
Lars Haugseth

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.

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:

ps. what are you? :slight_smile:

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

Cheers

[1] The Art of Readable Code: Simple and Practical Techniques for Writing Better Code by Dustin Boswell | Goodreads

Panagiotis Atmatzidis

ยทยทยท

On 4 ฮฃฮตฯ€ 2012, at 23:13 , Regis d'Aubarede <lists@ruby-forum.com> wrote:
-----------------------------
Pharmacy Student at VFU

email4lists: ml@convalesco.org
More info: http://about.me/atmosx

The wise man said: "Never argue with an idiot, he brings you down to his level and beat you with experience."

"best" according to what metric?

Cheers

robert

ยทยทยท

On Tue, Sep 4, 2012 at 10:13 PM, Regis d'Aubarede <lists@ruby-forum.com> 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]

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

ยทยทยท

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

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

ยทยทยท

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]

I like (B) best. Initially I liked (C), 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 Hickman 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.

ยทยทยท

On Tue, Sep 4, 2012 at 3:13 PM, Regis d'Aubarede <lists@ruby-forum.com>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]

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

ยทยทยท

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

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

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 Sep 4, 2012, at 13:24 , Panagiotis Atmatzidis <ml@convalesco.org> 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.

haha good one :wink:

ยทยทยท

On Wed, Sep 5, 2012 at 2:11 PM, Robert Klemme <shortcutter@googlemail.com>wrote:

On Tue, Sep 4, 2012 at 10:13 PM, Regis d'Aubarede <lists@ruby-forum.com> > 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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Sylvester Keil wrote in post #1074778:

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

or Range#clip

ยทยทยท

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

Robert Klemme wrote in post #1074749:

"best" according to what metric?

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

sorry :slight_smile:

ยทยทยท

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

Hello,

ยทยทยท

On 4 ฮฃฮตฯ€ 2012, at 23:27 , Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Sep 4, 2012, at 13:24 , Panagiotis Atmatzidis <ml@convalesco.org> 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 Atmatzidis
-----------------------------
Pharmacy Student at VFU

email4lists: ml@convalesco.org
More info: http://about.me/atmosx

The wise man said: "Never argue with an idiot, he brings you down to his level and beat you with experience."

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 Wed, Sep 5, 2012 at 11:46 AM, Nathan Weldegorges <elihu5991@gmail.com> wrote:

haha good one :wink:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme 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 Atmatzidis

ยทยทยท

On 5 ฮฃฮตฯ€ 2012, at 20:26 , Regis d'Aubarede <lists@ruby-forum.com> wrote:
-----------------------------
Pharmacy Student at VFU

email4lists: ml@convalesco.org
More info: http://about.me/atmosx

The wise man said: "Never argue with an idiot, he brings you down to his level and beat you with experience."

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.

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 Thu, Sep 6, 2012 at 8:45 AM, Panagiotis Atmatzidis <ml@convalesco.org>wrote:

On 5 ฮฃฮตฯ€ 2012, at 20:26 , Regis d'Aubarede <lists@ruby-forum.com> wrote:

> Robert Klemme 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 Atmatzidis
-----------------------------
Pharmacy Student at VFU

email4lists: ml@convalesco.org
More info: http://about.me/atmosx

The wise man said: "Never argue with an idiot, he brings you down to his
level and beat you with experience."