[英]Simplify this code: if-else
I have this block of code and I was wondering if there is a way to simplify the repeated if
statements:我有这段代码,我想知道是否有办法简化重复的
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);
}
The simpliest way would be:最简单的方法是:
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 it is within some method try sth like this:如果它在某种方法中,请尝试这样:
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");
(...)
You can also try some tricks with java8:您还可以尝试使用 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(status); VIEW.updateStatus(状态); LOG.warn(...)
LOG.warn(...)
In case of j8 you can also do some more variations.在 j8 的情况下,您还可以做更多的变化。
You don't need to nest that statement, else if
is your friend.您不需要嵌套该语句,
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);
}
Out of interest, are you looking for null
as a string literal, or are you checking if a string is null
?出于兴趣,您是在寻找
null
作为字符串文字,还是在检查字符串是否为null
? If it's the latter, then you're going about it the wrong way, you'll need this instead:如果是后者,那么你的方式是错误的,你需要这个:
} else if(convertedImgStr == null) {
Try below code which is simpler试试下面更简单的代码
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.