java - 如何提高代码质量(大部分重复)

标签 java

我有一组变量是通过古代遗留代码中的大型方法传入的......

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/

相关文章:

java - 如果满足我的条件,Thymeleaf 就会中断循环

java - 正则表达式来查找括号和其中的数字。 java

java - JOOQ:动态加入条件

java - 访问资源服务器 Controller 内的 Spring OAuth 2 JWT 负载?

java - 如何在 JUnit 中检查我们期望返回的确切值是什么?

java - NullPointerException hibernate Generic Dao 与 Spring

java - 如何将一个对象转换为另一个对象

java - 我无法从 Java 的较低级别访问 "higher level"变量

java - 有没有办法将 Apache Velocity 与 .properties 文件一起使用?

c# - 使用谷歌财经API或雅虎财经API构建(延迟价格)交易系统