[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

can ik make this more beautifull?

Remco Hh

1/31/2008 11:53:00 AM

nowadays i try to improve my coding style to produce nicer and beter
readable code. I try to improve the following code but i dont know how.

if condtion1
x=1
else
if condition2
x=2
else
if condition3
x=3
end
end
x=nill
end

there are three different conditions which can not occur at the same
time
can i make this nicer?
--
Posted via http://www.ruby-....

16 Answers

Felipe Giotto

1/31/2008 12:07:00 PM

0

Actually, when you set x to nil, in the end of the code, you're losing
your entire second "if" clause. Unless these conditions are calls to
other functions or something like that, you could refactor your code
this way:

x =3D condition1 ? 1 : nil

Best regards!

Felipe Giotto ;-)


On Jan 31, 2008 9:52 AM, Remco Hh <remco@huijdts.nl> wrote:
> nowadays i try to improve my coding style to produce nicer and beter
> readable code. I try to improve the following code but i dont know how.
>
> if condtion1
> x=3D1
> else
> if condition2
> x=3D2
> else
> if condition3
> x=3D3
> end
> end
> x=3Dnill
> end
>
> there are three different conditions which can not occur at the same
> time
> can i make this nicer?
> --
> Posted via http://www.ruby-....
>
>



--=20
Felipe Luiz Christ=F3folli Giotto
Inovare Development Team
http://www.i...

Felix Windt

1/31/2008 12:10:00 PM

0

On Thu, 2008-01-31 at 20:52 +0900, Remco Hh wrote:
> nowadays i try to improve my coding style to produce nicer and beter
> readable code. I try to improve the following code but i dont know how.
>
> if condtion1
> x=1
> else
> if condition2
> x=2
> else
> if condition3
> x=3
> end
> end
> x=nill
> end
>
> there are three different conditions which can not occur at the same
> time
> can i make this nicer?

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressio...

HTH,

Felix


Jari Williamsson

1/31/2008 12:23:00 PM

0

Remco Hh wrote:
> nowadays i try to improve my coding style to produce nicer and beter
> readable code. I try to improve the following code but i dont know how.
>
> if condtion1
> x=1
> else
> if condition2
> x=2
> else
> if condition3
> x=3
> end
> end
> x=nill
> end
>
> there are three different conditions which can not occur at the same
> time
> can i make this nicer?

A case expression is probably the most clear alternative. The shortest
is perhaps something like:

x = condition1 ? 1 : condition2 ? 2 : 3


Best regards,

Jari Williamsson

Peter Hickman

1/31/2008 1:47:00 PM

0

Jari Williamsson wrote:
> Remco Hh wrote:
>> nowadays i try to improve my coding style to produce nicer and beter
>> readable code. I try to improve the following code but i dont know how.
>>
>> if condtion1
>> x=1
>> else
>> if condition2
>> x=2
>> else
>> if condition3
>> x=3
>> end
>> end
>> x=nill
>> end
>>
>> there are three different conditions which can not occur at the same
>> time
>> can i make this nicer?
>
> A case expression is probably the most clear alternative. The shortest
> is perhaps something like:
>
> x = condition1 ? 1 : condition2 ? 2 : 3
>
>
> Best regards,
>
> Jari Williamsson
>
>
Short it may be but this sort of code can hardly be called beautiful, I
have had to maintain crap like this before and the few keystrokes the
author saved was made up for 1000 times when someone updated it and
things didn't 'quite' work how they thought. Ruby allows simplicity and
clarity, this however is heading into obfuscation.

The better solution is to use the elsif keyword to flatten the indentation:

if (temp == 'cat')
x = 1
elsif(temp == 'dog')
x = 2
elsif(temp == 'banana')
x = 3
else
x = nil
end


Wyatt Greene

1/31/2008 2:04:00 PM

0

On Jan 31, 8:47 am, Peter Hickman <pe...@semantico.com> wrote:
> Jari Williamsson wrote:
> > Remco Hh wrote:
> >> nowadays i try to improve my coding style to produce nicer and beter
> >> readable code. I try to improve the following code but i dont know how.
>
> >> if condtion1
> >> x=1
> >> else
> >> if condition2
> >> x=2
> >> else
> >> if condition3
> >> x=3
> >> end
> >> end
> >> x=nill
> >> end
>
> >> there are three different conditions which can not occur at the same
> >> time
> >> can i make this nicer?
>
> > A case expression is probably the most clear alternative. The shortest
> > is perhaps something like:
>
> > x = condition1 ? 1 : condition2 ? 2 : 3
>
> > Best regards,
>
> > Jari Williamsson
>
> Short it may be but this sort of code can hardly be called beautiful, I
> have had to maintain crap like this before and the few keystrokes the
> author saved was made up for 1000 times when someone updated it and
> things didn't 'quite' work how they thought. Ruby allows simplicity and
> clarity, this however is heading into obfuscation.
>
> The better solution is to use the elsif keyword to flatten the indentation:
>
> if (temp == 'cat')
> x = 1
> elsif(temp == 'dog')
> x = 2
> elsif(temp == 'banana')
> x = 3
> else
> x = nil
> end

