# DRY or not

**URL:** <https://www.ruby-forum.com/t/dry-or-not/84888>\
**Category:** Ruby\
**Created:** [February 8, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/dry-or-not/84888 "2007-02-08T12:42:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 8, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/dry-or-not/84888/1 "2007-02-08T12:42:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 8, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/dry-or-not/84888/2 "2007-02-08T14:46:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 8, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/dry-or-not/84888/3 "2007-02-08T14:59:20Z")

</div>

On 2/8/07, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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 M. that the problem is likely to  
be elsewhere (in overall structure).

Eivind.

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 8, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/dry-or-not/84888/4 "2007-02-08T15:27:24Z")

</div>

Devin M. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 10, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/dry-or-not/84888/5 "2007-02-10T12:01:57Z")

</div>

On 2007-02-08 14:58:20 +0100, “Eivind E.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > ```
> > params[:new_title] = ""
> > 
> > ```
> 
> Eivind.  
> Thanks to all … getting clear

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 8, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/dry-or-not/84888/6 "2007-02-08T16:11:29Z")

</div>

Hi –

On Thu, 8 Feb 2007, Devin M. 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] == “null”)

That doesn’t embody the same logic, though. If params[:new\_title] is  
neither blank? nor “null”, the original code doesn’t change it.

David
