Created
November 4, 2021 09:15
-
-
Save elct9620/5958de0fdb31b7961670fb05cb90001b to your computer and use it in GitHub Desktop.
Ruby Block-based Refactor
This file contains 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 BadObject | |
def initialize | |
@internal = 'Coupling' | |
end | |
def perform | |
puts 'Start' | |
100.times do |i| | |
puts "Long Code ... #{i} #{@internal}" | |
end | |
puts 'End' | |
end | |
end | |
puts '=== Bad Object ===' | |
BadObject.new.perform |
This file contains 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 RefactorObject | |
def initialize(&block) | |
@i_dont_care = block | |
end | |
def perform | |
puts 'Start' | |
@i_dont_care.call | |
puts 'End' | |
end | |
end | |
class RefactorBadObject | |
def initialize | |
@internal = 'Coupling' | |
end | |
def perform | |
RefactorObject.new do | |
100.times do |i| | |
puts "Long Code ... #{i} #{@internal}" | |
end | |
end.perform | |
end | |
end | |
puts '=== Refactor Bad Object ===' | |
RefactorBadObject.new.perform |
This file contains 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 DecouplingAction | |
def initialize(idx, ref) | |
@idx = idx | |
@ref = ref | |
end | |
def perform | |
puts "Long Code ... #{@idx} #{@ref}" | |
end | |
end | |
class RefactorDecouplingObject | |
def initialize(ref) | |
@ref = ref | |
end | |
def perform | |
puts 'Start' | |
100.times { |i| DecouplingAction.new(i, @ref).perform } | |
puts 'End' | |
end | |
end | |
class RefactorDecouplingBadObject | |
def initialize | |
@internal = 'Coupling' | |
end | |
def perform | |
RefactorDecouplingObject.new(@internal).perform | |
end | |
end | |
puts '=== Refactor Bad Object ===' | |
RefactorDecouplingBadObject.new.perform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment