Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Refactoring: Removing Tangled Control Coupling
class PayrollCalculator
def initialize(payroll, options = {})
@payroll = payroll
@skip_benefits = options[:skip_benefits]
@skip_taxes = options[:skip_taxes]
@skip_donations = options[:skip_donations]
end
def calculate
if !@skip_benefits
result = calculate_benefits(@payroll)
end
if !@skip_donations
result = calculate_donations(@payroll, result)
end
if !@skip_taxes
result = calculate_taxes(@payroll, result)
end
result
end
end
class PayrollCalculator
def initialize(payroll, options = {})
@payroll = payroll
@skip_benefits = options[:skip_benefits]
@skip_taxes = options[:skip_taxes]
@skip_donations = options[:skip_donations]
end
def calculate
if !@skip_benefits
result = BenefitsCalculator.calculate(@payroll)
end
if !@skip_donations
result = calculate_donations(@payroll, result)
end
if !@skip_taxes
result = calculate_taxes(@payroll, result)
end
result
end
end
class BenefitsCalculator
def self.calculate(payroll)
# The guts of the method formerly known
# as PayrollCalculator#calculate_benefits
end
end
class PayrollCalculator
def initialize(payroll, options = {})
@payroll = payroll
if options[:skip_benefits]
@benefits_calculating_klass = NullCalculator
else
@benefits_calculating_klass = BenefitsCalculator
end
@skip_taxes = options[:skip_taxes]
@skip_donations = options[:skip_donations]
end
def calculate
result = @benefits_calculating_klass.calculate(@payroll)
if !@skip_donations
result = calculate_donations(@payroll, result)
end
if !@skip_taxes
result = calculate_taxes(@payroll, result)
end
result
end
end
class BenefitsCalculator
def self.calculate(payroll)
# The guts of the method formerly known
# as PayrollCalculator#calculate_benefits
end
end
class NullCalculator
def self.calculator(payroll, result)
result
end
end
class PayrollCalculator
def initialize(
payroll,
benefits_calculating_klass: BenefitsCalculator,
**options
)
@payroll = payroll
@benefits_calculating_klass = benefits_calculating_klass
@skip_taxes = options[:skip_taxes]
@skip_donations = options[:skip_donations]
end
def calculate
# (unchanged from previous snippet)
end
end
class PayrollCalculator
def initialize(
payroll,
benefits_calculating_klass: BenefitsCalculator,
tax_calculating_klass: TaxCalculator,
donation_calculating_klass: DonationCalculator
)
@payroll = payroll
@benefits_calculating_klass = benefits_calculating_klass
@tax_calculating_klass = tax_calculating_klass
@donation_calculating_klass = donation_calculating_klass
end
def calculate
result = @benefits_calculating_klass.calculate(@payroll)
result = @donation_calculating_klass.calculate(
@payroll,
result
)
@tax_calculating_klass.calculate(
@payroll,
result
)
end
end
def print_address(include_zip_code)
if include_zip_code
street_1 + city + state + zip_code
else
street_1 + city + state
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.