#11
Mar 28, 2007

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

RSS Feed for Episode Comments 8 comments

1. 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.


2. 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.


3. 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. ^.^


4. David Beckwith Feb 07, 2008 at 15:58

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


5. 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?


6. Bvlgari replica Rings May 07, 2009 at 00:01

thanks


7. fake Tiffany Cuff Links May 07, 2009 at 00:26

I think you mean something like this


8. replica Tiffany Cuff Links May 07, 2009 at 00:27

 think you mean something like this.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

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