Refactoring: Refactorings/Inline Class
Jump to navigation
Jump to search
If a class is not doing much; merge it with another class. Logically the inverse of Inline Class
Mechanics
- Determine the public protocol of the source class onto the absorbing class. Delegate all these methods to the source class.
- Change all reference from the source class to the absorbing class
- Test
- Use Move Method and Move Field to move features from the source class to the absorbing class until there is nothing left
- Hold a short, simple funeral service :)
Example
Before
class Person
attr_reader :name
def initialize
@office_telephone = TelephoneNumber.new
end
def telephone_number
@office_telephone.telephone_number
end
def office_telephone
@office_telephone
end
end
class TelephoneNumber
attr_accessor :area_code, :number
def telephone_number
'(' + area_code + ') ' + number
end
end
After
class Person
def area_code
@office_telephone.area_code
end
def areaa_code=(arg)
@office_telephone.area_code = arg
end
def number
@office_telephone.number
end
def number=(arg)
@office_telephone.number = arg
end
end
# ...