1

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?

7
  • 3
    If it does the same thing all the time, why do you even have a switch? Commented Oct 25, 2013 at 5:20
  • Do you like writing more code? Why even have a case switch statement when every case is the same? Commented Oct 25, 2013 at 5:22
  • Your first option is much clearer to read and understand. Commented Oct 25, 2013 at 5:22
  • There's nothing "better" about the second one. Unless you're getting paid by the LOC. Commented Oct 25, 2013 at 5:26
  • Please see the update,because of other case(updated now) ,i have to use switch-case :( Commented Oct 25, 2013 at 5:26

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

In terms of coding practice its wrong but in above situation,all executes the same thing.
His point is, result for both will be same, why to add same lines of code for each case.
@Prateek how is it wrong? Nothing is wrong with this answer.
@DavidWallace i m not talking abt his answer, i m talking abt my first approach.i feel that every case will be executed in my first approach which i felt wrong in terms of coding practice.Please get me correct,if i m wrong.
Why would you feel such a thing? Your first approach is hands down the better of the two. I don't know what you mean by "wrong in terms of coding practice".
|
2

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(); }

}

Comments

2

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.

Comments

-1

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.

1 Comment

No that's the point. All of the cases execute the same code. It is logically equivalent to saying else if (condition || condition || condition || condition). Cases could even be added after the fallthrough chunk and it would still function correctly as it will only fall through to the handling code if one of the cases is true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.