Created
December 7, 2017 16:51
-
-
Save kellysutton/c33bc341f978e2c1b7cabc8fbc433e95 to your computer and use it in GitHub Desktop.
Refactoring: Removing Tangled Control Coupling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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