Webinar: How Workday Improved their Security Posture with Opsera | Register Now
DevOps

What is Code Smell & How to Avoid it

Published on
July 17, 2023

Empower and enable your developers to ship faster

Learn more
Table of Content

Digital technology is changing the way businesses operate, in every industry across the world. Digital services are now the new frontier. There is almost no organization whose business is not at least partially relying on software applications and data. Amid this significant transition, one thing is obvious: code is the language of the future. Code is the key aspect that defines the responsibility or role of your software, and your developers are the ones who give life to that code. And to maintain your competitive edge in the modern digital world, your developers need to build quality code. However, in reality, your developers may encounter many unavoidable challenges that may lead them to make mistakes during development, inadvertently creating the so-called Code Smells.

In this blog, let us discuss what code smells are, how they impact code quality and the best ways to avoid them:

What is Code Smell?

Code Smells are the traces in the code that indicates a deeper problem in the application or codebase. They are not bugs or errors but are tangible and observable violations of code design/development fundamentals that could eventually lead to poor code quality and technical debt. The code will still compile and perform as expected, but it may impede the development process and make the program vulnerable to performance and security issues in the future. Code smells indicate the underlying issues that lead to technical debt. 

A code smell is a surface indication that usually corresponds to a deeper problem in the system.

  • Martin Fowler

So, these code smells require some in-depth investigation and often reveal some critical problems in the code that require immediate remediation. To identify code smells and fix the underlying problem, it's imperative to know in detail the different types of code smells and the best practices to avoid them. In this blog, we'll cover all the key topics concerning code smells:

Common Types of Code Smells and How to Identify Them

Typically, code smells arise when developers fail to write the code on par with the design principles and standards. The high pressure to meet deadlines, lack of experience, and bypass of code reviews are some of the key reasons that can lead developers to make mistakes during code development. This end-up creating code smell. 

There are a vast number of code smells and they differ from project to project and developer to developer, and business to business. So, it’s imperative to thoroughly understand your organization’s design and development standards before handling any code smell. 

Here are the most common code smells to help you get a head start on improving your code quality:

1. Duplicate Code

Duplicate code is the most common code smell. It refers to the presence of identical or very similar segments of code in more than one place. Apart from very obvious copy-paste, there are subtle duplications such as repetitive code structures and parallel inheritance hierarchies that lead to duplicate code. Though duplicate code looks harmless, it becomes challenging to make multiple tweaks during feature updates. You may end up changing the code only in a few places, resulting in inconsistent code behaviors. 

Identifying and fixing this code smell is simple. All you need to do is extract the duplicate code to a separate method and call it everywhere the code is duplicated. Leverage the power of functions or loops to make code appear once in the program. 

2. Improper Names

Improper naming of variables, classes, and functions indicates that your code is not clean. One or two letter variable names, non-descriptive function names, excessively adorned class names, and in worst cases using different naming schemes across the codebase, lead to this code smell. This makes code hard to read, understand, and maintain. 

Names should be simple and accurate. Generally, variable names are short and descriptive. On the other hand, function and class names have at least one verb describing what they do, without including too many words.  

3. Dead Code 

A dead code is a section of code in the source code of a program that is of no use and is never reached or called. This is due to changes in requirements or corrections, making some parts of code obsolete. It can be a variable, parameter, field, method, or class. This code smell can cause codebase clutter and may later cause confusion in development, hampering speed. 

This is a very common code smell and is easy to fix. A static analysis code or modern IDE is the easiest way to identify and remove dead code. Delete the unused code and unnecessary files. You can recover them whenever you need by looking into the version history. Leverage inline class or collapse hierarchy to remove unnecessary classes. Remove parameters that have become obsolete. 

4. Middle Man

Middle man refers to the class that delegates work to another class and doesn't have any independent functionality. For instance, most of the methods of this class just call the methods of other classes. Middle man smell is the result of previous refactoring that moved functionality somewhere else, making the class useless. These classes increase the complexity of the code and create noise in the codebase. 

