Code Smell: It's Not That Simple 🔊

Just because there's a smell, that doesn't mean it's a bad one

The picture of a dog sniffing some flowers

Note: This is a different take on the widely known definition of "Code Smell".

Let's say that there's a code pattern or practice that can be used to deliver good results. However, there are some circumstances where it can deliver bad results too. In order to know if the usage is good or bad, it's necessary to know the whole context.

As a code pattern or practice becomes more popular, it's also more likely to be used as a silver bullet and have a tendency to be used in every context.

Let's say that code pattern or practice can be easily used in a context that can deliver bad results. In that case, you can assume, without knowing the context, that whenever you see it, it will be more likely to be used incorrectly.

When you see a popular code pattern or practice, without context, that has a tendency to be used in a certain way, good or bad, you can call it a “Code Smell”.

A "Code Smell" is the occurrence of a popular code pattern or practice that has a certain tendency. Therefore, it's more likely that tendency is gonna be followed whenever it's used.

It's very hard to know, with a high degree of confidence, if something is used correctly or not without knowledge of the context. Unless you spend enough time to understand it, you can't jump to any definitive conclusion.

A smell is not a definitive conclusion, though, there are too many unknowns for that. Instead, it's the realization that when something has a tendency, it's more likely it will follow that tendency.

That's why you need to make investigations: to remove unknowns.

An investigation will increase your confidence to make more accurate conclusions. Each step of the way will put you in a better position to know if your assumptions were correct or not.

As more time is invested in an investigation, you might find additional evidence that the context where the code pattern or practice is being used is the wrong one. When that happens, you can call it a "Bad Smell".

A Code Smell is a "Bad Smell" when there's more evidence that the current usage is the wrong one. There's still not enough evidence to make definitive conclusions, though, that's why it's still a Code Smell.

A Bad Smell is when there's more evidence that indicates a code pattern or practice is being used in the wrong context

As more time is invested in an investigation, you might also find more evidence that the context where the code pattern or practice is being used is the right one. When that happens, you can call it a “Good Smell”.

A Code Smell can be a "Good Smell" when there's more evidence that the current usage of a practice that has the tendency to be used incorrectly is the right one. There's still not enough evidence to make definitive conclusions, though, that's why it's still a Code Smell.

A Good Smell is when there’s more evidence that indicates a code pattern or practice that has the tendency to be used incorrectly is being used in the right context

Sometimes, you need to live with unknowns because there are not enough opportunities to invest effort in an investigation to remove them. However, it's useful to remove as many assumptions as you can to make sure you have the best understanding of the question. Otherwise, there is a good chance you'll jump to the wrong conclusion and a bad answer.

Separating a "Good Smell" from a "Bad Smell" holds the potential to drive some thoughts about your ability to find things out of place using heuristics.

Code Smells exist everywhere, it should be your task to find out if it's a Bad Smell, then investigate and fix it.

Or if it's a Good Smell, just leave as it is.


Thanks for reading. If you have some feedback, reach out to me on Twitter, Facebook or Github.