Pattern Design Anti-Patterns and Refactoring 2 — Questions and Answers
Question 1: The 'Feature Envy' code smell describes a method that:
- Seems more interested in the data of another class than its own (Correct answer)
- Has too many input parameters
- Duplicates logic already present elsewhere
- Is called from too many different places
Correct answer: Seems more interested in the data of another class than its own
Feature Envy occurs when a method accesses the data or methods of another class more than its own, suggesting it belongs in that other class.
Question 2: Replace Conditional with Polymorphism refactoring eliminates:
- Long if-else or switch statements based on object type (Correct answer)
- Duplicate code across subclasses
- Feature envy between classes
- Magic numbers in conditions
Correct answer: Long if-else or switch statements based on object type
Instead of a switch on object type to select behavior, each subclass overrides a method with its own behavior, leveraging polymorphism.
Question 3: The 'Primitive Obsession' code smell is addressed by:
- Replacing primitive data types with small value objects or domain classes (Correct answer)
- Extracting methods from long classes
- Splitting classes with too many responsibilities
- Removing duplicate code into a base class
Correct answer: Replacing primitive data types with small value objects or domain classes
Using primitives (String, int) for domain concepts (PhoneNumber, Money) loses meaning and validation — wrapping them in value objects adds clarity and safety.
Question 4: The 'Dead Code' anti-pattern refers to:
- Code that is never executed because it is unreachable (Correct answer)
- Code that executes very rarely in production
- Code that is scheduled for deletion in a sprint
- Code that has not been tested
Correct answer: Code that is never executed because it is unreachable
Dead code is unreachable or never-called code that clutters the codebase and should be removed to reduce maintenance overhead.
Question 5: The 'Blob' anti-pattern is identified by a class that has:
- Hundreds of attributes and methods performing unrelated functions (Correct answer)
- Only static utility methods with no state
- An excessively deep inheritance chain
- Too many design pattern implementations
Correct answer: Hundreds of attributes and methods performing unrelated functions
A Blob (or God Class) is a class that has absorbed so much responsibility it becomes a monolith within the design, making decomposition urgent.
Question 6: Inline Method refactoring is the opposite of Extract Method and is used when:
- A method's body is as clear as its name and the extra indirection is unnecessary (Correct answer)
- A method is called from too many places
- Two methods have overlapping functionality
- A method violates the Single Responsibility Principle
Correct answer: A method's body is as clear as its name and the extra indirection is unnecessary
Inline Method removes a method that adds no clarity and replaces all calls to it with the method's body directly in the callers.
The 'Feature Envy' code smell describes a method that: