CAD Code Refactoring and Quality 2 — Questions and Answers
Question 1: Which refactoring technique removes duplication by pulling common behavior into a shared base class?
- Extract Method
- Pull Up Method (Correct answer)
- Inline Method
- Move Method
Correct answer: Pull Up Method
Pull Up Method moves identical or similar methods from subclasses into their parent class to eliminate duplication.
Question 2: What is the primary purpose of the 'Replace Temp with Query' refactoring?
- Improve loop performance
- Eliminate a temporary variable by replacing it with a method call (Correct answer)
- Convert a query into a command
- Replace database queries with in-memory caches
Correct answer: Eliminate a temporary variable by replacing it with a method call
Replace Temp with Query extracts the expression assigned to a temp variable into its own method, making the code more readable and reusable.
Question 3: In the context of code quality metrics, what does 'cognitive complexity' measure?
- The number of classes in a system
- How difficult code is to understand due to control flow nesting and branching (Correct answer)
- Memory usage of the application
- The number of external dependencies
Correct answer: How difficult code is to understand due to control flow nesting and branching
Cognitive complexity measures how hard code is for a human to understand, penalizing nested control structures and non-linear flow.
Question 4: Which design smell describes a class that knows too much about another class's internal structure?
- Feature Envy
- Inappropriate Intimacy (Correct answer)
- Data Clumps
- Primitive Obsession
Correct answer: Inappropriate Intimacy
Inappropriate Intimacy occurs when a class accesses too many internal details of another class, violating encapsulation.
Question 5: When applying the 'Decompose Conditional' refactoring, what happens to complex if-else logic?
- It is replaced with a switch statement
- The condition and branches are extracted into separate named methods (Correct answer)
- Boolean variables replace the condition
- The conditional is moved to a configuration file
Correct answer: The condition and branches are extracted into separate named methods
Decompose Conditional extracts the condition expression and each branch into well-named methods to clarify intent.
Question 6: What is the 'Strangler Fig' pattern used for in the context of refactoring?
- Removing dead code incrementally
- Gradually replacing a legacy system by routing new functionality through a new system (Correct answer)
- Strangling infinite loops in legacy code
- Wrapping external APIs with adapters
Correct answer: Gradually replacing a legacy system by routing new functionality through a new system
The Strangler Fig pattern incrementally migrates a legacy system by routing traffic to new code while keeping the old system alive until it can be retired.
Question 7: Which metric measures the number of linearly independent paths through a method's source code?
- Lines of Code (LOC)
- Cyclomatic Complexity (Correct answer)
- Coupling Between Objects (CBO)
- Depth of Inheritance Tree (DIT)
Correct answer: Cyclomatic Complexity
Cyclomatic complexity counts independent execution paths through a method, and is a key indicator of testability and maintainability.
Which refactoring technique removes duplication by pulling common behavior into a shared base class?