#10 Refactoring User Name Part 1
Mar 26, 2007 | 5 minutes | Refactoring
Learn how to clean up your code through refactoring. This episode will show you how to move code from the view into the model to remove duplication and simplify the view.
i was curious about the interpolation of middle_name in the full_name method. why use interpolation there if 'middle_initial' (which i assume is a column in the db) would work too? is it b/c <pre>middle_initial + " " unless middle_initial.nil?</pre> wouldn't work? or would it?
this site is amazing, btw!
Concatonating strings like that will cause an error if middle_initial returns nil, which is why I chose to use string interpolation.
Hi Ryan, I think
name += middle_initial + ". " unless middle_initial.nil?
still work, won't cause any error even if middle_initial returns nil.
B/c the priority should be:
(name += middle_initial + ". ") unless middle_initial.nil?
Please let me know if I'm wrong.
I'm using
def name
[self.first_name, self.last_name].compact.join(' ')
end
:)
This episode has been updated for Rails 5 as a blog post. Refactoring User Name in Rails 5 App
bikin ngelu