Which is analogous to the case statement:

case temp
when 'cat': 1
when 'dog': 2
when 'banana': 3
else nil
end

Jari Williamsson

1/31/2008 2:14:00 PM

0

Peter Hickman wrote:
> Jari Williamsson wrote:
>> Remco Hh wrote:
>>> nowadays i try to improve my coding style to produce nicer and beter
>>> readable code. I try to improve the following code but i dont know how.
>>>
>>> if condtion1
>>> x=1
>>> else
>>> if condition2
>>> x=2
>>> else
>>> if condition3
>>> x=3
>>> end
>>> end
>>> x=nill
>>> end
>>>
>>> there are three different conditions which can not occur at the same
>>> time
>>> can i make this nicer?
>>
>> A case expression is probably the most clear alternative. The shortest
>> is perhaps something like:
>>
>> x = condition1 ? 1 : condition2 ? 2 : 3
>>
>>
>> Best regards,
>>
>> Jari Williamsson
>>
>>
> Short it may be but this sort of code can hardly be called beautiful, I
> have had to maintain crap like this before and the few keystrokes the
> author saved was made up for 1000 times when someone updated it and
> things didn't 'quite' work how they thought. Ruby allows simplicity and
> clarity, this however is heading into obfuscation.
>
> The better solution is to use the elsif keyword to flatten the indentation:
>
> if (temp == 'cat')
> x = 1
> elsif(temp == 'dog')
> x = 2
> elsif(temp == 'banana')
> x = 3
> else
> x = nil
> end

But this elsif solution isn't heading for clarity either, compared to
using a case expression:
x = case temp
when 1 then 'cat'
when 2 then 'dog'
when 3 then 'banana'
else nil
end

In the elsif example, x appears 4 times instead of 1, temp appears 3
times instead of 1. Apart from require an editor that support
refactoring and the risk of typos, there will also be a performance hit
when temp isn't matched.


Best regards,

Jari Williamsson

Mike Berrow

1/31/2008 2:14:00 PM

0

That is apparently a matter of opinion.
I find the single line alternative clear, concise and quite beautiful.
It reads just like a sentence for me.

The 9-line alternative is annoyingly verbose and just burns vertical
space.
That style has me paging up and down or reaching for the scroll bar more
often.
Having to do that does not make the code easier to read in my opinion.

-- Mike Berrow

---------------------------
Peter Hickman wrote:
> Jari Williamsson wrote:
>>> if condition3
>> A case expression is probably the most clear alternative. The shortest
>> is perhaps something like:
>>
>> x = condition1 ? 1 : condition2 ? 2 : 3
>>
>>
>> Best regards,
>>
>> Jari Williamsson
>>
>>
> Short it may be but this sort of code can hardly be called beautiful, I
> have had to maintain crap like this before and the few keystrokes the
> author saved was made up for 1000 times when someone updated it and
> things didn't 'quite' work how they thought. Ruby allows simplicity and
> clarity, this however is heading into obfuscation.
>
> The better solution is to use the elsif keyword to flatten the
> indentation:
>
> if (temp == 'cat')
> x = 1
> elsif(temp == 'dog')
> x = 2
> elsif(temp == 'banana')
> x = 3
> else
> x = nil
> end

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

Chris Hulan

1/31/2008 2:51:00 PM

0

On Jan 31, 9:14 am, Mike Berrow <mberr...@pacbell.net> wrote:
> That is apparently a matter of opinion.
> I find the single line alternative clear, concise and quite beautiful.
> It reads just like a sentence for me.
>
> The 9-line alternative is annoyingly verbose and just burns vertical
> space.
> That style has me paging up and down or reaching for the scroll bar more
> often.
> Having to do that does not make the code easier to read in my opinion.
>
> -- Mike Berrow
>
> ---------------------------
>
>
>
> Peter Hickman wrote:
> > Jari Williamsson wrote:
> >>> if condition3
> >> A case expression is probably the most clear alternative. The shortest
> >> is perhaps something like:
>
> >> x = condition1 ? 1 : condition2 ? 2 : 3
>
> >> Best regards,
>
> >> Jari Williamsson
>
> > Short it may be but this sort of code can hardly be called beautiful, I
> > have had to maintain crap like this before and the few keystrokes the
> > author saved was made up for 1000 times when someone updated it and
> > things didn't 'quite' work how they thought. Ruby allows simplicity and
> > clarity, this however is heading into obfuscation.
>
> > The better solution is to use the elsif keyword to flatten the
> > indentation:
>
> > if (temp == 'cat')
> > x = 1
> > elsif(temp == 'dog')
> > x = 2
> > elsif(temp == 'banana')
> > x = 3
> > else
> > x = nil
> > end
>
> --
> Posted viahttp://www.ruby-....

