What is Smelly Code? 5 Code Smells to Watch Out For

If you’re new to software development, you might have heard someone talk about “smelly code” and wondered what it meant.  After all, it’s not like code actually has a smell… and if it does, you probably need to clean your cooling fans.  “Smelly code” refers to code that is poorly written, hard to understand, and potentially full of bugs.  It can lead to a lot of problems–not just errors and bugs, but long development times, loss of productivity, and difficulty collaborating with other programmers.  So, what is “smelly code”, and how do you know if your code is smelly? 

Smell Check!

There are several common “code smells” that can lead to smelly code.  Here are five of the most common smells to watch out for!

Smell #1: The Pyramid Of Doom

The “pyramid of doom” occurs when code is indented several times while nesting loops, function calls, or conditional statements  inside of other loops, functional calls, or conditional statements. If you find yourself writing loops inside of loops inside of loops, your code is probably getting pretty smelly… rank, even.  Trying to decipher what happens at each level of the doom pyramid can be challenging, and nesting functions, conditionals, and loops inside of each other can easily lead to unnecessary code execution that slows your program down.

let(i = 0; i < 100; i ++) { 
	let(i = 0; i <50; i++) { 
		let(i = 0; i < 25; i++) { 
			console.log(“This is one smelly pyramid!”); 
		}
        }
}

Smell #2: Ctrl + C, Ctrl + V

Avoid copy-paste programming and keep your code DRY–Don’t Repeat Yourself!  Having the exact same code (or very similar code) in more than one place in your program makes it hard to maintain and increases the chance of introducing bugs.  If you have code that appears in more than one class or more than one file, move it into its own separate file to be imported or a function to be called.  That way, if you need to change how the code works, you only need to change it in one place.

Smell #3: I Don’t Know What This Does

Your variables, methods, and classes should have short, descriptive names!  If your code is difficult for you to read now because things aren’t named well, imagine how hard it will be for other people (or for you in the future)!  Trying to decipher what value a variable holds or what data type it is shouldn’t take more than a few seconds if your code isn’t smelly.  

let baNaNNaNNaNas = 5; //this is a clever JavaScript joke, but not terribly descriptive

let numOfBananas = 5; //this is much better

Smell #4: This Does Everything

If you find yourself writing a class or a method that does a lot of stuff, take a step back and evaluate if you are violating the Single Responsibility Principle.  A class or method should concern itself with only doing one thing, and doing it well.  If you find yourself writing a code block that is very, very long, it’s a sign that you probably need to break your code into smaller functional pieces. 

Smell #5: It Depends 

When functions, classes, and packages are tightly coupled, changes in one part of your application’s code can unexpectedly blow up other parts of the application.  This becomes a maintenance nightmare, so you need to watch out for this particular code smell.  You can tell if your code is becoming tightly coupled if one piece of code relies on other code for functionality, if it’s hard to modify or extend code without impacting another part of the application, or if a class accesses another class’s internal state, especially without the use of a getter, setter, or other method. 

public DependasaurusRex(Dependency firstDependency, OtherDependency secondDependency, DataDependency dataSourceDependency, ConfigDependency configurationStuff) { 
        //this class constructor sure needs an awful lot of other objects passed into it!!!
}

In Conclusion…

Now that you know five code smells to watch out for, you can use your knowledge to improve the quality of your code.  If you find your code becoming smelly, refactor it to make it simpler, more modular, and easier to understand.  This means following established coding standards and best practices, like DRY and the SRP, using meaningful variable/method/class names, and keeping a look out for the pyramid of doom and code that does too much!  It’s important to test your code as you’re writing it; this will also help identify problems before they start.  Happy (smell-free) coding! 

Add a Comment