This document discusses strategies for modernizing legacy code through an iterative process. It recommends starting by breaking dependencies with minimal changes while applying tests. Small changes should then be made and tests cleaned up before refactoring code to a cleaner state. The document outlines approaches like partial mocking, dependency injection, and out-of-context testing to break dependencies in an untestable code. It stresses adding tests incrementally and refactoring both production code and tests as the process continues.
14. Next Steps
Provide satisfactory test coverage, and
refactor...
Add tests for constructor
Comment code to help plan tests
Cover other code addressed in this iteration
15. Add Comments...
public function massiveMethod($param = 'hello world') {
// Check if DB knows how awesome we are
$dbConnection = $this->_getDbConnection();
$resultSet = $dbConnection->executeSql(
'SELECT group_of_developers FROM london WHERE everyone_is
LIKE "%we're the best php%" GROUP BY miles');
$dbRes = count($resultSet) > 0;
// Calculate something else
$aString = $param;
$aString .= 'more string';
$aString .= 'more string';
$aString .= 'more string';
$aString .= 'more string';
16. ...Remove Comments
public function massiveMethod($param = 'hello world') {
$dbRes = $this->_checkIfDbKnowsHowAwesomeWeAre();
$aString = $this->_generateSomeString($param);
17. Things to Bear in Mind When
Breaking Up Large Functions
Method name reflects comment
Test section before moving it
Parameter count
18. Looking Inside Large Methods
Sensing variables
if () {
foreach () {
if () {
$this->_privateAttrib = 'something';
$this->sensingVariable = true;
}
}
}
Indecent exposure
Partially mock most of class
19. Cleaning Up Production Code
Minimise public API
Remove unnecessary comments
Roll out dependency injection
Be sure to remove methods for partial
mocking
20. Refactoring Tests
Inject mock objects via DI
Stop testing partial mock
Remove 'out-of-context' setup
Remove single-line constructor tests,
sensing variable tests etc.