Lazy Class
Description (What)
A lazy class is a class that doesn't do much or doesn't contribute enough to warrant your attention.
How to Locate It (Where)
Find classes that appear small, have limited interaction with the rest of the program and don't contribute to the overall program.
How It Manifests (Why)
Classes that were originally designed to support some future work or have reduced in size by a lot after refactoring.
How to Fix It (Possible Treatments)
In Example 1, Collapse Hierarchy is used to remove the Lazy Class. Collapse Hierarchy works by removing a superclass that is not doing much more than simply than existing for the sake of hierarchy in the case of Lazy Class.
In Example 2, Collapse Hierarchy is also used to remove the unnecessary class. When there are some additional layers in the architecture,
but the extra layers are rarely used, developers could consider get rid of the complexity.
Other treatments are also possible based on the specific scenario, they can be found here
Examples
Example 1
Before:
Cheetah was added considering that it might have unique functionality in the future.
Observed Code Smells:
- Lazy Class (lines 29-33)
After:
Applied Collapse Hierarchy to remove the class given that it is the same as the super class Cat
Refactoring Applied:
- Lazy Class
- Collapse Hierarchy (Cheetah)
Observed Code Smells After Refactoring:
- None
Example 2
Before:
The subclass Security of the class Employee does not seem to do enough to earn attention at this time.
It only comes with an additional field which is badgeNumber.
Observed Code Smells:
- Lazy Class (line 76-91)
After:
Applied Collapse Hierarchy to merge the subclass into the superclass.
Refactoring Applied:
- Lazy Class:
- Collapse Hierarchy (line 88-90, 27-37)
Observed Code Smells After Refactoring:
- None
When to Ignore
In cases where classes are created in specific cases to project future work or extensions
More
- Example 1
- Example 2
- Before
- After
001 class Cat {002 private String name;003 private String cry;004005 public Cat(String name, String cry) {006 this.name = name;007 this.cry = cry;008 }009010 public void purr() {011 System.out.println(this.cry);012 }013014 public void getCatName() {015 System.out.println(this.name);016 }017 }018019 class Tiger extends Cat {020 public Tiger(String name, String cry) {021 super(name, cry);022 }023024 public void isDangerous() {025 System.out.println("Yes");026 }027 }028029 class Cheetah extends Cat {030 public Cheetah(String name, String cry) {031 super(name, cry);032 }033 }034035 public class LZCBE1 {036 public static void main(String[] args) {037 Cat cat = new Cat("Siamese", "Meow");038 Tiger tiger = new Tiger("Bengal", "Roar");039 Cheetah cheetah = new Cheetah("African", "Meow");040 cheetah.purr();041 cat.purr();042 tiger.purr();043 }044 }045