Pattern Design Anti-Patterns and Refactoring 1 — Questions and Answers
Question 1: The 'God Object' anti-pattern refers to a class that:
- Knows too much or does too much, violating the Single Responsibility Principle (Correct answer)
- Creates all other objects in the system
- Prevents subclassing through final declarations
- Acts as the sole observer for all events
Correct answer: Knows too much or does too much, violating the Single Responsibility Principle
A God Object accumulates so many responsibilities and so much knowledge about other classes that it becomes a maintenance nightmare and a central point of failure.
Question 2: The 'Lava Flow' anti-pattern describes:
- Dead code that is too risky to remove because its purpose is unknown (Correct answer)
- An over-engineered class hierarchy
- Excessive use of global state
- Hardcoded values spread throughout the codebase
Correct answer: Dead code that is too risky to remove because its purpose is unknown
Lava Flow refers to code that has solidified (like cooled lava) and is no longer understood — developers fear removing it in case it breaks something.
Question 3: The 'Magic Numbers' anti-pattern is resolved by refactoring them into:
- Named constants or enumerations (Correct answer)
- Configuration files only
- Database-stored settings
- Environment variables always
Correct answer: Named constants or enumerations
Replacing magic numbers (unexplained literals like 86400) with named constants (SECONDS_PER_DAY) makes code self-documenting and easier to maintain.
Question 4: The 'Spaghetti Code' anti-pattern results from:
- Lack of structure, excessive control flow jumps, and unclear responsibilities (Correct answer)
- Over-use of design patterns
- Too many abstraction layers
- Using inheritance instead of composition
Correct answer: Lack of structure, excessive control flow jumps, and unclear responsibilities
Spaghetti code has tangled, convoluted control flow with no clear structure, making it nearly impossible to follow, test, or maintain.
Question 5: Extract Method refactoring is applied when:
- A code fragment can be grouped and named as its own method (Correct answer)
- Two classes share identical fields that should be merged
- A subclass overrides too many parent methods
- A method has too many parameters
Correct answer: A code fragment can be grouped and named as its own method
Extract Method improves readability by pulling a code fragment into a well-named method, making the original method shorter and easier to understand.
Question 6: The 'Shotgun Surgery' code smell means that:
- A single change requires many small edits scattered across multiple classes (Correct answer)
- One class is changed far too frequently
- A feature is implemented in too many places
- A method does too many things at once
Correct answer: A single change requires many small edits scattered across multiple classes
Shotgun Surgery occurs when a single logical change forces you to touch many different files or classes, indicating the responsibility is poorly distributed.
The 'God Object' anti-pattern refers to a class that: