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
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 endC)
[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
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
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 endC)
[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
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ā¦
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.
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 endC)
[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 (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 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.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs