改进找到的类似代码的代码
Improving code for similar code found
我将codeclimate传给了我的代码,得到了以下信息:
Similar code found in 1 other location
这是我的代码:
stradd = 'iterable_item_added'
if stradd in ddiff:
added = ddiff[stradd]
npos_added = parseRoots(added)
dics_added = makeAddDicts(localTable, pk, npos_added)
else:
dics_added = []
strchanged = 'values_changed'
if strchanged in ddiff:
updated = ddiff[strchanged]
npos_updated = parseRoots(updated)
dics_updated = makeUpdatedDicts(localTable, pk, npos_updated)
else:
dics_updated = []
其中 iterable_item_added
和 values_changed
重复。怎么改?
只需抽象参数并创建辅助方法:
def testmethod(name,localTable,m,ddiff,pk):
if name in ddiff:
npos = parseRoots(ddiff[name])
rval = m(localTable, pk, npos)
else:
rval = []
return rval
称呼它:
dics_added = testmethod('iterable_item_added',localTable,makeAddDicts,ddiff,pk)
dics_updated = testmethod('values_changed',localTable,makeUpdatedDicts,ddiff,pk)
注意:分解代码时要小心,您可能会引入错误(并使代码更易读:))。
另外:辅助方法强制传递大量局部变量。也许创建一个对象和成员变量会更简单。
在那种情况下,似乎"overkill"这样做是为了让您的审核工具闭嘴。
我将codeclimate传给了我的代码,得到了以下信息:
Similar code found in 1 other location
这是我的代码:
stradd = 'iterable_item_added'
if stradd in ddiff:
added = ddiff[stradd]
npos_added = parseRoots(added)
dics_added = makeAddDicts(localTable, pk, npos_added)
else:
dics_added = []
strchanged = 'values_changed'
if strchanged in ddiff:
updated = ddiff[strchanged]
npos_updated = parseRoots(updated)
dics_updated = makeUpdatedDicts(localTable, pk, npos_updated)
else:
dics_updated = []
其中 iterable_item_added
和 values_changed
重复。怎么改?
只需抽象参数并创建辅助方法:
def testmethod(name,localTable,m,ddiff,pk):
if name in ddiff:
npos = parseRoots(ddiff[name])
rval = m(localTable, pk, npos)
else:
rval = []
return rval
称呼它:
dics_added = testmethod('iterable_item_added',localTable,makeAddDicts,ddiff,pk)
dics_updated = testmethod('values_changed',localTable,makeUpdatedDicts,ddiff,pk)
注意:分解代码时要小心,您可能会引入错误(并使代码更易读:))。
另外:辅助方法强制传递大量局部变量。也许创建一个对象和成员变量会更简单。
在那种情况下,似乎"overkill"这样做是为了让您的审核工具闭嘴。