Free CAD Code Refactoring and Quality Questions and Answers 1 — Questions and Answers
Question 1: An agile developer is reviewing a colleague's code and notices a method with a very long parameter list. This is a classic example of what?
- A feature envy
- A code smell (Correct answer)
- Technical debt
- A failed unit test
Correct answer: A code smell
A 'code smell' is a surface-level indicator of a potentially deeper problem in the code. A long parameter list is a well-known code smell, suggesting the method may have too many responsibilities or that the parameters could be grouped into an object. While it contributes to technical debt, the immediate characteristic is best described as a code smell.
Question 2: According to Martin Fowler, what is the primary definition of code refactoring?
- Fixing bugs and improving the user interface.
- Rewriting a large module from scratch to improve performance.
- Adding new functionality while simultaneously cleaning up old code.
- Changing the internal structure of software without changing its external behavior. (Correct answer)
Correct answer: Changing the internal structure of software without changing its external behavior.
Martin Fowler, who popularized the term, defines refactoring as the process of changing a software system's internal structure to make it easier to understand and cheaper to modify without changing its observable behavior. Fixing bugs or adding new functionality are distinct activities from refactoring.
Question 3: A team is about to add a new feature to their application. They notice the existing code in that area is complex and hard to understand. To make adding the new feature easier, they first restructure the existing code. This is an example of which refactoring strategy?
- Red-Green-Refactor
- Refactoring by Abstraction
- Preparatory Refactoring (Correct answer)
- Composing Methods
Correct answer: Preparatory Refactoring
Preparatory Refactoring is the practice of improving the design of existing code specifically to make it easier to add the next feature. The team is refactoring with the immediate goal of simplifying a future development task, which is the essence of this strategy.
Question 4: Which of the following is a primary goal of code refactoring in an agile environment?
- To achieve 100% code coverage with unit tests.
- To reduce the number of user stories in the backlog.
- To reduce technical debt and improve maintainability. (Correct answer)
- To eliminate the need for a dedicated QA team.
Correct answer: To reduce technical debt and improve maintainability.
Refactoring is a key practice for managing and reducing technical debt. By continuously improving the internal code structure, teams make the software easier to maintain, adapt, and extend in future sprints, which is crucial for agility.
Question 5: During a sprint, a developer needs to make a change. According to Kent Beck's 'two hats' metaphor, what should the developer do first if the required change is difficult to implement in the current code structure?
- Implement the change using the quickest possible hack and create a new story to fix it later.
- Switch to the 'refactoring hat' to make the change easy, then switch to the 'adding function hat' to make the easy change. (Correct answer)
- Ask the Product Owner to de-prioritize the feature until the architecture can be properly redesigned.
- Perform the refactoring and the functional change in a single, large commit to ensure they are linked.
Correct answer: Switch to the 'refactoring hat' to make the change easy, then switch to the 'adding function hat' to make the easy change.
Kent Beck's advice, highlighted by Martin Fowler, is to separate the acts of refactoring and adding functionality. When faced with a difficult change, the developer should first put on their 'refactoring hat' to restructure the code, making the change easy. After that work is committed, they switch to their 'adding function hat' to implement the feature itself. This approach keeps commits small, focused, and easier to review.
Question 6: A team is using the Red-Green-Refactor technique. They have just written the minimum amount of code required to make a failing test pass. What is the immediate next step?
- Write the next failing test for another piece of functionality.
- Merge the code into the main branch to get feedback.
- Look for opportunities to improve the code's design and eliminate duplication. (Correct answer)
- Deploy the working code to a staging environment for QA testing.
Correct answer: Look for opportunities to improve the code's design and eliminate duplication.
The Red-Green-Refactor cycle, which is fundamental to Test-Driven Development (TDD), has three distinct steps. After writing code to make the test pass (the 'Green' step), the next immediate step is 'Refactor'. This is the phase where the developer improves the internal structure of the code they just wrote, such as removing duplication or improving clarity, while ensuring all tests remain green.
An agile developer is reviewing a colleague's code and notices a method with a very long parameter list.
This is a classic example of what?