Description

Code Smells are the indicators of problems within the source code, that need to be addressed during refactoring.

  • They are easy to find and fix.
  • But they may be just symptoms of the actual problem, that exists deep within the code.

Here is a list of common code smell types.

  • Bloaters
  • Object-Oriented Abusers
  • Change Preventers
  • Dispensables
  • Couplers

Bloaters

Bloater is a piece of code, method, or class that has gradually become too huge to manage, and needs immediate attention.

These evolved over time as the code becomes bigger and more complex, and nobody makes an effort to refactor.

Here is a list of such common code smells.

Code Smell Problem Solution
Long Method A method contains too many lines of code. Generally, any method longer than ten lines should make you start asking questions.

Use the mentioned refactoring techniques to avoid these kinds of code smells.

  • Extract Method
  • Replace Temp with Query
  • Introduce Parameter Object
  • Preserve Whole Object
  • Replace Method with Method Object
  • Decompose Conditional
Long Class A class contains many fields/methods/lines of code.

Use the mentioned refactoring techniques to avoid these kinds of code smells.

  • Extract Class
  • Extract Subclass
  • Extract Interface
  • Duplicate Observed Data
Long Parameter List More than three or four parameters for a method.

Use the mentioned refactoring techniques to avoid these kinds of code smells.

  • Replace Data Value with Object
  • Introduce Parameter Object
  • Preserve Whole Object
  • Replace Type Code with Class
  • Replace Type Code with Subclass
  • Replace Type Code with State/Strategy
  • Replace Array with Object
Primitive Obsession

This can include any of the below.

  • Use of primitives instead of small objects for simple tasks (such as currency, ranges, special strings for phone numbers, etc.)
  • Use of constants for coding information (such as a constant USER_ADMIN_ROLE = 1 for referring to users with administrator rights.)
  • Use of string constants as field names for use in data arrays.

Use the mentioned refactoring techniques to avoid these kinds of code smells.

  • Replace Parameter with Method call
  • Preserve Whole Object
  • Introduce Parameter Object
Data Clumps Sometimes different parts of the code contain identical groups of variables (such as parameters for connecting to a database). These clumps should be turned into their own classes.

Use the mentioned refactoring techniques to avoid these kinds of code smells.

  • Extract Class
  • Introduce Parameter Object
  • Preserve Whole Object

For more details about bloaters, check this link.

Object-Oriented Abusers

Incorrect or incomplete application of object-oriented programming principles can result in issues that damage system functionality and performance.

All such issues are called Object-Oriented Abusers.

Here is a list of such common code smells.

  • Alternative Classes with Different Interfaces
  • Switch Statements
  • Temporary Field
  • Refused Bequest

For more details about object-oriented abusers, check this link.

Change Preventers

Change Preventer is a piece of code that prevents a programmer to make any changes, as a simple change in one place needs many changes in other places.

These make development more complicated and expensive, as they prevent programmers to touch certain parts of the code.

Here is a list of such common code smells.

  • Divergent Change
  • Parallel Inheritance Hierarchies
  • Shotgun Surgery

For more details about change preventers, check this link.

Dispensables

Dispensable is a piece of code, method, or class that is not needed or whose absence makes the code cleaner, easy to understand, and more efficient.

Here is a list of such common code smells.

  • Comments
  • Dead Code
  • Duplicate Code
  • Data Class
  • Lazy Class
  • Speculative Generality

For more details about dispensables, check this link.

Couplers

The excessive coupling between classes can cause code smells, which can be resolved through excessive delegation.

Here is a list of such common code smells that cause excessive coupling.

  • Feature Envy
  • Incomplete Library Class
  • Middle Man
  • Inappropriate Intimacy
  • Message Chains

For more details about couplers, check this link.

Overall

We now know different types of code smells that can be resolved by refactoring.

Related Links