[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

DRY or not ...

Josselin

2/8/2007 11:39:00 AM

as a new(ru)bie I always wonder when trying to DRY such code is good or
not and why it should be (I don't like a lot the if.. else.. elsif...
but sometimes it seems better for readiness... vs performances (less
code..)

If I should DRY it , any tip ?
if params[:new_title].blank?
params[:new_title] = nil
elsif params[:new_title] == "null"
params[:new_title] = ""
end


joss

5 Answers

Devin Mullins

2/8/2007 1:46:00 PM

0

Josselin wrote:
> if params[:new_title].blank?
> params[:new_title] = nil
> elsif params[:new_title] == "null"
> params[:new_title] = ""
> end
params[:new_title] = if params[:new_title].blank?
nil
elsif params[:new_title] == "null"
""
end
For reasons yet unbeknownst to you, reverse that if statement:
params[:new_title] = if params[:new_title] == "null"
""
elsif params[:new_title].blank?
nil
end
Implicit in that if statement is an "else nil". (Try it in IRB.) So you
don't really need anything but the first part:
params[:new_title] = if params[:new_title] == "null"
""
end
You can shorten even further by using the line-modifier version:
params[:new_title] = ("" if params[:new_title] == "null")
(Parentheses are important, here. Otherwise, the if modifies the whole
line, and if not "null", the :new_title remains unchanged.) But that's a
little greek. Here's another way:
params[:new_title] = params[:new_title] == 'null' ? '' : nil

Now, there's something very weird with the above logic, in the first
place. You might be able to reduce even more code by changing the thing
that sets :new_title. ("null", for Ford's sake?)

Devin

Eivind Eklund

2/8/2007 1:58:00 PM

0

On 2/8/07, Josselin <josselin@wanadoo.fr> wrote:
> as a new(ru)bie I always wonder when trying to DRY such code is good or
> not and why it should be (I don't like a lot the if.. else.. elsif...
> but sometimes it seems better for readiness... vs performances (less
> code..)
>
> If I should DRY it , any tip ?
> if params[:new_title].blank?
> params[:new_title] = nil
> elsif params[:new_title] == "null"
> params[:new_title] = ""
> end

Seems to me like this should likely be an object, with
object.new_title being something like

@title.blank? ? nil : (@title == "null" ? "" : @title)

Though I also agree with Devin Mullins that the problem is likely to
be elsewhere (in overall structure).

Eivind.

Devin Mullins

2/8/2007 2:26:00 PM

0

Devin Mullins wrote:
> Josselin wrote:
>
>> if params[:new_title].blank?
>> params[:new_title] = nil
>> elsif params[:new_title] == "null"
>> params[:new_title] = ""
>> end
>
> params[:new_title] = if params[:new_title].blank?
> nil
> elsif params[:new_title] == "null"
> ""
> end

Heh. Big fat bug. Right at the beginning. *cough*

params[:new_title] = case params[:new_title]
when '': nil
when 'null': ''
else params[:new_title]
end
or:
params[:new_title] = {
'' => nil, 'null' => '', params[:new_title] => params[:new_title]
}[params[:new_title]] #yikes!

Devin

dblack

2/8/2007 3:10:00 PM

0

Josselin

2/10/2007 9:36:00 AM

0

On 2007-02-08 14:58:20 +0100, "Eivind Eklund" <eeklund@gmail.com> said:

> On 2/8/07, Josselin <josselin@wanadoo.fr> wrote:
>> as a new(ru)bie I always wonder when trying to DRY such code is good or
>> not and why it should be (I don't like a lot the if.. else.. elsif...
>> but sometimes it seems better for readiness... vs performances (less
>> code..)
>>
>> If I should DRY it , any tip ?
>> if params[:new_title].blank?
>> params[:new_title] = nil
>> elsif params[:new_title] == "null"
>> params[:new_title] = ""
>> end
>
> Seems to me like this should likely be an object, with
> object.new_title being something like
>
> @title.blank? ? nil : (@title == "null" ? "" : @title)
>
> Though I also agree with Devin Mullins that the problem is likely to
> be elsewhere (in overall structure).
>
> Eivind.

Thanks to all .. getting clear