What Are Code Smells?
Published: 2020-07-20 15:26:52

I am a big fan of movies in general. One of the great movies I believe is "A Few Good Men". In that movie there's a scene where the defense attorney asks the defendant, a Marine, to turn in the manual to the page on where the mess hall (cafeteria) is. The defendant can't. It's not explicit in the manual. He then asks the defendant if he never ate all the time he was in the military. "No sir" comes the reply. Then the attorney asks him how he found the mess hall and he replies "I guess I just followed everyone else".

Code smells are often one of those topics. We are often left to just "absorb" it from other developers. Learning code smells is an important part of creating and maintaining quality code, so I want to take a few minutes and talk about them in general. In future blogs, I'll cover specific smells in more depth.

Code smells are pretty much what they sound like. When we detect a "bad smell" in our life, we can use the smell as a way to find something that's dreadfully wrong, such as an undetected spill, some rotting food, or heaven forbid, a dead rat in our air ducts. These smells help us find and eliminate these problems. Not every smell is indicative of a problem, but they usually are, although we may choose to ignore them.

![image](https://cdn-images-1.medium.com/max/800/0*BC6MeG2t54gINACX.jpg?)

Photo by [Derek Howard](https://www.instagram.com/bar_open_a/)

Code smells help us in the same manner. They indicate something we should look at closer. They aren't bugs. They don't have to be fixed immediately, they may be ignored temporarily or longer, and they may not indicate a serious problem, but they give us a metric that helps us find quality and maintenance issues in our code.

There are a number of "canonical" code smells, and many more that can be discovered and internalized by developers over their career. My favorite code smell is the various flavors of "Large Class" or "Large File". This is a code smell indicated by a class (or file in non-class languages) that is just too darn long. Like physical smells, this code smell usually starts small and gets worse over time. And in between "small" and "too large" there are many degrees that are difficult to identify as being exactly "wrong" but can be trending that direction.

What is "too large"? Well, like smells identifying "too stinky" is just too subjective to give a hard answer. If you saw a class that was 2,000 lines long, you'd almost certainly say that's "too long" but what about 40 lines? 100? 200? 500? It's impossible to detect which grain of sand turns a hill into a mountain, but at some point, you have a mountain.

Me personally, I use the general rule that 2 screen heights of code is acceptable. Anything over that I consider to be a code smell. That doesn't mean that if it's over 2 screen heights I automatically consider it bad code, but instead, I look at the code, and determine if there's a possible problem that can be improved. I may look for other code smells at that point as well since oftentimes multiple code smells go together.

In the future I'll look at some other code smells in more detail, but for now, we'll leave the topic with this basic coverage. If you aren't brushed up on your code smells, now may be a great time to look further into them, and consider them in the code you write.

Happy coding!

Signup for my newsletter [here](https://thinkster.io/?previewmodal=signup?utm_source=devto&utm_medium=blog&utm_term=whatarecodesmells&utm_content=&utm_campaign=blog).

Visit Us: [thinkster.io](https://thinkster.io?utm_source=devto&utm_medium=blog&utm_term=whatarecodesmells&utm_content=&utm_campaign=blog) | Facebook: @gothinkster | Twitter: @GoThinkster