Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:50:28+00:00 2026-05-29T09:50:28+00:00

I have 10 cases in my switch statement. Each of them does the same

  • 0

I have 10 cases in my switch statement. Each of them does the same thing with the other except that the assignments are different. If I have to change 1 line of code (during development) in one case block then I have to change all other 9 cases manually.

Here are the scenarios of each case statements:

  1. Each case contains long lines of code with many function calls and assignments.
  2. Only variable assignments, function arguments, and if statement conditions vary.
  3. There is no pattern/sequence in the assignment of variables and function arguments.
  4. Adding helper functions and calling them on each case statements is almost impossible for some reason.

How do I optimize or shorten this?

To illustrate:

final int CONSTANT_A = 0;
final int CONSTANT_B = 1;
...
final int CONSTANT_J = 10;

int varA = 0;
int varB = 1;
...
int varJ = 10;

int anothervarA = 0;
int anothervarB = 1;
...
int anothervarJ = 10;

int action = 0;

switch(something) {
    case 1:
        ... long lines of code here
        // If I have to change the variables below
        // then I have to update all other variables in
        // other cases below
        varA = CONSTANT_J;
        anothervarA = CONSTANT_B;
        ... another long lines of code here
        int ret = someObject.foo(varA);
        ... do something with ret.
        action = 5;
        break;
    case 2:
        ... long lines of code here
        varB = CONSTANT_I;
        anothervarB = CONSTANT_C
        ... another long lines of code here
        int ret = someObject.foo(varA);
        ... do something with ret.
        action = 100;
        break;
    ...
    ...
    case 9:
        ... long lines of code here
        varI = CONSTANT_B;
        anothervarI = CONSTANT_A;
        ... another long lines of code here
        int ret = someObject.foo(varA);
        ... do something with ret.
        action = 100;
        break;
    case 10:
        ... long lines of code here
        varK = CONSTANT_A;
        anothervarJ = CONSTANT_F;
        ... another long lines of code here
        int ret = someObject.foo(varA);
        ... do something with ret.
        action = 4;
        break;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:50:29+00:00Added an answer on May 29, 2026 at 9:50 am

    Nothing obvious jumps out, except maybe to factor all that code into a set of classes (or an enum), and rely on polymorphism instead of switch to call the right one. So for instance, load your cases into a Map<Integer,Foo> — or even a List<Foo> if it’s not a sparse array — and then replace the switch with myFoos.get(something).whatever();.

    As for assigning the variables when you’re done, if they’re member variables you could have the Foos set them directly; if this is always called in a single-threaded environment, you could have foo.whatever() set up state and then have getters. If it’s in a multi-threaded environment, you could have whatever() return back a new object with those getters. Something like:

    FooResult result = myFoos().get(something).getResult(whatever, args);
    varA = result.getA();
    action = result.getAction();
    etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather long switch-case statement. Some of the cases are really short
I have a switch statement that has over 300 case statements. case 'hello': {
I have a switch statement in a factory that returns a command based on
Let's say that I have a switch statement in my thread function that evaluates
If I have a switch-case statement where the object in the switch is a
I have a switch statement similar to this one: switch (number) { case 1:
I have kept hearing this statement. Switch..Case is Evil for code maintenance, but it
I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
I have a UIActionSheet (sheet A for short) with a switch statement of which
I have an issue with accessing variables initialized in a switch-statement, and then using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.