A cleaner way of checking conditions in Javascript
Introduction
Using `if-else` statements and `switch` cases in JavaScript can often lead to verbose code due to their repetitive and explicit nature. Let’s explore two examples of this verbosity:
- Multiple Conditions in `if-else` Statements:
When you have to evaluate multiple conditions, `if-else` chains become cumbersome. For instance, consider a scenario where you want to categorize a person’s age into different age groups. Using `if-else`, you might end up with a lot of repetitive code:
if (age < 18) {
category = "Child";
} else if (age >= 18 && age < 30) {
category = "Young Adult";
} else if (age >= 30 && age < 50) {
category = "Adult";
} else if (age >= 50) {
category = "Senior";
}
This code is verbose and prone to errors, especially if you need to adjust the age groups. Using a switch case can also be verbose when multiple cases share the same code block.
2. Switch Cases:
Switch cases are straightforward when you’re comparing a single value, but they can become verbose when you need to perform more complex comparisons. For example all of this code just to tell us the day of the week.
switch (dayOfWeek) {
case "Monday":
console.log("It's Monday!");
break;
case "Tuesday":
console.log("It's Tuesday!");
break;
case "Wednesday":
console.log("It's Wednesday!");
break;
case "Thursday":
console.log("It's Thursday!");
break;
case "Friday":
console.log("It's Friday!");
break;
case "Saturday":
console.log("It's Saturday!");
break;
case "Sunday":
console.log("It's Sunday!");
break;
default:
console.log("Invalid day of the week");
}
As shown, this approach results in a lot of indentation and can be challenging to read and maintain. In such cases, a more concise and readable approach, such as using object literals or function-based dispatching, can make the code less verbose and more maintainable.
In summary, while `if-else` and `switch` statements have their uses, they can become verbose and hard to manage in certain situations, especially when dealing with multiple conditions or complex comparisons. In such cases, it’s often better to explore alternative coding patterns that lead to more concise and maintainable code.
So let’s try “when”
In the Kotlin language we can use when statements which is their answer to the Java switch statement. Here is an example:
fun main() {
val number = "11"
val result = when (number) {
"12" -> { "not eleven?" }
"11" -> { "Bingo! It's eleven." }
else -> { "Invalid number" }
}
println(result)
}
In order to replicate this I have created the kotlin-when npm library. In this library, if a matching case is found, only the code in the respective case block is executed, and execution continues with the next statement after the when block.
This essentially means that we don’t need break statements at the end of each case block.
when(dayOfTheWeek, {
"Monday": () => console.log("It's a Monday",)
'Tuesday': () => console.log( "It's a Tuesday")
});
If you want to sign up to access more medium content please do so with the following link. This will enable me to write more stories through a small commission from Medium.