DEV Community

Cover image for Code Smell 36 - Switch/case/elseif/else/if statements
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

5

Code Smell 36 - Switch/case/elseif/else/if statements

First programming lesson: Control structures. Senior developer lesson: avoid them.

Problems:

  • Too many decisions together

  • Coupling

  • Duplicated code

  • Violation of Open/Closed Principle.

  • A new condition should not change the main algorithm.

  • Nulls

Solutions

  1. Polymorphism

  2. Create hierarchies/compose objects following Open closed principle.

  3. Use State pattern to model transitions.

  4. Use Strategy Pattern/Method Object to choose for branches.

Examples

  • Discrete Values

  • State transition

  • Algorithm choice.

Sample Code

Wrong

class Mp3Converter {
convertToMp3(source, mimeType) {
if(mimeType.equals("audio/mpeg")) {
this.convertMpegToMp3(source)
} else if(mimeType.equals("audio/wav")) {
this.convertWavToMp3(source)
} else if(mimeType.equals("audio/ogg")) {
this.convertOggToMp3(source)
} else if(...) {
// Lots of new clauses
}
view raw converters.js hosted with ❤ by GitHub

Right

class Mp3Converter {
convertToMp3(source, mimeType) {
const foundConverter = this.registeredConverters.
find(converter => converter.handles(mimeType));
// Do not use metaprogramming to find and iterate converters
// since this is another problem.
if (!foundConverter) {
throw new Error('No converter found for ' + mimeType);
}
foundConverter.convertToMp3(source);
}
}

Detection

Since there are valid cases for If/else usages, we should not pull the plug and forbid these instructions. We can put a ratio of if statements/other statements as a warning instead.

Relations

More info

Credits

Photo by Adarsh Kummur on Unsplash

If debugging is the process of removing software bugs, then programming must be the process of putting them in.

Edsger Dijkstra

DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping

Top comments (3)

Collapse
 
richardsprins profile image
Richard S Prins Jr.

The code isn't at all different, not sure of the intent here?

Collapse
 
mcsee profile image
Maxi Contieri

If you see the code exactly the same is due to a bug in dev.to cache. Try refreshing the cache

Collapse
 
moopet profile image
Ben Sinclair

It's been a couple of years and it's definitely the same for me :)

Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

👋 Kindness is contagious

Delve into a trove of insights in this thoughtful post, celebrated by the welcoming DEV Community. Programmers of every stripe are invited to share their viewpoints and enrich our collective expertise.

A simple “thank you” can brighten someone’s day—drop yours in the comments below!

On DEV, exchanging knowledge lightens our path and forges deeper connections. Found this valuable? A quick note of gratitude to the author can make all the difference.

Get Started