Small!

From WikiContent

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
-
Look at this code
+
Look at this code:
private void executeTestPages() throws Exception {
private void executeTestPages() throws Exception {
Map<String, LinkedList<WikiPage>> suiteMap = makeSuiteMap(page, root, getSuiteFilter());
Map<String, LinkedList<WikiPage>> suiteMap = makeSuiteMap(page, root, getSuiteFilter());
Line 11: Line 11:
}
}
}
}
 +
 +
Your first reaction is probably not positive. Not because the code is that complicated or daunting, but just because you don't necessarily feel like untangling the intent hidden in 11 lines of code with 3 levels of indent. You know you can do it, but it feels like work.
 +
 +
Now look at this function:
 +
private void executeTestPages() throws Exception {
 +
Map<String, LinkedList<WikiPage>> pagesByTestSystem;
 +
pagesByTestSystem = makeMapOfPagesByTestSystem(page, root, getSuiteFilter());
 +
for (String testSystemName : pagesByTestSystem.keySet())
 +
executePagesInTestSystem(testSystemName, pagesByTestSystem);
 +
}
 +
 +
Notice that it doesn't feel so much like work. You can look at it, and grasp the intent without much effort. Not much has changed, I've just cleaned up a few variable names and extracted a few functions. And yet that small change, so easy to do with modern refactoring browsers and a suite of tests, makes the function much easier to read.
 +
 +
The extracted functions are pretty easy to read too:
 +
 +
private void executePagesInTestSystem(String testSystemName,
 +
Map<String, LinkedList<WikiPage>> pagesByTestSystem) throws Exception {
 +
List<WikiPage> pagesInTestSystem = pagesByTestSystem.get(testSystemName);
 +
announceTestSystem(testSystemName);
 +
startTestSystemAndExecutePages(testSystemName, pagesInTestSystem);
 +
}
 +
 +
private void announceTestSystem(String testSystemName) throws Exception {
 +
if (response.isHtmlFormat()) {
 +
suiteFormatter.announceTestSystem(testSystemName);
 +
addToResponse(suiteFormatter.getTestSystemHeader(testSystemName));
 +
}
 +
}
 +
 +
The point is that functions should be ''small''. How small? Just a few lines of code with one or two levels of indent.

Revision as of 00:25, 17 November 2008

Look at this code:

 private void executeTestPages() throws Exception {
   Map<String, LinkedList<WikiPage>> suiteMap = makeSuiteMap(page, root, getSuiteFilter());
   for (String testSystemName : suiteMap.keySet()) {
     if (response.isHtmlFormat()) {
       suiteFormatter.announceTestSystem(testSystemName);
       addToResponse(suiteFormatter.getTestSystemHeader(testSystemName));
     }
     List<WikiPage> pagesInTestSystem = suiteMap.get(testSystemName);
     startTestSystemAndExecutePages(testSystemName, pagesInTestSystem);
   }
 }

Your first reaction is probably not positive. Not because the code is that complicated or daunting, but just because you don't necessarily feel like untangling the intent hidden in 11 lines of code with 3 levels of indent. You know you can do it, but it feels like work.

Now look at this function:

 private void executeTestPages() throws Exception {
   Map<String, LinkedList<WikiPage>> pagesByTestSystem;
   pagesByTestSystem = makeMapOfPagesByTestSystem(page, root, getSuiteFilter());
   for (String testSystemName : pagesByTestSystem.keySet())
     executePagesInTestSystem(testSystemName, pagesByTestSystem);
 }

Notice that it doesn't feel so much like work. You can look at it, and grasp the intent without much effort. Not much has changed, I've just cleaned up a few variable names and extracted a few functions. And yet that small change, so easy to do with modern refactoring browsers and a suite of tests, makes the function much easier to read.

The extracted functions are pretty easy to read too:

 private void executePagesInTestSystem(String testSystemName,
                                       Map<String, LinkedList<WikiPage>> pagesByTestSystem) throws Exception {
   List<WikiPage> pagesInTestSystem = pagesByTestSystem.get(testSystemName);
   announceTestSystem(testSystemName);
   startTestSystemAndExecutePages(testSystemName, pagesInTestSystem);
 }
 private void announceTestSystem(String testSystemName) throws Exception {
   if (response.isHtmlFormat()) {
     suiteFormatter.announceTestSystem(testSystemName);
     addToResponse(suiteFormatter.getTestSystemHeader(testSystemName));
   }
 }

The point is that functions should be small. How small? Just a few lines of code with one or two levels of indent.

Personal tools