CAD Code Refactoring and Quality 3 — Questions and Answers
Question 1: A developer notices that several classes all pass the same three parameters together in multiple method calls. Which refactoring should be applied?
- Extract Class
- Introduce Parameter Object (Correct answer)
- Replace Method with Method Object
- Consolidate Conditional Expression
Correct answer: Introduce Parameter Object
Introduce Parameter Object groups related parameters that always travel together into a single object, reducing parameter list length.
Question 2: What does the SOLID principle 'D' (Dependency Inversion) state?
- Delete unused dependencies
- High-level modules should not depend on low-level modules; both should depend on abstractions (Correct answer)
- Dependencies should be minimized to one per class
- Derived classes should never depend on base classes
Correct answer: High-level modules should not depend on low-level modules; both should depend on abstractions
Dependency Inversion Principle states high-level modules and low-level modules should both depend on abstractions, not on each other directly.
Question 3: Which refactoring technique is best when a method is used more often in another class than in its own class?
- Extract Method
- Inline Method
- Move Method (Correct answer)
- Hide Delegate
Correct answer: Move Method
Move Method relocates a method to the class that uses it most, reducing Feature Envy and improving cohesion.
Question 4: In agile development, what is a 'code smell' in a production codebase?
- A security vulnerability in the code
- A surface indication that usually corresponds to a deeper problem in the code (Correct answer)
- Deprecated API usage detected by a linter
- An unhandled exception in production logs
Correct answer: A surface indication that usually corresponds to a deeper problem in the code
A code smell is a symptom in code that suggests a possible deeper design problem worth investigating and refactoring.
Question 5: What is the goal of 'Replace Magic Number with Symbolic Constant' refactoring?
- Improve runtime performance of numeric calculations
- Replace unexplained literal values with named constants that express intent (Correct answer)
- Convert integers to enumerations
- Remove numbers from conditional logic entirely
Correct answer: Replace unexplained literal values with named constants that express intent
This refactoring replaces hard-coded literal numbers with named constants, making code self-documenting and easier to maintain.
Question 6: Which practice is most aligned with maintaining code quality in an agile team?
- Performing a full code review sprint every quarter
- Integrating refactoring into every sprint alongside feature development (Correct answer)
- Waiting until technical debt accumulates before addressing it
- Assigning a dedicated refactoring team separate from the feature team
Correct answer: Integrating refactoring into every sprint alongside feature development
Agile teams treat refactoring as a continuous activity woven into every sprint, preventing technical debt from accumulating.
Question 7: What does the 'Law of Demeter' (Principle of Least Knowledge) restrict?
- The number of methods a class can expose
- An object's method from calling methods on objects returned by other method calls (Correct answer)
- Developers from accessing third-party library internals
- The depth of class inheritance trees
Correct answer: An object's method from calling methods on objects returned by other method calls
The Law of Demeter states a method should only call methods on its direct dependencies, avoiding chained calls like a.getB().getC().doSomething().
A developer notices that several classes all pass the same three parameters together in multiple method calls.
Which refactoring should be applied?