Search |
||
Yet Another Refactoring ExamplePosted by thedarksavant on January 19, 2009 at 12:24 PM PST
Perhaps there are already enough refactoring examples, but if not then I present yet another. I was writing some code investigating a potential project here at Venture, and since it was just spike, code, I just hammered it out. Because it is spike code there are a couple of notable differences from good development practices that I should point out. First, I wrote this code alone. No pair programming and no code review. Second, I wrote no tests. Not first, not during, not after. Not doing TDD is why I believe I created such a horrible mess in the first place which then allowed me to refactor it here, for your enjoyment. Here's the original
public void run() {
Map
If you're anything like me, you'll need a few minutes to compose yourself after reading this. Code like this should make you at least a little queezy. Methods and AbstractionI learned from Clean Code by Robert Martin that all methods should have the same level of abstraction. Public highlevel methods should do high level things. They should be composed of medium abstract methods that are themselves composed of highly detailed methods that do lowlevel things. For example this code
ObjectOutput objectOutput = new ObjectOutputStream(new FileOutputStream(new File(directoryName, ".vobas")));
for (FileDetail fileDetail : fileDetails.values()) {
objectOutput.writeObject(fileDetail);
}
objectOutput.close();
writes FileDetails to a file. It can be extracted into a self describing method writeFileDetails(fileDetails, directoryName); ... Now, run() has code that looks like: ... writeFileDetails(fileDetails, directoryName); directoryName = reader.readLine(); ...
The section just above that starts with
for (File file : eachFileIn(directoryName)) {
if (fileModified(file, fileDetails.get(file.getName()))) {
ScpTo.send(directoryName + File.separatorChar + file.getName());
fileDetails.put(file.getName(), new FileDetail(new Date(), file.getName()));
}
}
and then replaced with a self describing method sendModifiedFiles(fileDetails, directoryName); The fileDetails Map is starting to look pretty interesting to me. It seems this naked collection should be made a first class object. I'll repress this urge for now, but I wanted to mention it during the same part of the process that it occurred to me. My run method is starting to fit on one page. Always a good sign. Arguments over method length can be as heated and never ending as brace wars, but while we can't seem to agree on one or two line methods, most can agree they should fit on one page in your editor. The if/else clause is about reading from the file detail file and loading up the fileDetails Map. Let's extract that method so
if (!fileData.exists()) {
fileData.createNewFile();
} else {
ObjectInputStream fileDetailsReader = new ObjectInputStream(new FileInputStream(fileData));
FileDetail fileDetail = (FileDetail) fileDetailsReader.readObject();
while (fileDetail != null) {
fileDetails.put(fileDetail.getName(), fileDetail);
try {
fileDetail = (FileDetail) fileDetailsReader.readObject();
} catch (EOFException e) {
break;
}
}
}
becomes readFileDetails(fileDetails, fileData); Now we can extract the try clause into a very abstract method.
try {
BufferedReader reader = new BufferedReader(new FileReader(MainFrame.WATCHED_DATA));
String directoryName = reader.readLine();
File fileData = new File(directoryName, ".vobas");
while (directoryName != null) {
readFileDetails(fileDetails, fileData);
sendModifiedFiles(fileDetails, directoryName);
writeFileDetails(fileDetails, directoryName);
directoryName = reader.readLine();
}
reader.close();
}
becomes
try {
sendModifiedFiles(fileDetails);
}
The two page try clause has been reduced to a single method call that explains succinctly what the method is trying to do. By breaking the long try clause into small chunks and extracting self describing methods, I've converted a fairly incomprehensible chunk of code into a self documenting class. I'll save wrapping the file details Map in a class and moving most of the methods to it since it seems to be the one doing all the work for another entry. Until then, here is the resulting class:
public class VobasBackupService implements Runnable {
public void run() {
Map»
Related Topics >>
Programming Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|