Description

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 commonly observed code smells.

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

Comments

Problem: A method is filled with explanatory comments.

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

  • Extract Variable
  • Extract Method
  • Rename Method
  • Introduce Assertion

Dead Code

Problem: A variable, parameter, field, method, or class is no longer used (usually because it’s obsolete).

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

  • Inline Class
  • Collapse Hierarchy
  • Remove Parameter

Duplicate Code

Problem: Two code fragments look almost identical.

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

  • Extract Method
  • Pull Up Field
  • Pull Up Constructor Body
  • Form Template Method
  • Substitute Algorithm
  • Extract Superclass
  • Extract Class
  • Consolidate Conditional Expression
  • Consolidate Duplicate Conditional Fragments

Data Class

Problem: A data class refers to a class that contains only fields and crude methods for accessing them (getters and setters). These are simply containers for data used by other classes. These classes don’t contain any additional functionality and can’t independently operate on the data that they own.

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

  • Encapsulate Field
  • Encapsulate Collection
  • Move Method
  • Extract Method
  • Remove Setting Method
  • Hide Method

Lazy Class

Problem: Understanding and maintaining classes always costs time and money. So if a class doesn’t do enough to earn your attention, it should be deleted.

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

  • Inline Class
  • Collapse Hierarchy

Speculative Generality

Problem: There’s an unused class, method, field, or parameter.

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

  • Collapse Hierarchy
  • Inline Class
  • Inline Method
  • Remove Parameter

Overall

We now know how to find and resolve dispensables in the code to improve the code quality.

Related Links