Smell Detail

Lazy Element

冗赘的元素

Dispensables

Program elements (like classes and functions) no longer provide enough value to justify their existence and should be inlined or removed.

Define

Introduction

Program elements such as classes and functions can add structure to code, supporting change, promoting reuse, or simply providing better names. But sometimes we really don't need that extra layer of structure. There might be a function whose name looks exactly the same as its implementation code, or a class that is essentially just a simple function.

Key Identifiers

  • Functions that are just one line and don't add meaningful abstraction
  • Classes that contain only one method or just delegate to another class
  • Classes that have become too simple after refactoring
  • Inheritance hierarchies with unnecessary intermediate classes
  • Interfaces with only one implementation that adds no value
  • Wrapper methods that just call another method without adding logic

Refactoring Suggestions

  1. Inline Function: Replace function calls with the function body when the function doesn't add value
  2. Inline Class: Move the class's features into another class when it's too simple
  3. Collapse Hierarchy: Remove unnecessary classes from inheritance hierarchies
  4. Remove Wrapper Methods: Eliminate methods that just delegate without adding value
  5. Merge Classes: Combine classes that are too closely related or too simple

Explanation

Lazy elements often arise because a programmer expected a function or class to grow and become more complex someday, but that day never came. Or perhaps the class was originally useful but became smaller and smaller through refactoring until only one function remained. Whatever the reason, such program elements should be retired gracefully. The key is to maintain only the structure that actually adds value to the codebase.

Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Lazy class - only one meaningful method
class Logger {
public void log(String message) {
System.out.println(message); // Only one line of meaningful code
}
}
// Lazy function - name is the same as implementation
class MathUtils {
public static int add(int a, int b) {
return a + b; // Function name says exactly what the code does
}
public static boolean isPositive(int number) {
return number > 0; // Obvious implementation
}
}
// Simplified class after refactoring - now too simple
class Logger {
public void log(String message) {
System.out.println(message); // Only one line of meaningful code
}
}
// Lazy utility class with obvious methods
class StringHelper {
public static boolean isEmpty(String str) {
return str == null || str.isEmpty(); // Could be inlined
}
public static String toUpperCase(String str) {
return str.toUpperCase(); // No additional logic
}
}
// Service class with only one method
class PasswordHasher {
public String hash(String password) {
return "hashed_" + password; // Simplified implementation
}
// Single responsibility, but maybe too simple to warrant a separate class
}