The refactoring catalogue
The usual refactorings from Fowler's catalogue and the smell each one cures: extract, rename, move, introduce a parameter object, replace a conditional with polymorphism.
- 33 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Refactoring: what is it?
Show the answer
A transformation of the structure of code that improves its readability or design without changing its observable behaviour.
Adding a feature or fixing a bug is not one: the behaviour changes. A refactoring is done in small steps, with tests green before and after.
This extraction changed the behaviour:
total(10, 10)returned 90.0 before and now returns 100.0. Why?
```java
static void remise(double t, int qte) {
if (qte >= 10) t = t - 10;
}
static double total(double prix, int qte) {
double t = prix * qte;
remise(t, qte);
return t;
}`- The method receives a copy of t: changing it does not reach the caller's variable
- The compiler drops the call remise(t, qte), since the method returns nothing
- The extracted method evaluates qte >= 10 before t is computed in total
- The t in remise is a new field of the class, which total does not read
Show the answer
The method receives a copy of t: changing it does not reach the caller's variable
In Java, a
doubleis passed by value. The block modifiedt: the extracted method must therefore return the new total (t = remise(t, qte);). The two real outputs: 90.0 then 100.0.A refactoring changes the … of code without changing its … behaviour.
Show the answer
A refactoring changes the structure of code without changing its observable behaviour.
That is what makes it checkable: the same tests must pass before and after.
Extract method: what is it?
Show the answer
A refactoring that moves a block of statements into a new function named after its intent, and calls it in place of the block.
It cures the long method and duplicated code. The variables the block reads become parameters; the one it modifies becomes the returned value.
An 80-line method has four blocks preceded by the comments "compute the discount", "compute shipping", "total" and "formatting". What should you do?
- Keep the block as is and add one more comment for each line
- Replace the four comments with a single one at the top of the method
- Move the whole method into another class without splitting it
- Extract each block into a method whose name restates its comment
Show the answer
Extract each block into a method whose name restates its comment
The comment already announces the intent: making it a method name makes the comment unnecessary and each step testable. The original method just chains four calls.