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
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
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.
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]
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
regards
Panagiotis Atmatzidis
-----------------------------
Pharmacy Student at VFU
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 ?