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?
This is the sixth article in a series of eight articles on Code Quality
- 1 - Introduction To Code Quality
- 2 - Introduction To Coding Standards - Java examples
- 3 - Five Important Coding Standards
- 4 - Best Practices in Static Code Analysis with SonarQube
- 5 - Code Review Best Practices
- 6 - What Are Code Smells?
- 7 - What Is Refactoring?
- 8 - Continuous Integration - 5 Important Questions or Tips
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:
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
- You make use of a
forloop, but do not increment/decrement the loop counters
- You access a
staticmember using an instance variable
- You make use of a
switchstatement, but do not have a
- You have a
switchstatement with just two
- You call
- In a situation where you need to use a
whileloop, you use a
- You name a class as
Exception, but do not extend the built-in
Exceptionclass, or a subclass of it
- You throw an exception in the
- You throw very generic exceptions
- You use a
catchblock, 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:
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
- Spring Framework for Beginners in 10 Steps
- Spring Boot for Beginners in 10 Steps
- Spring MVC in 10 Steps
- JPA and Hibernate in 10 Steps
- Eclipse Tutorial for Beginners in 5 Steps
- Maven Tutorial for Beginners in 5 Steps
- JUnit Tutorial for Beginners in 5 Steps
- Mockito Tutorial for Beginners in 5 Steps
- Complete in28Minutes Course Guide
Top 5 Recommended in28Minutes Courses
in28Minutes is helping 300,000 Learners across the world reach their learning goals. Click here for the complete catalogue of 30 Courses.