Code Quality Basics - What Are Code Smells?

In this article, we have a closer look at code smells. We see what they are, and what you should do about them.

You will learn

  • What Are Code Smells?
  • What are the typical code smells?
  • How do you identify code smells?
  • How do you react to code smells?

Article Series

This is the sixth article in a series of eight articles on Code Quality

What Are Code Smells?

A code smell refers to a certain pattern in the code that signals there could be a defect lurking somewhere.

Static analysis tools are very good at detecting code smells. They analyze the patterns, and then see if they could lead to a problem.

For example, if you assign a variable in Java a null value, and then immediate call a method on it, a NullPointerException would result. That is nothing but a code smell!

If you do static analysis of that part of code, the tool would indicate there is a high chance of a defect in the code at that point.

Choosing static analysis tools is the best way to detect code smells in your application:

image info

SonarQube has great tools for detecting code smells. what we see in the snapshot above are the rules for Java, and a profile where there are 194 code smells present.

Typical Code Smells

What are examples of typical code smells? Let’s take the example of Java programs:

  • You make use of a deprecated method definition, that is annotated by @Deprecated
  • You make use of a for loop, but do not increment/decrement the loop counters
  • You access a static member using an instance variable
  • You make use of a switch statement, but do not have a default clause
  • You have a switch statement with just two case clauses
  • You call toString() on a String object
  • In a situation where you need to use a while loop, you use a for loop
  • You name a class as Exception, but do not extend the built-in Exception class, or a subclass of it
  • You throw an exception in the finally clause
  • You throw very generic exceptions
  • You use a try-catch block, where a try-with-resources might be a better option
  • You have unused private methods

When you see instances of such smells, you conclude that the developer does not fully understand what he/she was trying to do. Such situations could cause hidden bugs to exist in the code.

Other Kinds Of Code Smells

Other kinds of code smells could exists, especially in your JUnit code. These include:

  • Ignored unit tests
  • Empty test case
  • Test case without asserts

Handling Code Smells

Make sure that having zero code smells is an integral part of your Definition Of Done.

It is important that you do static analysis and manual code review on a regular basis.

Static analysis should be part of continuous integration build.

Do check out our video on the same topic:

image info


Code smells are patterns in code that could indicate hidden bugs. The two ways to detect code smells are static analysis, and manual code reviews. It is important to do both systematically, to keep the application free of code smells.

10 Step Reference Courses

Image Image Image Image Image

in28Minutes is creating amazing solutions for you to learn full stack and the cloud - Docker, Kubernetes, AWS, React, Angular etc. Click here for the complete catalogue of 30 Courses.

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Spring and Spring Boot Video Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

Software Design - Separation Of Concerns - with examples

Software architects and programmers love having Seperation of Concerns. What is it? Why is it important? Let's get started.

Object Oriented Software Design - Solid Principles - with examples

Software design is typically complex. Object oriented design takes it to the next level. There are a number of design patterns and other stuff to be aware of. Can we make things simple? What are the goals to aim for when you are doing object oriented design? SOLID Principles is a great starting point for Object Oriented Design.

Software Design - Open Closed Principle - with examples

Open Closed Principle is one of the SOLID Principles. You want your code to be easily extended. How do you achieve it with minimum fuss? Let's get started.

Software Design - What is Dependency Inversion Principle?

Dependency Inversion Principle is one of the important SOLID Principles. Dependency Inversion Principle is implemented by one of the most popular Java frameworks - Spring. What is it all about? How does it help you design good applications?

Introduction to Four Principles Of Simple Design

With agile and extreme programming, the focus is on keeping your design simple. How do you keep your design simple? How do you decide whether your code is good enough?

Software Design - Single Responsibility Principle - with examples

For me, Single Responsibility Principle is the most important design principle. What is Single Responsibility Principle? How do you use it? How does it help with making your software better? Let's get started.

REST API Best Practices - With Design Examples from Java and Spring Web Services

Designing Great REST API is important to have great microservices. How do you design your REST API? What are the best practices?

Designing REST API - What is Code First Approach?

Designing Great REST API is important to have great microservices. Code First approach focuses on generating the contract from code. Is it the best possible approach?