Lazy Class

A class that doesn't do enough meaningful work to justify its existence.

Sign of Smell

Lazy Class also known as Freeloader. When you encounter a class that isn’t doing enough to earn your attention, it’s a sign of this smell.

Perhaps a class was initially designed to be fully functional, with all the necessary features and capabilities. However, after undergoing multiple rounds of refactoring and optimization, it has inadvertently become ridiculously small in size.

Alternatively, the class may have been initially designed with the intention of supporting future development work that was never implemented or completed. This could be due to various reasons such as shifting priorities, resource constraints, or changes in project requirements. Regardless, the class remains in its current state, waiting for the opportunity to fulfill its original purpose and contribute to future development efforts.

Lazy Class is the opposite code smell to Large Class.

Reason of Smell

Here are some reasons why Lazy Classes are considered a code smell:

  • Reduced Readability: Unnecessary classes make the code harder to read and understand. They can clutter the codebase, making it difficult to focus on the important parts of the system.

  • Complexity: Lazy classes add unnecessary complexity to the code, making it harder to understand and maintain.

  • Wasted Resources: Developing and maintaining code for classes that provide little or no value consumes development time and resources that could be better spent elsewhere.

  • Reduced Maintainability: Code that contains unnecessary classes is more challenging to maintain because developers have to sift through extra code that doesn't serve a purpose.

Refactoring Recipes

  • Collapse Hierarchy

  • Inline Class

  • Inline Singleton

Collapse Hierarchy

If you find the Lazy Class smell in a subclass, we can consider merging it into the superclass.

Inline Class

The class currently has limited functionality and no specific responsibilities. It does not have any assigned tasks, and there are no plans to give it any additional responsibilities. Therefore, it is suggested to transfer all the features from this class to another class.

Inline Singleton

A Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to that instance. If you encounter a class with a code smell of Lazy Class and implement it as a singleton, you may consider transforming it into an inline singleton.

First, carefully review the methods and attributes of the Singleton instance in the Lazy Class to understand its functionality and how it is used by the consuming class. Next, copy the methods and attributes from the Singleton instance in the Lazy Class and paste them directly into the consuming class.

Once you have modified the consuming class to use the copied methods and attributes, you can safely delete the original Lazy Class.

Reference

https://refactoring.guru/smells/lazy-class

https://www.informit.com/articles/article.aspx?p=1400866&seqNum=12

https://refactoring.guru/design-patterns/singleton

Last updated