extensions development for Joomla!™

 

Avoid using the switch() loop

The switch() loop, evaluates an expression, matching the expression's value to a case label, and executes statements associated with that case. This method can be a source for tricky bugs that usually happen, especially with novice programmers. Also, it's more of a procedural programming type than an object oriented one. There are also performance benefits using the Object Oriented Programing (OOP) approach of the switch loop, which can be applied using the Command Design Pattern.

Here you'll find the two methods and basic code for testing and future usage, as well as notes explaining the risks of using the switch() loop and, the benefits of using the Command Pattern approach in your future development processes.

The switch() loop

Syntax (basic)

switch (expression) {
  case labelA:
    statementsA // executed if labelA matches the expression
    break; // optional (but more than often required)
  case labelB:
    statementsB
  case labelN:
    statementsN
    break;
  default:
    statementsDefault
    break;
}

 

If lableA matches the expression passed as the switch argument, the statementsA is executed and the looping is stopped when it reaches the break; . Now the problem may come, if not defined to work as such, when the break; isn't set, which will result in a fall-through.

Let's use the labelB example to illustrate what happens when the programmer inadvertently misses to declare the break; If labelB matches the expression, both, statementsB and statementsN will be executed and the loop stops at the next break;, which in this case appears after the statementsN. let's see a more mundane example:

var language = "JavaScript";
switch (language) {
  case "Java":
    console.log("Language is Java.");
    break;
  case "JavaScript":
    console.log("Language is JavaScript");
  case "C#":
    console.log("Language is C#");
    break;
  default:
    console.log("No language matched the expression.");
    break;
}

 

The above switch statement will result in the following log in your browser console:

Language is JavaScript
Language is C#
undefined

 

Although "JavaScript" was correctly assigned to the variable language, what happens is that the inexistent break; will make the loop continue until it finds a break; and exits the loop. Sometimes this is intentionally done, but more than often, it is an error that will result in bugs within the application.

So how does the Command Pattern OOP approach would look like?

function defineLanguage(language) {
    "use strict";
     var languages = {
               "Java": function() {
                    return "Language is Java.";
               },
               "JavaScript": function() {
                    return "Language is JavaScript.";
               },
               "C#": function() {
                    return "Language is C#.";
               }
          };
 
     if(typeof languages[language] !== "function") {
          return "Could not find that language.";
//          throw new Error("Could not find that language."); // optionally throw an error
     }
     return languages[language]();
}
 
defineLanguage("JavaScript"); // returns "Language is JavaScript"

 

At the beginning, it might not be easy to switch your habits, but with practice and knowledge about the advantages you can take from using the OOP Command Pattern approach, you'll be prone to encounter less bugs and make your code more reusable and fast.

The above example is as simple as it can be, and as you start using it and make it one of your standard coding practices, you'll find many more advantages of this OOP design pattern approach against the procedural switch() that you're used to.


Shopping cart

The cart is empty

Login

Extension added to cart! x
Back to Top