我在代码中使用了许多“OR”运算符,如何重构Java代码呢?

huangapple go评论140阅读模式
英文:

I use many "OR" operator how can i refactor code in JAVA

问题

我想重构我的代码。其中有许多“OR”运算符,我该怎么办?

public boolean validateRequireField(JSONObject tradeDeal) {
    boolean status = true;

    if (!tradeDeal.has("Symbol") || !tradeDeal.has("Deal") || !tradeDeal.has("Position")) {
        status = false;
    } else if (tradeDeal.get("Symbol").equals("") || tradeDeal.get("Deal").equals("")
            || tradeDeal.get("Position").equals("")) {
        status = false;
    }

    return status;
}
英文:

I want to refactor my code. That have many "OR" operator
what should i do ?

        public boolean validateRequireField(JSONObject tradeDeal){
		boolean status = true;

        if (!tradeDeal.has("Symbol") || !tradeDeal.has("Deal") || !tradeDeal.has("Position")) {
			status = false;
		} else if (tradeDeal.get("Symbol").equals("") || tradeDeal.get("Deal").equals("")
				|| tradeDeal.get("Position").equals("")) {
			status = false;
		}

        return status;
 }

答案1

得分: 0

我会按照这些线索做一些事情

public boolean validateRequireField(JSONObject tradeDeal){
    
    if(!tradeDeal.has("Symbol") || tradeDeal.get("Symbol").equals("") ) {
        return false;
    }
    if(!tradeDeal.has("Deal") || tradeDeal.get("Deal").equals("") ) {
        return false;
    }
    if(!tradeDeal.has("Position") || tradeDeal.get("Position").equals("") ) {
        return false;
    }
    
    return true;
}
英文:

I would do something along those lines

public boolean validateRequireField(JSONObject tradeDeal){
    
	if(!tradeDeal.has("Symbol")  || tradeDeal.get("Symbol").equals("") ) {
    	return false;
    }
    if(!tradeDeal.has("Deal")  || tradeDeal.get("Deal").equals("") ) {
    	return false;
    }
    if(!tradeDeal.has("Position")  || tradeDeal.get("Position").equals("") ) {
    	return false;
    }
    
    return true;
}

huangapple
  • 本文由 发表于 2020年8月20日 13:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63498990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定