The Switch Block – Break, Default, Fail-Through Allows Hierarchical code execution

The Switch Block allows to execute some code based on the value itself without a boolean check.

Consider the block of code below

function colour (id)
{
switch(id){
case 1:
this.colour = "red";
case 2:
this.colour = "blue";
case 3:
this.colour = "yellow";
case 4:
this.colour = "green";
}
}

var col = new colour(2);
console.log(col.colour);

The result of the above code is “green”.

The case keyword and its value is just a label for an executable “starting point”.

Adding the break keyword allows to exit the switch statement immediately ensuring that one and only case action is taken.

function colour (id)
{
switch(id){
case 1:
this.colour = “red”;
break;
case 2:
this.colour = “blue”;
break;
case 3:
this.colour = “yellow”;
break;
case 4:
this.colour = “green”;
break;
}
}

var col = new colour(2);
console.log(col.colour);

The result is now “blue”;

If you want same action for similar cases, the more than one case can be stacked before the action. Since JavaScript does not care about case order, the cases can be moved appropriately.

function colour (id)
{
switch(id){
case 4:
this.colour = “red”;
break;
case 5:
this.colour = “blue”;
break;
case 3:
this.colour = “yellow”;
break;
case 1:
case 2:
case 6:
this.colour = “green”;
break;
}
}
var col = new colour(1);
console.log(col.colour);

The result is ‘green’.

The Default case help to deal with unavailable value in the switch statement.

function colour (id)
{
switch(id){
case 4:
this.colour = "red";
break;
case 5:
this.colour = "blue";
break;
case 3:
this.colour = "yellow";
break;
case 1:
case 2:
case 6:
this.colour = "green";
break;
default:
this.colour = "white";

}

var col = new colour(100);
console.log(col.colour);

Result is white.

There is no need to add the break as default is the last one.

An interesting application of the Switch block is the hierarchical code execution via the use of fail-through.
An organised switch block will add least common properties first and common properties last.

Let’s say the more senior an employee is in the organisation, the more privileges the latter has.

function getPrivileges (level)
{
switch(level){
case "Chairman":
this.IsFounder = true ;
case "Director":
this.CanAttendBoardMeeting = true;
case "Department Manager":
this.IsResponsibleforDepartment = true;
case "Line Manager":
this.IsResponsibleForTeam = true;
case "Staff":
this.IsStaff = true;
}
}

var staff= new getPrivileges("Director");
console.log(staff);

Result is
getPrivileges {CanAttendBoardMeeting: true, IsResponsibleforDepartment: true, IsResponsibleForTeam: true, IsStaff: true}

The switch statement started at “Director” and experienced fall through acquiring the lower ranks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s