top of page

Switch Statement Fun


Hey kids! Let’s have a chat about how to properly use a switch statement. We are only able to use a switch is we are comparing a constant variable. Meaning - the value you are comparing must already be evaluated.

There is not a limit to the amount of cases your constant can match, however it is a good idea to always list a default value in case the expected constant value comes through as something different. Always account for edge cases in your code.

Let’s use our friend, the if statement as our base example.


We want to determine if a student is in a particular college, and connect the college color to the UI when the student opens their customized view. Let’s assume there are 3 colleges.

If we wanted to use an if statement, it would look like this:


if (Student.college == College.Business)
{
    listBlueBackground.add(Student);
}
else if (Student.college == College.Art)
{
    listYellowBackground.add(Student);
}
else if (Student.college == College.Nursing)
{
    listPinkBackground.add(Student);
}
else
{
    listWhiteBackground.add(Student);
}


Not bad, not bad. But we can do better. And if additional colleges are added, we can easily add new cases for each college.


switch(Student.college)
{
    case College.Business:
        listBlueBackground.add(Student);
        break;
    case College.Art:
        listYellowBackground.add(Student);
        break;
    case College.Nursing:
        listPinkBackground.add(Student);
        break;
    default:
        listWhiteBackground.add(Student);
        break;
}

In small examples like this, using an if statement or a switch statement can be equally accurate and efficient. The more into the weeds you get in the code, with nested ifs or switches, one or the other may stand out as easier to use. Always keep in mind that readability for the developer is important. You always want to code things so that any developer coming after you can follow what you did and why without a lot of effort. If your code gets mega complex, take a step back, talk to your duck, and look at it as if you don’t know what it does. Does it still make sense?


Now let’s decide we want to compare a student age in their profile object and add them to a specific age group.


We could do this using an if statement as shown here:



if (Student.age < 18)
{
    listUnder18.add(Student);
}
else if (Student.age > 18 && Student.age < 30)
{
    listOver18Under30.add(Student);
}
else if (Student.age > 30)
{
    listOver30.add(Student);
}

Now let’s look at a switch statement for this problem instead


switch (Student.age)
{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
	case 7:
	case 8:
	case 9:
	case 10:
	case 11:
	case 12:
	case 13:
	case 14:
	case 15:
	case 16:
	case 17:
		listUnder18.add(Student);
		break;
	case 18:
	case 19:
	case 20:
	case 21:
	case 22:
	case 23:
	case 24:
	case 25:
	case 26:
	case 27:
	case 28:
	case 29:
		listOver18Under30.add(Student);
		break;
	case 30:
	case 31:
	case 32:
		listOver30.add(Student);
		break;
	default:
		listNoAge.add(Student);
		break;
}

Yikes! That’s a lot of unnecessary code to use a switch statement. This is an example of when it would be more efficient to use the if statement instead of the switch.



This is also another consideration to review. Are you writing a bunch of code that can be condensed easily by using a different syntax type? Are you saving lines of code or expanding?


Hopefully this short review can help to discern the correct type of syntax to use when each type is called for.

Remember, Keep It Simple, Stupid (KISS) 💋😘



14 views0 comments

Comments


bottom of page