简体   繁体   中英

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:

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:

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); LOG.warn(...)

In case of j8 you can also do some more variations.

You don't need to nest that statement, else if is your friend.

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 ? 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;
}    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM