8. Rename Rename things to reflect your current
knowledge of the system
Give meaning to them
$message
$confirmationMessage
public function processFile($file)
public function extractInvoiceIdsFromFile($file)
10. Extract variables Simplify complex expressions slicing
them into parts
Give meaning to them
$amount = $price - ($price * $pct / 100);
$discount = $price * $discountPercent / 100;
$amount = $price - $discount;
11. Extract method Isolate dependencies while not ready to
inject them
Give meaning to them
$date = new DateTime();
$date = $this->getCurrentDate();
public function getCurrentDate()
{
return new DateTime();
}
12. Extract method Simplify complex operations slicing them
into parts based on abstraction level
Give meaning to them
public function doSomething()
{
// more than 30 lines of code…
}
public function doSomething()
{
$this->prepareThings();
$this->getData();
$this->processData;
return $result;
}
13. Extract interface Abstract behaviors so you can
replace/exchange implementations
Give meaning to them
class SomeService
{
public function doSomething(): Class
{
}
}
interface Service
{
public function doSomething(): Class;
}
class SomeService implements Service
class BetterService implements Service
27. Takeaways
Legacy is code without tests
Refactor to
• reflect (evolving) knowledge about the
system
• put system in a testable state
Describe behavior with tests
Fight hidden and global dependencies