DEV Community

Alex Pushkarev
Alex Pushkarev

Posted on

1

Fix my code! Chain of if-else statements.

How to get rid of a chain of if-else statements in your code? There're several ways.

👉Ignore it

Not all the code that you write can and has to be pretty and clean. Sometimes it just doesn't worth investing into making a particular code cleaner. And don't forget that any refactoring creates risk of breaking a working code.

👉Design patterns

GoF patterns offer several different ways to deal with if-else chains. Check them out!

👉Use loop

Sometimes it is possible to replace if-else statements with a loop. On the video below I show how to do that

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more