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 1078467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:43:44+00:00 2026-05-16T21:43:44+00:00

I inherited a largish JavaScript (MooTools) application for browsing and configuring products. In it

  • 0

I inherited a largish JavaScript (MooTools) application for browsing and configuring products.

In it there’s a function that tracks the products’ configured state. Other functions are called depending on what state the product is in.

I extended it to add some new features, but have ended up with a mess of nested conditionals inside a big switch statement.

My question is, how do you refactor conditional logic? Is there a general (or JS specific) pattern / technique for tracking state?

Update: here’s a pseudo-version of the function to illustrate the problem:

switchToVariation: function(link) {

  // set the variables      
  // before switch, set state    

  switch ( nextType ) {   
    case 'type1':
      if ( prevType == 'type1' ) {        
        if ( nextIsSameVariation ) {            
          // do stuff                                           
        } else {                                                                              
          if ( nextIsSomethingElse ) {              
            // do stuff
          }          
          // more stuff                      
        }
      } else {        
        // different stuff        
      }
      break;

    case 'type2':        
      if ( prevWasType1 ) {
        // stuff
      } else {
        if (nextIsSameStyle) {
          // stuff
        }
        // more stuff
      }    
      break;    

    default:                       
      // default stuff        
  }          

  // after switch, set state

}
  • 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-16T21:43:45+00:00Added an answer on May 16, 2026 at 9:43 pm

    The primary answer, of course, is: Break it up into smaller pieces, where each piece does one thing well.

    It’s hard to be more specific without seeing an example, but you said you have a “…mess of conditionals inside a big switch statement…” which definitely offers an opportunity for improvement: You can move the content of each case into its own function.

    If you do that, you have two choices: Either make the functions work purely with arguments you pass into them, which tends to keep things more modular, or make the functions closures that manipulate the data in the outer function. I’d say the latter is less ideal — for these purposes — but it may be quicker to do and so be a “quick win.”

    Say we originally had:

    function myBigFunction() {
        var v1, v2, v3;
    
        /* ...set v1, v2, and v3 to various things...*/
    
        switch (condition) {
            case foo:
                /* ...do lots of stuff involving v1 and v2...*/
                break;
    
            case bar:
                /* ...do lots of stuff involving v1 only...*/
                break;
    
            case charlie:
                /* ...do lots of stuff involving v2 and v3...*/
                break;
        }
    }
    

    Then:

    The first option: Completely independent functions:

    function myBigFunction() {
        var v1, v2, v3;
    
        /* ...set v1, v2, and v3 to various things...*/
    
        switch (condition) {
            case foo:
                doFooStuff(v1, v2);
                break;
    
            case bar:
                doBarStuff(v1);
                break;
    
            case charlie:
                doCharlieStuff(v2, v3);
                break;
        }
    }
    
    function doFooStuff(v1, v2) {
    }
    
    function doBarStuff(v1) {
    }
    
    function doCharlieStuff(v2, v3) {
    }
    

    …and of course, the odds are that you’d need to have those functions return something and then handle that, so for instance:

        case foo:
            v1 = doFooStuff(v1, v2);
            break;
    

    …if doFooStuff needs to update v1. If it needs to update v1 and v2, you could return an array, or make v1 and v2 properties on an object you pass into doFooStuff, etc. Here’s an example of using properties: Replace the v1 and v2 properties with an object (vdata) which has v1 and v2 properties on it:

    var vdata = {
        v1: "someInitialValue",
        v2: "someInitialValue"
    };
    

    …then your call to doFooStuff:

        case foo:
            doFooStuff(vdata);
            break;
    

    …which allows doFooStuff to update both v1 and v2. But be careful, you don’t want to create a kitchen sink with all of myBigFunction‘s variables, that would be sacrificing the modularity.

    The second option: Using closures and working directly with the parent function’s data:

    function myBigFunction() {
        var v1, v2, v3;
    
        /* ...set v1, v2, and v3 to various things...*/
    
        switch (condition) {
            case foo:
                doFooStuff();
                break;
    
            case bar:
                doBarStuff();
                break;
    
            case charlie:
                doCharlieStuff();
                break;
        }
    
        function doFooStuff() {
            // work with v1 and v2 directly
        }
    
        function doBarStuff() {
            // work with v1 directly
        }
    
        function doCharlieStuff() {
            // work with v2 and v3 directly
        }
    }
    

    Note that here, the various subroutines are closures inside myBigFunction and so they have direct access to all of myBigFunction‘s local variables. This is more modular, but not quite the full modular approach of the first option. It’s also like a local version of the issue of functions working with global variables: Side-effects are easy to introduce and can cause trouble.

    The first option is preferred, but if it’s impractical, the second option can at least help you make your mainline logic clearer. And of course, a combination of both may work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I inherited a shell-script application that is a combination of kshell scripts, awk, and
I inherited an ASP.NET application that builds pages with massive viewstate values. As I
I inherited a Basic MSI InstallShield ISM to install an application that has a
I inherited an ASP classic application created in Visual Studio 6.0, I believe. There
I inherited a project that needs to be mutithreaded. There is three major classes
I inherited an ASP.NET 1.1 application that was written about 8 years ago, the
I inherited an application that used forms to POST data. I am not very
I inherited a project that includes the following crazy JavaScript. I assume this is
Inherited an app with a page that has a link that calls the javascript
I inherited an Access application that I have made some changes to. When I

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.