#11
Mar 28

Refactoring User Name Part 2

Testing and refactoring go hand in hand. Refactoring is all about improving code without changing its behavior. Testing is all about making sure you don't change the behavior while you are improving the code.
Download (13.1 MB, 6:34)
alternative download for iPod & Apple TV (9 MB, 6:34)
# models/user.rb
def full_name
  [first_name, middle_initial_with_period, last_name].compact.join(' ')
end

def middle_initial_with_period
  "#{middle_initial}." unless middle_initial.blank?
end

5 comments:

steve D Oct 05, 2007 at 04:12

Hi Ryan,

Testing is about proving that the code is fit for purpose.

Regression testing is about proving that the modified/new code does not impact on the existing code - which is what you are hinting at above.


Eric Anderson Oct 16, 2007 at 20:48

One other thing to consider is if first name or last name is an empty string. You can do something like:

[first_name, middle_initial_with_period, last_name].reject(&:blank?) * ' '

This will reject all blank values.


Syn Oct 23, 2007 at 10:47

I think you mean something like this:

[f, m, l].reject{|i| i.empty?}.compact.join(" ")

Yay Ruby. ^.^


David Beckwith Feb 07, 2008 at 16:58

Hello,
Great site and great coding! I would like to see a railscast on Rspec. Any plans for that?
David :)


Allen Feng Apr 21, 2008 at 02:07

I cannot get autotest to work on XP. I get error message "couldn't find HOME environment.." What should I add to the environment variable setting in Window?

Add your comment:

(required)

(not displayed)

(SKIP THIS ONE)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player