公開された関数(レベル0)があると仮定しましょう。この関数をさまざまなパラメーターで呼び出します。内部的には、この関数は2番目の関数(レベル1)を呼び出しますが、引数として3番目の関数(レベル2)を呼び出す以外は、指定されたパラメーターを使用しません。しかし、それは他のことをするかもしれません。
私の質問はです。中間層関数(レベル1)であまりノイズを発生させずに、引数を渡すにはどうすればよいですか?以下にいくつかの可能な方法をリストします。ただし、それらのいくつかはかなり醜く、完全性の理由でのみ存在することに注意してください。トピックに関する個人的な意見ではなく、確立されたガイドラインを探しています
# Transport all of them individually down the road.
# This is the most obvious way. However the amount of parameter increases the
# noise in A_1 since they are only passed along
def A_0(msg, term_print):
A_1(msg, term_print)
def A_1(msg, term_print):
A_2(msg, term_print)
def A_2(msg, term_print):
print(msg, end=term_print)
# Create parameter object (in this case dict) and pass it down.
# Reduces the amount of parameters. However when only reading the source of B1
# it is impossible to determine what par is
def B_0(msg, term_print):
B_1({'msg': msg, 'end': term_print})
def B_1(par):
B_2(par)
def B_2(par):
print(par['msg'], end=par['end'])
# Use global variables. We all know the pitfalls of global variables. However
# in python there are at least limited to their module
def C_0(msg, term_print):
global MSG, TERM_PRINT
MSG = msg
TERM_PRINT = term_print
C_1()
def C_1():
C_2()
def C_2():
print(MSG, end=TERM_PRINT)
# Use the fact that python creates function objects. We can now append those
# objects. This makes some more 'localised' variables than shown before. However
# this also makes the code harder to maintain. When we change D_2 we have to alter
# D_0 as well even though it never directly calls it
def D_0(msg, term_print):
D_2.msg = msg
D_2.term_print = term_print
D_1()
def D_1():
D_2()
def D_2():
print(D_2.msg, end=D_2.term_print)
# Create a class with the functions E_1, E_2 to enclose the variables.
class E(dict):
def E_1(self):
self.E_2()
def E_2(self):
print(self['msg'], end=self['end'])
def E_0(msg, term_print):
E([('msg', msg), ('end', term_print)]).E_1()
# Create a nested scope. This make it very hard to read the function. Furthermore
# F_1 cannot be called directly from outside (without abusing the construct)
def F_0(msg, term_print):
def F_1():
F_2()
def F_2():
print(msg, end=term_print)
F_1()
A_0('What', ' ')
B_0('is', ' ')
C_0('the', ' ')
D_0('best', ' ')
E_0('way', '')
F_0('?', '\n')
非常に多くのパラメーターと非常に多くのレベルの関数が存在する理由の完全な詳細を知らずに、完全な答えを出すことは困難です。ただし、一般に、渡すパラメータが多すぎると見なされますcode smell
。
一般に、関数のグループがすべて同じパラメーターを使用する場合、それらは何らかの方法で密接に関連しており、関連するすべてのメソッドがそのデータを共有できるように、クラス内にパラメーターをカプセル化することでメリットが得られる可能性があります。
TooManyParametersは多くの場合CodeSmellです。その量のデータを一緒に渡す必要がある場合は、データが何らかの方法で関連していて、独自のクラスにカプセル化する必要があることを示している可能性があります。別々に属する単一のデータ構造を渡すことは問題を解決しません。むしろ、アイデアは、一緒に属するものは一緒に保つということです。離れて属するもの、離れておく; OneResponsibilityRuleごと。
実際、他の関数にデータを渡すだけであれば、関数全体が完全に不要であることに気付くかもしれません。
class A(object):
def __init__(self, msg, term_print)
super(A, self).__init__()
self.msg = msg
self.term_print = term_print
def a_0():
return self.a_1()
def a_1():
return self.a_2()
def a_2():
print msg, term_print
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加