The document discusses strategies for refactoring legacy code through analogies like cleaning a trash pile or squeezing toothpaste from a tube. It recommends adding tests, refactoring to improve design without changing behavior, and making incremental changes to facilitate switching programming languages or improving code over time. Refactoring pays dividends by preventing technical debt and building technical wealth through continuous improvement that makes future changes easier.
32. “For each desired change:
make the change easy
(warning: this may be hard),
then make the easy change.”
Kent Beck
33. class User:
def __init__(self, data):
self.data = data
def validate(self):
if '@' not in self.data['email']:
raise ValueError('Invalid email provided.')
if len(self.data['first_name']) > 50:
raise ValueError('First name too long.')
if len(self.data['last_name']) > 100:
raise ValueError('Last name too long.')
38. class User:
def __init__(self, data):
self.data = data
def validate(self):
self.validate_email()
self.validate_first_name()
self.validate_last_name()
def validate_email(self):
if '@' not in self.data['email']:
raise ValueError('Invalid email provided.')
def validate_first_name(self):
if len(self.data['first_name']) > 50:
raise ValueError('First name too long.')
def validate_last_name(self):
if len(self.data['last_name']) > 100:
raise ValueError('Last name too long.')
43. class TestValidateUser:
. . .
def test_invalid_phone_number_raises_error(self):
data = {'phone_number': 'no_phone'}
user = User(data)
with pytest.raises(ValueError):
user.validate_phone_number()
75. References:
Refactoring - Martin Fowler & Kent Beck
https://www.amazon.com/Refactoring-Improving-Design-Existing-
Code/dp/B007WTFWJ6/ref=sr_1_1?
s=books&ie=UTF8&qid=1472533198&sr=1-1&keywords=refactori
ng+martin+fowler#nav-subnav
Working with Legacy Code - Michael Feathers
https://www.amazon.com/Working-Effectively-Legacy-Michael-
Feathers/dp/0131177052
Forget Technical Debt — Here's How to Build
Technical Wealth
http://firstround.com/review/forget-technical-debt-heres-how-to-
build-technical-wealth/
76. Credits:
IMG_5163 - Justin Henry, https://flic.kr/p/ebaYR
The sky is the limit #2 - Mark Dries, https://flic.kr/p/eeq2eQ
The way it should be! - Patrick Dirden, https://flic.kr/p/
8Yp4mf
0001_zoriah_photojournalist_war_photographer_20120719
_0631 - Zoriah, https://flic.kr/p/dkScaB
Freaktography - Ravaged, https://flic.kr/p/nh72Tp
Aachen - Aachener Dom Pala d’oro Antependium - Daniel
Mennerich, https://flic.kr/p/pBmHyw