我正在阅读Sandi的“面向对象的设计”一书,她的主要原则之一是“单一责任”。下面是她书中的一个例子,在书中,她对代码进行了重构,以说明这一点。
def gear_inches
foo = some_intermediate_result * wheel.diameter
enddef gear_inches
foo = some_intermediate_result * diameter
end
def diameter
wheel.diameter
end通过这种重构,我如何确定在得到堆栈级别错误之前,我可以重构一个具有单一责任的方法?例如
def gear_inches
foo = some_intermediate_result * diameter
end
def diameter
wheel.diameter * calculation
end
def calculation
wheel.calculation * another_calculation
end
def another_calculation
wheel.another_calculation * yet_another_calculation
end
def yet_another_calculation
wheel.yet_another_calculation
end以此类推。如何在不滥用单一责任设计的情况下确定嵌套函数的级别?
发布于 2014-11-21 19:13:18
这将显示导致堆栈溢出的嵌套调用的数量:
def my_function a
my_function(a + 1)
rescue Exception => e
puts "a is #{a}"
puts e.message
end
my_function(1)在以下方面的成果:
a is 8732
stack level too deep因此,在8,732次调用后,会出现堆栈溢出。
将导致堆栈溢出的嵌套调用的实际数量取决于许多因素(例如传递的参数等),但通常,在手动创建嵌套调用时,我认为您不需要担心堆栈级别。堆栈溢出通常由递归调用造成。
https://stackoverflow.com/questions/27068282
复制相似问题