2722012beb9afcad75df5c9f2229fd8c

I have a helper that appends the title of a page if a variable exists. I hate that it repeats, but can't think of a cleaner way to approach it. Any thoughts?

def display_title
  if @title.blank?
    APP_CONFIG[:site][:title]
  else
    APP_CONFIG[:site][:title] + " - #{@title}"
  end
end

Refactorings

No refactoring yet !

41508f4877e77d461357e6d3e956a1f8

Aaron Tinio

December 2, 2010, December 02, 2010 06:31, permalink

1 rating. Login to rate!
def display_title
  APP_CONFIG[:site][:title] + (@title.blank? ? '' : " - #{@title}")
end

# or:

def display_title
  APP_CONFIG[:site][:title] + (" - #{@title}" unless @title.blank?).to_s
end
41508f4877e77d461357e6d3e956a1f8

Aaron Tinio

December 2, 2010, December 02, 2010 06:32, permalink

No rating. Login to rate!
def display_title
  APP_CONFIG[:site][:title] + (@title.blank? ? '' : " - #{@title}")
end

# or:

def display_title
  APP_CONFIG[:site][:title] + (" - #{@title}" unless @title.blank?).to_s
end
D41d8cd98f00b204e9800998ecf8427e

Marc

December 2, 2010, December 02, 2010 07:57, permalink

2 ratings. Login to rate!
def display_title
  [APP_CONFIG[:site][:title], @title].compact.join(' -')
end
2722012beb9afcad75df5c9f2229fd8c

TimK

December 2, 2010, December 02, 2010 13:37, permalink

No rating. Login to rate!

Aaron, I really like both of your suggestions. Is there a reason that your second example requires to_s? Aren't you essentially making it a string by having it in ""?

Marc, your example is awesome too. It took me a minute to see why that would work conditionally - my brain was reading right over the compact call.

B8ba61cc84ecb63c859435be28547dfb

steved

December 2, 2010, December 02, 2010 18:01, permalink

2 ratings. Login to rate!

TimK: I think this site often views fewest lines of code as the goal. I think your original example, while five lines instead of one, is easier to read/understand than any of the refactorings.

Here's a non-repeating version that is still easy to read:

def display_title
  result = APP_CONFIG[:site][:title]
  result << " - #{@title}" if @title.present?
  result    
end
2722012beb9afcad75df5c9f2229fd8c

TimK

December 2, 2010, December 02, 2010 22:40, permalink

No rating. Login to rate!

I agree Steve, there has to be a mix between readability and leanness. I really like your example, that was actually what I was trying to conceive in my head but for some reason I dismissed << as appending the item. Thanks!

41508f4877e77d461357e6d3e956a1f8

Aaron Tinio

December 3, 2010, December 03, 2010 00:12, permalink

No rating. Login to rate!

TimK, if @title is blank, the expression inside the parenthesis returns nil, hence the to_s, which does nothing if @title is not blank.

2722012beb9afcad75df5c9f2229fd8c

TimK

December 3, 2010, December 03, 2010 01:37, permalink

No rating. Login to rate!

Aaron, thank you for explaining - I see what you mean.

A8d3f35baafdaea851914b17dae9e1fc

Adam

December 4, 2010, December 04, 2010 07:35, permalink

No rating. Login to rate!

I kind of like the explicitness of the original. @steved watch for the mutation of the stored title in your refactoring, otherwise some unexpected results may occur on subsequent calls to the method.

B8ba61cc84ecb63c859435be28547dfb

steved

December 4, 2010, December 04, 2010 20:43, permalink

No rating. Login to rate!

@Adam: "mutation of the stored title": are you referring to @title or result? In either case I'm not following your concern.

D41d8cd98f00b204e9800998ecf8427e

James Ferguson

December 12, 2010, December 12, 2010 22:34, permalink

No rating. Login to rate!

@steved:
You shouldn't need line 4, it'd be returned from the previous line.

I think Adam was saying that result is just a reference to APP_CONFIG[:site][:title] and << is a destructive call so your method would have the side effect of changing APP_CONFIG[:site][:title].

Try this in irb:

>> foo = "title"
=> "title"
>> bar = foo
=> "title"
>> bar << "stuff"
=> "titlestuff"
>> bar
=> "titlestuff"
>> foo
=> "titlestuff"

Your refactoring





Format Copy from initial code

or Cancel