DEV Community

Discussion on: Why switch is better than if-else

Collapse
 
thefern profile image
Fernando B 🚀 • Edited

Great read. I particularly don't use switch too often unless I use more than 3 or 4 ifs to make code cleaner. To make switch statements even cleaner combine them with enums if your language supports them.

Switch in kotlin are nice:

when {
    x.isOdd() -> print("x is odd")
    x.isEven() -> print("x is even")
    else -> print("x is funny")
}