Since your making a choice, a hash seems like it might be a tidy
option:

answers = {condition1=>1, condition2=>2, condition3=>3}
x = answers[condition]

As for the oneline version, I always find it particulary confusing in
Ruby as you can have ? at the end of a method or it can be used in
front a character to get the characters ASCII code. And nesting just
multiplies the problemn, IMHO

cheers

Robert Klemme

1/31/2008 3:06:00 PM

0

2008/1/31, Jari Williamsson <jari.williamsson@mailbox.swipnet.se>:
> Peter Hickman wrote:
> > Jari Williamsson wrote:
> >> Remco Hh wrote:
> >>> nowadays i try to improve my coding style to produce nicer and beter
> >>> readable code. I try to improve the following code but i dont know how.
> >>>
> >>> if condtion1
> >>> x=1
> >>> else
> >>> if condition2
> >>> x=2
> >>> else
> >>> if condition3
> >>> x=3
> >>> end
> >>> end
> >>> x=nill
> >>> end
> >>>
> >>> there are three different conditions which can not occur at the same
> >>> time
> >>> can i make this nicer?
> >>
> >> A case expression is probably the most clear alternative. The shortest
> >> is perhaps something like:
> >>
> >> x = condition1 ? 1 : condition2 ? 2 : 3
> >>
> >>
> >> Best regards,
> >>
> >> Jari Williamsson
> >>
> >>
> > Short it may be but this sort of code can hardly be called beautiful, I
> > have had to maintain crap like this before and the few keystrokes the
> > author saved was made up for 1000 times when someone updated it and
> > things didn't 'quite' work how they thought. Ruby allows simplicity and
> > clarity, this however is heading into obfuscation.
> >
> > The better solution is to use the elsif keyword to flatten the indentation:
> >
> > if (temp == 'cat')
> > x = 1
> > elsif(temp == 'dog')
> > x = 2
> > elsif(temp == 'banana')
> > x = 3
> > else
> > x = nil
> > end
>
> But this elsif solution isn't heading for clarity either, compared to
> using a case expression:
> x = case temp
> when 1 then 'cat'
> when 2 then 'dog'
> when 3 then 'banana'
> else nil
> end
>
> In the elsif example, x appears 4 times instead of 1, temp appears 3
> times instead of 1. Apart from require an editor that support
> refactoring and the risk of typos, there will also be a performance hit
> when temp isn't matched.

If you are using "case" then there is yet another solution:

x = case
when condition1 then 1
when condition2 then 2
when condition3 then 3
else nil
end

Another approach would be to stuff conditions in an array:

CONDITIONS = [
lambda {|x| condition1 },
lambda {|x| condition2 },
lambda {|x| condition3 },
]

def test(*values)
CONDITIONS.each_with_index do |c,i|
return i+1 if c[*values]
end
nil
end

x = test "whatever"

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Peter Hickman

1/31/2008 3:18:00 PM

0

Jari Williamsson wrote:
> But this elsif solution isn't heading for clarity either, compared to
> using a case expression:
> x = case temp
> when 1 then 'cat'
> when 2 then 'dog'
> when 3 then 'banana'
> else nil
> end
>
> In the elsif example, x appears 4 times instead of 1, temp appears 3
> times instead of 1. Apart from require an editor that support
> refactoring and the risk of typos, there will also be a performance
> hit when temp isn't matched.

Indeed this is a nice solution to the problem of assigning a value.
However the 'temp == ...' was of my own invention and not part of the
OP. That simply gave 'condition1', 'condition2' etc so there is a strong
possibility that what you propose would not work for him as the
conditions might not be based upon the same variable as in my example,
if the OP is new to programming you will have led him down the wrong
path by not pointing out that your solution only works for conditions
based upon the conditions all being tests of temp.

Also when posting code it helps to run it, no matter how much of a
genius you are. Your code does not do what mine did. Try this instead:

x = case temp
when 'cat' then 1
when 'dog' then 2
when 'banana' then 3
else nil
end

I would worry less about how many keystrokes you have saved and spend a
little more time getting it right and testing your code. You wrote 3
lines less than me and got it completely backwards.