For more than half a century, computer programming has been a widespread activity throughout the world. Programming electronic devices to follow sets of logical instruction, i.e. coding, is difficult. Therefore it should come as no surprise that there’s a lot of bad code out there.

What Are Code Smells?

Code smells are signs that your code is not as clean and maintainable as it could be. They can derive from the misuse of syntax and almost always suggest your code needs to be refactored or redesigned to improve the overall quality of the program(s).

Trust me; I wrote some of it. I don’t just mean a JavaScript program missing one or two semicolons. I'm talking about the kind of code that makes your eyes hurt, that makes you want to throw your computer into the river. That’s right. I’m talking about code smells — a sign that your code needs to be refactored or redesigned.

More From JB LarsonWhat Is Gwei?

 

Common Code Smells

Not all code smells are created equal. Let’s take a look at some common code smells. 

 

Code Without Comments

Comments are important for coding projects of any kind. Comments are absolutely essential for large undertakings that will involve a number of individuals contributing to the same code base. The ability to check comments in real time allows collaborators to understand your variables, functions, and objects without requiring your direct input.

The solution here is simple. When you write code that might require explanation, add a comment explaining it.

 

Duplicate Code

It’s easy to understand why duplicate code is one of the most common code smells. Duplicate code happens when the same code is repeated multiple times in different parts of the application. Not only does this make your code harder to maintain, but it also increases the risk of bugs and security vulnerabilities.

The solution is to identify duplicate objects, ensure “the duplicate” is not a dependency of another part of your program and remove the duplicate.

Here’s an example of duplicate code. Imagine you are calculating the average price value of a dictionary. If a program were to contain duplicate code, the process carried out by this function would occur more than once. Duplicate code like that could be more efficiently executed as a single function that’s called multiple times. 

# function that returns avg price for priceDict
def avgFunc(targetDict):
    targetList = list(targetDict.values())
    sumOfRates = 0
    for rate in targetList:
   	 sumOfRates += rate

    avgRate = sumOfRates / len(targetList)
    return avgRate

 

God Objects

God objects are a code smell that is both common and problematic. God objects occur when a single class or program is so central to the system that it becomes a bottleneck, making changes difficult and risky. 

The solution is to refactor the class into smaller, more manageable classes, each with a single responsibility. The best approach is to remain mindful of this concept throughout the development process, and address potential God objects as they occur.

For example, in the case of our diagram, the centrally located geckoFuncz.py script is a God object.

God object flow chart for code smells story
Image: JB Larson / Built In

Dead Code

Dead code is code that’s no longer used or needed, but still remains in the application. Not only does it make your code harder to understand, but it also increases the risk of bugs and security vulnerabilities. 

With a complex application you may only be monitoring updates to certain libraries, which could allow a malicious actor to exploit vulnerabilities that have already been addressed (patched) by the original developer.

The solution is to remove the dead code completely. The best practice is to remove dead code immediately after writing code that replaces its functionality.

More Software Engineering PerspectivesWhat Is Schrödinger’s Cat?

 

How to Prevent Code Smells

Like most other issues in life, if you don’t address code smells early on, they’ll only get worse with time. Before you know it, what was originally just sloppy code from that side project you were working on has now become technical debt for that startup you’re trying to launch. 

Technical debt, or tech debt, is what happens when coders/management place a greater value on the short-term gains of completing tasks as quickly as possible while ignoring the long term consequences of rushing through components that should be paid careful attention.

 

Write Clean and Maintainable Code

Avoiding code smells is all about writing clean, maintainable code from the beginning.

Adopt good coding practices and keep your code organized and readable. This means using descriptive variable and function names, breaking down complex code into smaller, more manageable pieces and following standard coding conventions.

 

Avoid Needless Complexity

By the philosophical thought experiment Occam’s Razor, the simplest solution is often the optimal solution. Therefore it stands to reason that another way to prevent code smells is to avoid over-engineering and write the simplest code that can solve a problem. The more complex the code, the more difficult it will be to understand and maintain.

It’s also a good idea to regularly review and refactor your code to catch any code smells before they become a problem. This will help you identify areas that need to be improved and make changes before small problems snowball into larger issues down the road.

 

Test your Code

Another effective strategy for preventing code smells is to write tests for your code. This will help you catch bugs and ensure that changes to the code don’t break existing functionality.

 

Code Review or Pair Programming

Finally, code review or even pair programming can be an effective last resort if you’re still having trouble shaking some code smells. Having another set of eyes on your code might make you nervous, but it’s worth it if it helps you catch code smells and improve your code quality.

In conclusion, code smells are a clear sign that your code is not as clean and maintainable as it could be. They indicate areas that need to be refactored or redesigned to improve the overall quality of the code. By addressing code smells early on, you can prevent larger and more complex problems from developing down the road. And that, my friends, is the key to writing clean, maintainable code that will stand the test of time.

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us