Code Smell: It's Not That Simple

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

A Dog, sniffing some flowers

According to Martin Fowler:

A code smell is a surface indication that usually corresponds to a deeper problem in the system …
… smells don’t always indicate a problem …
… it’s easy for inexperienced people to spot them …

Although, here is what I believe would be an interesting definition of Code Smell:

Let’s say that there is a general code pattern or practice that has some objectively good applications and many others between good and bad ones. Defining if the current application being analyzed is good or bad depends on the specific context it is being inserted in.

Let's say that, among those applications of the same code pattern or practice, that there is a popular one which is known to have been used in the bad context for a long time. The popular usage is not supposed to be consistent through all the use-cases, but sometimes might be interpreted as such and more likely to be used consistently wrong.

The code pattern or practice that, without context, seems to have been used as the popular usage without proper thought or analysis (like, for example, blindly copying it), is called a “Code Smell”.

A "Code Smell" is a code pattern or practice that seems to have been used as the popular usage without proper thought or analysis

It is very hard, if not impossible, to say if that practice was more likely to have currently been used under the good or bad application because there's no evidence supporting any of the possibilities, it is all a "smell" (or “gut feeling”) that something may or may not be right.

Once there is at least one weak evidence pointing out that the application of the code pattern or practice is more likely to be a bad one, we call it a "Bad Smell". A Code Smell can be a “Bad Smell” when there is at least one weak evidence that the current usage seems to be the incorrect usage. There is still no strong evidence to conclusively prove anything and that’s why it is still a Code Smell, or more specifically a "Bad Code Smell".

A Bad Code Smell is when there's at least one weak evidence towards a known incorrect usage

Once there is at least one weak evidence pointing out that the application of the code pattern or practice is more likely to be a good one, we call it a "Good Smell". A Code Smell can be a "Good Smell" when there is at least one weak evidence that the current usage seems to be the correct usage. There is still no strong evidence to conclusively prove anything and that's why it's still a Code Smell, or more specifically a "Good Code Smell".

A Good Code Smell is when there’s at least one weak evidence towards a known correct usage

Even when we find some weak evidence that the code pattern or practice is more likely to have been applied in a good way, it might still be a "Code Smell" because we still may not have enough information or context to prove the real purpose behind the use of that code pattern or practice and we couldn't be able to infer that purpose the first time we looked at it. Maybe the developer has left the project, the abstraction is not clear enough, the Hidden Documentation doesn't provide enough data... The point is that we can't reasonably conclude if the usage was right or wrong given the circumstances of the original implementation, even if it looks like it is.

We are too optimistic. We assume that the code is right and some pattern or practice might be wrong, but we never think for a second that the code can be wrong and some pattern or practice might be right. When we can't conclude reasonably if either something we are analyzing is right or wrong, then it should be just a "Code Smell". Just because we believe on, it doesn't mean that something is right or wrong, the likelihood is the same for both assumptions unless we are able to provide a strong evidence that points out to an objective and realistic conclusion.

We are too optimistic. We assume that the code is right and some pattern or practice might be wrong, but we never think for a second that the code can be wrong and some pattern or practice might be right

Sometimes we need to live with assumptions because there’s not enough time or resources to remove them. Whenever possible, though, it is recommended to remove as many assumptions as possible to make sure we have the best understanding of the questions, otherwise, there is a high likelihood of reaching the wrong conclusion and a bad answer.

Conclusion

Separating a "Good Smell" from a "Bad Smell" may not yield a lot of benefits, but at least it has the potential to drive some discussions about our ability to find things out of place using heuristics.

Code Smell exist everywhere, it should be our task to investigate them, find out which type of smell that is and fix it.

Or not.


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

If you want to support more posts, buy me a coffee in Bitcoin: 1223ieANXAG8aQqU7i2e6bmtok7Y6jQBhz