refactoring_improving_the_design_of_existing_code

Page 227

} Then I replace every return in the original function, one at a time, with calls to the new query. I test after each replacement. When I'm done the original method looks like the following:

String foundMiscreant(String[] people){ for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ sendAlert(); return foundPerson(people); } if (people[i].equals ("John")){ sendAlert(); return foundPerson(people); } } return foundPerson(people); } Now I alter all the calling methods to do two calls: first to the modifier and then to the query:

void checkSecurity(String[] people) { foundMiscreant(people); String found = foundPerson(people); someLaterCode(found); } Once I have done this for all calls, I can alter the modifier to give it a void return type:

void foundMiscreant (String[] people){ for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ sendAlert(); return; } if (people[i].equals ("John")){ sendAlert(); return; } } } Now it seems better to change the name of the original:

void sendAlert (String[] people){ for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ sendAlert(); return;

227


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.