Skip to content

Instantly share code, notes, and snippets.

@bnadlerjr
Forked from practicingruby/refactoring patterns
Created October 2, 2010 18:40
Show Gist options
  • Save bnadlerjr/607873 to your computer and use it in GitHub Desktop.
Save bnadlerjr/607873 to your computer and use it in GitHub Desktop.
Fork this and add your example
# = Extract Superclass
#
# == Before Refactor
class Department
attr_reader :total_annual_cost, :name, :head_count
end
class Employee
attr_reader :annual_cost, :name, :id
end
# == After "Extract Superclass" Refactoring
class Party
attr_reader :annual_cost, :name
end
class Employee < Party
attr_reader :id
end
class Department < Party
attr_reader :head_count
end
# == After "Extract Superclass" (using module)
module Party
attr_reader :annual_cost, :name
end
class Employee
include Party
attr_reader :id
end
class Department
include Party
attr_reader :head_count
end
# = Replace Nested Conditional with Guard Clauses
#
# == Before Refactoring
def get_payment_amount
if dead?
result = dead_amount
elsif separated?
result separated_amount
elsif retired?
result retired_amount
else
result = normal_pay_amount
end
result
end
# == After Refactoring
def get_payment_amount
return dead_amount if dead?
return separted_amount if separated?
return retired_amount if retired?
normal_pay_amount
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment