我有一组变量是通过古代遗留代码中的大型方法传入的......
public List<type> check (String required, String sales, String report,
Long passId, Long seatId, String capName, String vCapName,
String attName, Long vid) {
if(required != null) {
goodA = method(required);
goodB = methodTwo(required);
goodC = methodThree(required);
}
if(sales != null) {
goodA = method(sales);
goodB = methodTwo(sales);
goodC = methodThree(sales);
}
if(report != null) {
goodA = method(report);
goodB = methodTwo(report);
goodC = methodThree(report);
if(passId != null)
... you got the point....
}
传入 check 的变量只能是 1 个有效值,所有其他变量都将变为 null。 例如 检查("is",空,空,空,空,空...) 或者 检查(空,空,空,13212L,空,空,空,空)
现在我正在尝试将其重写为重复性较低且干净的内容,我想知道是否有人可以提供一些关于如何做到这一点的想法。
最佳答案
这样的事情怎么样?
List<Object> items = Lists.newArrayList(required, sales, report,
capName, vCapName, attName);
for(Object item : items) {
if(item != null){
methodOne(item);
methodTwo(item);
methodThree(item);
}
}
关于java - 如何提高代码质量(大部分重复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33533048/