英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论