Skip to main content

Ensure the default Clause is Placed at the End or Beginning of a Switch Statement

Medium
coding-practicesReadability

What is it?

This practice is triggered when a switch statement in TypeScript contains a default clause that is not the first or last clause. For improved readability and maintainability, the default clause should be positioned at the very beginning or the very end of the switch block. This facilitates a quick identification of the default behavior, thereby reducing cognitive load when reading the code.

Why apply it?

Placing the default clause in the middle of other cases can obscure the intended fallback behavior, making the code harder to follow and maintain. Aligning the default clause at either the first or last position ensures that unexpected or unhandled cases are clearly and visibly accounted for.

How to Fix it?

Reorder the cases of your switch statement so that the default clause is either the first or the last clause. This simple reordering improves both the readability and the overall structure of your code.

Examples

Example 1:

Negative

Incorrect implementation that violates the practice.

function handleStatus(status: number): void {
switch (status) {
case 0:
console.log("Status is zero");
break;
default: // Noncompliant: default clause should be the first or last one.
console.log("Unknown status");
break;
case 1:
console.log("Status is one");
break;
case 2:
console.log("Status is two");
break;
}
}

Example 2:

Positive

Correct implementation following the practice.

function handleStatus(status: number): void {
switch (status) {
case 0:
console.log("Status is zero");
break;
case 1:
console.log("Status is one");
break;
case 2:
console.log("Status is two");
break;
default:
console.log("Unknown status");
break;
}
}

Negative

Incorrect implementation that violates the practice.

function processDay(day: string): void {
switch (day) {
case "Monday":
console.log("Start of the work week");
break;
default: // Noncompliant: default clause should be the first or last one.
console.log("Not a standard weekday");
break;
case "Tuesday":
console.log("Second day of the work week");
break;
case "Wednesday":
console.log("Midweek day");
break;
case "Thursday":
console.log("Almost there");
break;
case "Friday":
console.log("Last day of the work week");
break;
}
}

Example 3:

Positive

Correct implementation following the practice.

function processDay(day: string): void {
switch (day) {
default:
console.log("Not a standard weekday");
break;
case "Monday":
console.log("Start of the work week");
break;
case "Tuesday":
console.log("Second day of the work week");
break;
case "Wednesday":
console.log("Midweek day");
break;
case "Thursday":
console.log("Almost there");
break;
case "Friday":
console.log("Last day of the work week");
break;
}
}