Fix this code smell by removing the middle man. Use the inline function to inline all the class methods that are not delegating to a caller. 

5. Long Parameter List

A long list of parameters in a method or class is a code smell. This hinders code readability and reusability and makes it prone to bugs and errors. In general, it is best to limit the number of parameters in a method to three or four. More than three or four parameters are not advisable and it indicates that the particular function, method, or class is trying to handle too many responsibilities. 

This code smell can be fixed by introducing a parameter object. Create a new object from the list of parameters that match and transfer it as a single argument. This makes code more readable and shorter. Moreover, any duplicate code can be revealed using this method. 

6. Data Clumps

Data clumps occur when a set of data items always appear together in multiple places, as fields in multiple classes or as parameters in multiple functions. This makes it hard to centrally control their behavior. The simple way to identify this code smell is when one item makes sense only in a group but not individually. 

Leverage Extract Class to turn repetitive class fields into an object. And, use Introduce Parameter Object or Preserve Whole Object to slim down the repetitive function parameters. 

7. Long Functions

This code smell occurs when a function grows too long. When a function gets too long, it means that it is responsible for too many tasks. While there’s no specific number of lines of code that makes a function too long, you know it when you see it. This code smell makes code harder to read, test, and reuse. 

Break the long functions into sub-functions, where each handles a single task. So, now the original long function is turned into a list of sub-function calls. This makes the code clean and easy to read. 

In our experience, the programs that live best and longest are those with short functions.

-- Martin Fowler and Kent Beck

8. Primitive Obsession 

Primitive Obsession occurs when the code uses primitives instead of small classes for simple tasks such as currency, phone numbers, ranges, and coordinates. This code smell arises when you use primitive types for representing an object in a domain. Other causes of this code smell are the usage of type codes for coding info and arrays, maps, or dictionaries for representing a specific object. It becomes a real challenge when these primitives increase along with their behavioral characteristics. And when you define the same primitives at various places with code duplication. 

Though primitive obsession is easy to identify, it is also easy to miss. Here are some of the ways to fix this code smell:

  • If the primitive fields logically belong together, replace the data value with the object. 
  • use Introduce Parameter Object or Preserve Whole Object if the primitives are being used as parameters
  • Use Replace Type Code with Subclasses or Replace Type Code With State Or Strategy If data is coded in variables
  • Use Replace Array with Object if there are arrays or other collections in the variables

9. Feature Envy

Feature envy occurs when a class with a method is overly dependent on another class. This indicates that the feature of the other class is better if defined in another location, such as in the envious method’s class, a super class of both classes, or another unrelated class.

Here are the steps to fix a feature envy:

  • If a function invokes methods on another object, use Move Function to move the function to the data 
  • If only a part of a function is envy, first use Extract Function and then Move Function
  • In case, if the function envies several modules, use Extract Function to break the function into several pieces, and then the Move Function

10. Comments

Code comments are a code smell. Comments in your code are definitely a good practice, but leaving the commented-out code in place is a code smell. Because Other the commented-out code would be a complete mystery for other developers and they would have no idea about what conditions should bring it back into the program. Comments can be helpful to explain the logic, but overusing comments for every step of the program creates excessive noise in your code and hampers readability. Your code should be self-explanatory and use comments as minimally as possible. 

Some of the best practices to fix this code smell are:

  • Use Extract Function to explain what a block of code does
  • Change Function Declaration to rename the function to explain what a method does
  • Introduce Assertion to state some rules about the required state of the system

Some other code smells that need your attention are:

  • Global Data
  • Mutable Data
  • Divergent Change
  • Shotgun Surgery
  • Repeated Switches
  • Loops
  • Lazy Element
  • Speculative Generality
  • Temporary Field
  • Message Chains
  • Insider Trading
  • Large Class
  • Data Class
  • Refused Bequest

Are these pungent code smells itching your nose? 

Let Opsera’s DevOps experts help you get rid of this bad smell! Contact Us Now!

