How to Detect and Eliminate Code Smells in Real-Time

Posted by Matthew Grant on October 26, 2022
How to Detect and Eliminate Code Smells in Real-Time

Code smell – finding something in the source code that might indicate a deeper problem – is one of the most common yet elusive challenges developers and IT organizations face. Why? Because there is no single solution for detection or elimination of code smells, and it can’t be done in batches.

Instead, code smells needs to be investigated on an individual basis — an inefficient undertaking in the eyes of developers. But when left unaddressed, code smells can easily develop into issues that degrade application performance, increase technical debt, and make it more difficult for IT teams to provide value over time.

In the sections that follow, we’ll dive deeper into the concept of "code smell" — what it is, what it looks like in practice, and how developers can proactively address it to reduce any negative impact.

Quick Takeaways

  • Code smell indicates larger problems with source code — mainly that it doesn’t align with developer best practices and/or organization standards.
  • Code smell is a likely (but not definite) indicator of a problem, meaning it needs to be investigated and confirmed.
  • When ignored, code smell builds up to become code rot, which increases technical debt and slows down development velocity.
  • The best way to detect and eliminate code smell in real time is to build the detection process into SDLC processes and automate as much as possible, preferably with a software tool to help.

Code Smells 101: Definition and Types

What are code smells?

Code smells are any indicator that there’s a bigger problem with your code. They arise from a number of causes, including developer ignorance (i.e., a new developer or a not very good one) , rushed coding (someone just trying to “get the job done”), or unclear standards (company didn't outline them for developers).

In any case, code smells are a sign that source code is messy or otherwise does not meet best practice standards in the developer world or at your organization.

Some of the most common types of code smells include:

  • Duplicated code - Similar or identical code placed in more than one location
  • Long parameters - Extensive list of parameters indicating a single method or class has too many responsibilities
  • Lazy class - Underutilized code that unnecessarily increases complexity
  • Long methods/classes - Long code that takes too much time to read and understand
  • Code comments - Excessive comments indicating that code is not self explanatory

The Code Smell Dilemma

It's the symptomatic nature of code smells that makes dealing with them complicated. They aren’t bugs or code errors — a specific issue with a clear fix. Code smells are a symptom that suggests low quality code. They can mean that there’s a larger problem, but sometimes they don’t. Coding is nuanced, and there are times when imperfect or “bad” code is necessary in a specific scenario.

For this reason, it can’t be assumed that a code smell is a sure sign of a problem — just that it’s a likely one. But for all that, they can't simply be ignored. Code smells have to be investigated and confirmed before they can be eliminated.

This, of course, doesn’t always (or even often) happen. There are plenty of scenarios where busy developers need to work at a rapid pace with little time to investigate code smells they encounter along the way.

When left untreated, however, code smell can eventually become code rot, where code degrades over time causing the initial problem to grow and grow. Code rot increases technical debt and inevitably impedes the development process, often slowing velocity or creating roadblocks to successful deployment of new updates.

CAST Software demonstrates how the accumulation of code smell and code rot increases technical debt and lowers business value.

Graph demonstrating how the accumulation of code smell and code rot increases technical debt and lowers business value.

Technical debt compounds as developers compensate for smelly code without actually doing anything to eliminate it. Code smell needs to be taken seriously and addressed continuously in order to keep applications optimized and basically healthy. Unfortunately, addressing it won’t happen so long as the job remains time consuming and undefined.

Developers need a streamlined, straightforward way to detect and eliminate code smell in real time — a way that doesn’t disrupt the work they’re doing.

How to Detect and Eliminate Code Smells in Real-Time

Once a code smell is detected, there are a few different response options. First, it can be left alone with no action taken. This, as we know, puts organizations at risk for code rot and buildup of technical debt.

The next option is to investigate, rather than ignore, the smell and then to make a conscious decision not to deal with it. It may be, for example, that the code smell can’t be feasibly fixed. Of course, it could also be the case that it did not indicate a true problem.

The third option is refactoring — the primary way in which problematic code smell is eliminated.

Co-author of the book Refactoring for Software Design Smells, Dalhousie University assistant professor Tushar Sharma visualizes the three potential response options to code smell this way: no action, discard, or refactor.

Code Smell Action OptionsRefactoring changes code to fix its fundamental issues without impactingthe  software's core functionalities or how it behaves. Regular refactoring helps maintain code hygiene and aligns code with internal standards and architectures.

The key is to build refactoring into the SDLC so that it happens regularly and never feels like an overwhelming, unexpected undertaking. Indeed, it should happen any time a new update or application feature is released.

When developers know that code smell elimination is a standard step in the process (i.e., they’re expected to do it and know when it’s supposed to happen), it becomes the norm rather than an ad-hoc task that always gets tossed to the side for later.

All this said, even regular manual refactoring can be a tedious and time consuming process. For this reason, the best way to continuously detect and eliminate code smells in real-time is with a software tool that can automate the process.

Tools dedicated specifically to code cleanup do this well. An even better approach is to pair such tools (or your manual code smell cleanup process) with a high-visibility value stream management platform that drives continuous improvement with a focus on delivering business value through IT.

Subscribe to the LeanIX Blog and never miss a post again!

Related Posts