[英]Simplify this code: if-else
我有这段代码,我想知道是否有办法简化重复的if
语句:
convertedImgStr = getConvertedImage();
if (convertedImgStr.contains("::::::::::") || convertedImgStr.contains("null")) {
if (convertedImgStr.contains("::::::::::")) {
VIEW.updateStatus(STS_WARNING_IMG);
LOG.warn("Converter might have failed to transform the image correctly");
}
if (convertedImgStr.contains("null")) {
VIEW.updateStatus(STS_FAILURE_IMG);
LOG.warn("Converter failed to transform the image");
}
} else {
VIEW.updateStatus(STS_SUCCESS_IMG);
}
最简单的方法是:
if (convertedImgStr.contains("::::::::::")) {
VIEW.updateStatus(STS_WARNING_IMG);
LOG.warn("Converter might have failed to transform the image correctly");
} else if (convertedImgStr.contains("null")) {
VIEW.updateStatus(STS_FAILURE_IMG);
LOG.warn("Converter failed to transform the image");
} else {
VIEW.updateStatus(STS_SUCCESS_IMG);
}
如果它在某种方法中,请尝试这样:
if (!convertedImgStr.contains("::::::::::") && !convertedImgStr.contains("null")) {
VIEW.updateStatus(STS_SUCCESS_IMG);
return;
}
if (convertedImgStr.contains("::::::::::")) {
VIEW.updateStatus(STS_WARNING_IMG);
LOG.warn("Converter might have failed to transform the image correctly");
return;
}
VIEW.updateStatus(STS_FAILURE_IMG);
LOG.warn("Converter failed to transform the image");
(...)
您还可以尝试使用 java8 的一些技巧:
Status status = Optional.ofNullable(convertedImgStr).filter(str -> str.equals(":::::::::").map(str -> STS_WARNING_IMG);
status = Optional.ofNullable(convertedImgStr).filter(str -> str.equals("null").map(str -> STS_FAILURE_IMG);
if (Objects.isNull(status)) {
status = STS_SUCCESS_IMG;
}
VIEW.updateStatus(状态); LOG.warn(...)
在 j8 的情况下,您还可以做更多的变化。
您不需要嵌套该语句, else if
是您的朋友。
convertedImgStr = getConvertedImage();
if (convertedImgStr.contains("::::::::::")) {
VIEW.updateStatus(STS_WARNING_IMG);
LOG.warn("Converter might have failed to transform the image correctly");
} else if(convertedImgStr.contains("null")) {
VIEW.updateStatus(STS_FAILURE_IMG);
LOG.warn("Converter failed to transform the image");
} else {
VIEW.updateStatus(STS_SUCCESS_IMG);
}
出于兴趣,您是在寻找null
作为字符串文字,还是在检查字符串是否为null
? 如果是后者,那么你的方式是错误的,你需要这个:
} else if(convertedImgStr == null) {
试试下面更简单的代码
if (convertedImgStr.contains("::::::::::")) {
updateStatus(STS_WARNING_IMG,"Converter might have failed to transform the image correctly");
}
elsif (convertedImgStr.contains("null")) {
updateStatus(STS_FAILURE_IMG,"Converter failed to transform the image");
}
else {
updateStatus(STS_SUCCESS_IMG,"");
}
public void updateStatus(String constant,String logMessage){
VIEW.updateStatus(constant);
LOG.warn(logMessage);
}
convertedImgStr = getConvertedImage();
switch(convertedImgStr) {
case convertedImgStr.contains("::::::::::") : {
VIEW.updateStatus(STS_WARNING_IMG);
LOG.warn("Converter might have failed to transform the image correctly"); break; } ,
case "null": {
VIEW.updateStatus(STS_FAILURE_IMG);
LOG.warn("Converter failed to transform the image"); break;
},
default:{
VIEW.updateStatus(STS_SUCCESS_IMG); break;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.