Reputation: 4634
def self.method_A
a = xx
b = xx
c = xx
d = xx
complex_computing1(a, b, c, d)
end
def self.method_B
a = xx
b = xx
c = xx
d = xx
complex_computing2(a, b, c, d)
end
I need to refactor the above code, so I create A method called method_C
def self.method_C
return {
a: xx,
b: xx,
c: xx,
d: xx,
}
end
So, in method_A
it will become
def self.method_A
result = method_C
complex_computing1(result[:a], result[:b], result[:c], result[:d])
end
But I don't think it is the best way, can anyone give me more suggestion?
Upvotes: 0
Views: 50
Reputation: 1968
First of all, I assume you've abstracted your actual code – which makes it difficult to suggest actual working refactorings since it's unclear what is constant and what is the result of additional calculations. For the sake of my suggestion, I'll assume that all values denoted by xx
are the results of method calls and thus can't simply be turned into a constant like Shimu suggested.
In this case, then, the probably cleanest way to refactor would be this:
def self.method_A
complex_computing1(*method_C.values_at(:a, :b, :c, :d))
end
If they keys don't carry real logic, you can leverage the fact that hashes are ordered in Ruby (at least since Ruby 1.9) and do this:
def self.method_A
complex_computing1(*method_C.values)
end
Hope that answers your question.
Upvotes: 1