简体   繁体   中英

What to use in switch-case statement when every case block has same code?

I have the code which can be done in two ways :

SWITCH WITHOUT BREAK(for common code in case)

ResultCodes resCode = ResultCodes.fromResponseCode(resultCode);
switch (resCode) {
case SUCCESS:
    if(userIdentity != null)
        Logger.logInfo(MODULE, "User Authenticated Successfully, UseIdentity: " +userIdentity);
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = SUCCESS;
    break;
case REDIRECT:
    url = resultMap.get(WebinKeyConstants.REDIRECTION_URL.val);
    Logger.logInfo(MODULE, "Redirecting to URL : " + url);
    resultMessage=getText(resCode.responseCode.toString());         
    RESPONSE = REDIRECT;
    break;
case AUTHENTICATION_FAIL:           
case USER_ACCOUNT_BLOCKED:
case USER_ACCOUNT_INACTIVE:
case USER_ACCOUNT_SUSPENDED:
case USER_ACCOUNT_TERMINATED:
case USER_ACCOUNT_BLOCKED_ALERT:
case OTP_SEND_SUCCESS:
case USER_PROFILE_NOT_FOUND:
         resultMessage=getText(resCode.responseCode.toString());
         RESPONSE = ERROR;
         break;
}

In above scenario there is only one break implying that all case will execute the same code.

WITH BREAK FOR EACH CASE(for common code in case)

Above scenario can be achieved in other way too, as shown below

ResultCodes resCode = ResultCodes.fromResponseCode(resultCode);
switch (resCode) {
case SUCCESS:
    if(userIdentity != null)
        Logger.logInfo(MODULE, "User Authenticated Successfully, UseIdentity: " +userIdentity);
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = SUCCESS;
    break;
case REDIRECT:
    url = resultMap.get(WebinKeyConstants.REDIRECTION_URL.val);
    Logger.logInfo(MODULE, "Redirecting to URL : " + url);
    resultMessage=getText(resCode.responseCode.toString());         
    RESPONSE = REDIRECT;
    break;
case AUTHENTICATION_FAIL:           
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = ERROR;
    break;
case USER_ACCOUNT_BLOCKED:
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = ERROR;
    break;
case USER_ACCOUNT_INACTIVE:
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = ERROR;
    break;
case USER_ACCOUNT_SUSPENDED:
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = ERROR;
    break;
case USER_ACCOUNT_TERMINATED:
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = ERROR;
    break;
case USER_ACCOUNT_BLOCKED_ALERT:
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = ERROR;
    break;
case OTP_SEND_SUCCESS:
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = ERROR;
    break;
case USER_PROFILE_NOT_FOUND:
    resultMessage=getText(resCode.responseCode.toString());
    RESPONSE = ERROR;
    break;
}

Which one is better to use? Is there any performance issue?

First switch block has less code and more readable. In case of performance issue, it is negligible.

This would be much clearer:

ResultCodes resCode = ResultCodes.fromResponseCode(resultCode);
if (resCode.isError()) {
     resultMessage = resCode.getResultMessage();
     RESPONSE = ERROR;
}

public enum ResultCodes {

    OK(false), AUTHENTICATION_FAIL(true);

    public final boolean isError;

    ResultCodes(boolean error) { isError = error; }

    public boolean isError() { return isError; }
    public String getResultMessage() { return name(); }

}

If you use the second form, then the next developer to see this code will spend hours looking at the individual lines to see what the differences are. Using the first form clearly states "there is no difference here, between these two cases".

This is a no-brainer. Use the first form.

Case statement uses fall through model it means it will execute every case after getting a true case and execute until it won't get a break statement. So in the first case, Suppose that you have found USER_ACCOUNT_BLOCKED_ALERT case true then it will execute OTP_SEND_SUCCESS and USER_PROFILE_NOT_FOUND case as well.

And in the Second case. if it found USER_ACCOUNT_BLOCKED_ALERT as true then it will execute only USER_ACCOUNT_BLOCKED_ALERT block because at end of the block it will find a break and it will get out of switch statement.

Now i think you will get an idea that what is happening.

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