3 Best Ways to Avoid Code Smell

While code smells are avoidable, development teams lack the time and resources to prevent them from happening. Developers, often, wait for issues to emerge and try to fix them instead of proactively searching for potential risks that may impact code health in the future. Consequently, code smells continue to stay in the system, even the smells that are easy to fix. Here are the five best practices that help you to prevent code smell:

1. Refactoring

Code refactoring is one of the most effective methods to avoid code smells and maintain code quality. It is a restricting process that aims to make code cleaner, concise, and efficient without changing its core functionality.   

Refactoring is “A change to the system that leaves its behavior unchanged, but enhances some nonfunctional quality—simplicity, flexibility, understandability, performance” as defined by Kent Beck. 

Refactoring is “A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior” as defined by Martin Fowler.

Regular code refactoring helps prevent design decay, improve code quality, and make code readable, maintainable, and less prone to bugs. The best time to conduct code refactoring is before making any major updates to the code. So, you will be cleaning the existing code before adding any new features. You should also refactor code after the code is deployed to production, so developers can spend some time cleaning the code before picking up another project. However, you should ensure that there is complete test coverage before refactoring a code. 

There are three main refactoring techniques to eliminate code smells. They are composing methods, simplifying method calls, and finally, refactoring by abstraction. Some of the most common tools for automating code refactoring are SonarQube, Visual Studio IntelliCode, Rider, and Eclipse IDE. 

2. Continuous Integration 

Continuous integration and continuous deployment (CI/CD) enable quick innovation cycles of software through the incremental release of code. It empowers the DevOps team to track small code changes. Any changes made to the code by any individual developer are noted, packaged, and tested, thus eliminating any scope for code smell. Continuous integration allows faster and more frequent feedback, which leads to lesser code errors and improved software quality. On the other hand, continuous delivery automates the release of code changes and updates, eliminating the manual errors that lead to code smells. 

3. Automated Code Review

Automated code review is an effective way to identify and address code smells. Code review also referred to as peer review, is a systematic examination of source code with an aim to identify code errors overlooked in the software development process and improve overall code quality. Pair programming, formal inspections, and informal walkthroughs are some of the forms of code review. However, manual coder review can be a tiresome and indefinite process. Leveraging automated code review tools, like Opsera’s Unified Insights Platform, provides a faster and more efficient way to ensure that your source code is on par with your predefined design principles, style guidelines, and best coding practices. Our platform provides end-to-end visibility across your software development lifecycle, giving you the ability to easily identify code smells and potential vulnerabilities. 

What To Do After Spotting a Code Smell

Say, you are investigating your code using automated code review and you sensed the code smell. At first, you need to acknowledge that the code smell only indicates an underlying issue. Then, you need to get a deep understanding of that code smell. Start with what exactly the smell is about. From there, you can start investigating the reasons. You need to find the exact reason for that smell, and decide whether it should be fixed right away or can be postponed. For this decision-making, you need to pool opinions from other developers, managers, and stakeholders. Opsera’s Unified Insights Platform enables you to populate code smell analytics targeting vertical roles including developers, managers, and executives, so you can make better technical and business decisions. This collaboration not only helps in taking insightful decisions, but also improves understanding of the codebase, coding standards, and the programming languages used. 

Catch Code Smells Early, Improve Developer Productivity with Opsera’s Insights Tool

Opsera’s Unified Insights tool is a powerful DevOps metrics dashboard that enables you to gain complete, end-to-end visibility into your source codebase, providing a clear visualization of risky code. The deep visibility helps you know what to fix, where to fix it, and when to fix it. With our tool at your disposal, you can easily catch code smell and enhance developer productivity. 

Schedule a demo today and see how significantly you can sense and stave off code smells!

Is your engineering team a performing leader or a laggard?

1-hour Assessment Workshop for Engineering Leaders

Register here

Read the 2023 Gartner Magic Quadrant for DevOps Platforms.

Download the report

Recommended Blogs