DEV Community

Cover image for How to eliminate if-else chain, for long life software

How to eliminate if-else chain, for long life software

Sameh Muhammed on October 10, 2022

Motivation All programing principles and paradigms aim to enhance code readability and maintainability, from OOP to functional, SOLID, D...
Collapse
Β 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard β€’

Agree with all your points.

Language specific, but in Kotlin the when expression is a massive improvment over if/else

val message = when (x) {
    in 1..10 -> "x is in the range"
    in validNumbers -> "x is valid"
    !in 10..20 -> "x is outside the range"
    else -> "none of the above"
}
println("validate($x): $message")
Enter fullscreen mode Exit fullscreen mode
Collapse
Β 
smuhammed profile image
Sameh Muhammed β€’

Yes, good tip but i don't use Kotlin that much, it's language specific but the idea is to make code more readable.

Collapse
Β 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard β€’

I wonder what other languages have a similarly good structure.
Not like switch case which don't return anything, something better.
Probably functional languages in general?

Thread Thread
Β 
alexmario74 profile image
Mario Santini β€’

Like switch expressions for Java v12 have a look

Thread Thread
Β 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard β€’

indeed yes, I didn't know they can return a value

Thread Thread
Β 
xfbs profile image
Patrick Elsen β€’

Rust has a nice match statement too, even though it is not functional.

match person {
    Person::Employee(employee) if employee.years() > 3 => true,
    Person::Employee(employee) if employee.age() >63 => true,
    Person::Intern(internet) if intern.name().first() == 'S' => true,
    _ => false
}
Enter fullscreen mode Exit fullscreen mode

I believe it is something that most functional language have and newer languages are starting to adopt it.

Collapse
Β 
moopet profile image
Ben Sinclair β€’

Just a heads up that the Markdown we use here supports syntax highlighting, and is generally more accessible than inserting an image of code. Images of text are an issue for people using screen readers, for example, and their content won't get picked up by the site's search facility.

You can add code blocks with 3 backticks: code block with colors example More details in our editor guide!

Collapse
Β 
smuhammed profile image
Sameh Muhammed β€’

Thanks! , great idea

Collapse
Β 
rob84 profile image
Robert β€’

I would say be aware of polymorphism. It's not wrong, but not the only concept.
The principle composition over inheritance is often a better choice, because you don't couple classes with an inheritance structure.
Sometimes it's a great mix to use inheritance for the structure and composition for the logic.

Collapse
Β 
peerreynders profile image
peerreynders β€’

Sometimes it's a great mix to use inheritance for the structure and composition for the logic.

i.e. prefer interface inheritance (implements; for polymorphism) over implementation inheritance (extends).

The original quote (GoF, 1994, p.20):

Favour object composition over class inheritance

talks about implementation inheritance.

Collapse
Β 
smuhammed profile image
Sameh Muhammed β€’

Actually, we talked about this principle here Composition vs Inheritance, take a look πŸ˜‰

Collapse
Β 
juanvegadev profile image
Juan Vega β€’

Good post.

Just to add something, there is another possible iteration when you have a set of fixed conditions (like the switch), for large number of options a map is a better and more readable approach.

Collapse
Β 
turowski profile image
Kacper Turowski β€’

I am SOOOO HAPPY to see your article. For once it's "how to do it better" and not "omigosh, you should NEVER nest if-s, here's why". Glad to see people who don't clickbait.

Collapse
Β 
smuhammed profile image
Sameh Muhammed β€’

Your comment made me happy also πŸ˜„,
You can follow me for more amazing content, and stay tuned πŸ˜‰

Collapse
Β 
turowski profile image
Kacper Turowski β€’

Such humility too, hahahaha! 😁

Thread Thread
Β 
smuhammed profile image
Sameh Muhammed β€’

πŸ˜ƒπŸ˜ƒπŸ˜„

Collapse
Β 
fabestah profile image
fabestah β€’

Great read πŸ’―

Collapse
Β 
hngvchnh1 profile image
HΖ°ng VΓ΅ ChΓ‘nh β€’

nice

Collapse
Β 
mcsee profile image
Maxi Contieri β€’

amazing post !

Collapse
Β 
terrydiana profile image
Terry-Diana β€’

This is a good tip

Collapse
Β 
marcello_h profile image
Marcelloh β€’

In the Fail early, the final result of your change is not the same.
Because it should fail at a negative price or a discount equal or higher than 50.

Collapse
Β 
smuhammed profile image
Sameh Muhammed β€’

Yeah, but it just example that helps illustrate the idea not more