The Composed Method implementation pattern

2009-06-28

Infoq is one of my favorite sources for reading general-purpose stuff on software development. Some time ago they published an excellent presentation: 10 Ways to Improve Your Code by Neal Ford. I just loved it. The first practice it focused on is the composed method implementation pattern, one of the most fundamental development practices I can think of.

It was formally described a lot of years ago, as a pattern, in Kent Beck’s book Smalltalk Best Practice Patterns. I havent’t read this book yet (it is arriving in my next Amazon’s order). Sincerely I don’t know anything about Smalltalk but people usually say that this book is recommendable for anyone interested in good programming practices. I’m sure I will enjoy it. I expect it to be better than Implementation Patterns, also by Kent Beck. While not a bad book, I thought it was going to be much better. In this book, it is said that you should:

Compose methods out of calls to other methods, each of which is at roughly the same level of abstraction

I don’t know why but I tended to call this implementation pattern as factorized code. I think it has to do with the fact that the first time I realized about its importance was reading Martin Fowler’s Refactoring book. It was a lot of years ago but I remember that in some section (I guess it was in the extract method refactoring pattern) the book said that when you have in your code something like this:

void someMethod(){
  //do some stuff
  line 1
  line 2
  line 3

  //calculate some other stuff
  line 4
  line 5
  line 6

  //do final stuff
  line 7
  line 8
  line 9
}

You should refactor to something like this:

void someMethod(){
  doSomeStuff()
  calculateSomeOtherStuff()
  doFinalStuff()
}

void doSomeStuff(){
  line 1
  line 2
  line 3
}

void calculateSomeOtherStuff(){
  line 4
  line 5
  line 6 
}

void doFinalStuff(){
  line 7
  line 8
  line 9 
}

It said that, instead of having a sequence of comments and lines of code, you should extract lines of code to methods with proper names. This way you could also remove comments, as they were redundant. If I remember something of the Refactoring book, is how impressed I was about Fowler’s coding style (the catalog is full of Java code samples).

In my first year at the University, we were taught about procedural programming. I remember being told that when you designed a program, you had to divide high-level routines into calls to lower-level ones. By doing this recursively, you obtained a tree, where leafs represented primitive routines.

Watching the way many people code it seems that this fundamental principle has simply being lost. It seems like if with objects, you only have to take care of carefully design the public contract of objects and their relations. While this aspect is very important, you have to pay a lot of attention to the design of internal code.

Although this pattern may seem very simple, applying it correctly is not as simple as saying you should divide methods into small steps. I think there are three fundamental properties that should rule methods design:

  • Cohesion
  • Clear interfaces
  • Symmetry

Methods should be highly cohesive. This means that methods should focus in a single responsibility or functionality. This property comes from the software quality field, usually applied to modules or classes. Applying it when designing methods implies that you shouldn’t have methods that do many things from the point of view of the client that is using it. Who is the client depends on the abstraction level where the code is being invoked. Could be another class o just a private method inside in the same one. A method that receives five parameters is usually a bad smell.

Methods should have clear interfaces. In Writing Solid Code Steve Maguire says that the signature of all methods (functions in those days) should be clear enough in order to understand what the method is supposed to do, without any other kind of documentation. Signature means the name of the method as well as of the parameters it receives and returns (and their types). Achieving this involves many things but without cohesion is just impossible.

The book shows a good example of how a method should not being designed: the C realloc() function. It just does many things with a very obscure interface, which you won’t understand without the documentation. The book also contains a very representative example from the physical word, the candy machine interface problem: when the candy machine offers numbers for both selecting products and displaying their prices. Methods should not offer a candy machine interface.

The composition of methods should also be symmetric. The steps a method is divided in should be at the same level of abstraction. Abstraction is a construction that a person does in order to understand something. Jumping from one level of abstraction to another requires mental effort. If you have to keep jumping between different levels of abstraction in order to understand some code, it will be far more difficult to understand.

In conclusion, while this pattern will improve the quality of your software, I use it because for me it is easier to program this way. It is just the principle of divide and conquer working. I usually code methods following a top-down approach. Writing the top level methods calling lower-level methods before they exist. This way I can directly project the structure of steps that I, like a human, have in my head. Other people prefer to write the code without decomposing it first, and then refactorize to extract methods (I think this approach is far less effective).

Anyway I think that if you apply this practice exhaustively, along with using proper names for naming identifiers, your code will be like a 400% better.

No Comments

Slides of Agile Development and Rails Course

2009-04-12

Here it is the last version of the presentation I used in the Agile Development and Rails courses I taught in the University of Oviedo (HCI-RG group). They correspond to a 6 hours-class (the whole course was one week length). It is a very short duration to introduce both the agile development approach and the rails platform so I had to leave out many things. I was very interested in the part of basic good development practices because I think these are easy to learn and very worthy for everyone.

NameDescriptionLanguageDate
2008-04-agile-rails.pdfSlides on Agile development and Introduction to the Rails Platform. They are part of the Agile Development and Rails courses I participated in at the University of Oviedo.
Spanish
2008-04
No Comments

Research Report on Model Driven Software Development (2006)

2009-04-12

In 2006 I got a FICYT Research Grant in Software Engineering. As the results of my research I wrote a document I would like to share. Since it was written three years ago it is quite outdated but perhaps somebody can find it useful.

The report shows very well my evolution during the research period. First, I was very interested in evaluating the possibilities of UML for specifying software formally. As result there is a very long chapter on the foundations of the language. Then I learned the benefits of the multi-DSL proposal (and the inconveniences of the UML-centric one), and the basics on model to model transformations and formal languages definition through their metamodels, and I focused my research on these topics.

NameDescriptionLanguageDate
2006-MDSD-FICYT.pdfReport on Model Driven Software Development. Written during the research period corresponding to a FICYT Research Grant in Software Engineering.
spanish
2006

No Comments

Slides of Web Accesibility Course

2009-04-10

There are some stuff I would like to share online. It may be useful for someone and is totally useless in my hard drive. Perhaps in the future I will create a downloads page or something similar. For now, I will collect these things in a Downloads Category in the blog.

One of the things I would like to share are the slides of the courses I have taught in the past years. This one is the last version of the Accesibility part I have been teaching in several of the HCI-RG courses on Human Computer Interaction.

The slides are mainly focused in the 1.0 version of the WCAG. It also contains an introduction to the 2.0 version and AJAX related issues. They were created using the HTML Slidy System so you can view them in the browser.

NameDescriptionLanguageDate
2008-07-CursoAccesibilidadWeb.zipSlides of a course on Web Accessibility. HCI-RG courses on Human Computer Interaction (University of Oviedo). In HTML Slidy format. View them online.
Spanish
2008-07

No Comments

The role of models in Agile and Model Driven Development approaches

2009-04-5

When I was at the university, I remember being very interested in the suggestion that building software was like building bridges, and that software should have blueprints as sophisticated as those needed to build a bridge. I remember to has been told that you will never find a book on “How to build bridges in 7 days” but you will find the equivalent on “how to learn C++ in 7 days”.

Today, I’m not so convinced that this analogy is a good one. I can’t agree more with this quote from Dave Thomas (the Pragmatic Programmers proposed a new analogy that I’m pretty sure not everyone can digest):

Software development is neither. Nor is it art. It’s just software development. People who look for the “software is like xxx” analogies are missing the point. Software development is like software development. Let’s decide what works for us, and have fun while doing it

If you are interested, Martin Fowler explains the reasons why the building bridges analogy fails. I think, there is a sentence in his article that summarizes the problem very well:

Can you get a design that is capable of turning the coding into a predictable construction activity? And if so, is cost of doing this sufficiently small to make this approach worthwhile?

This question is, obviously, rhetorical. You won’t. Traditionally, the gap between models and code has been huge. If you ignore this gap, then you may think that you have to make great efforts in creating very rich models, so the low-skilled and no-talented programmer can translate them into working code. And if you can create these models, then nothing stops you from planning the full development process up-front. Since the construction phase is mechanical and predictable, then you only have to calculate how much time you will have to spend in modeling. And since modeling is about drawing (if we use this development approach we are using UML for modeling everything for sure), this estimation should be easy too. After all, drawings, like Word documents or Power Point slides, always compile and work. Of course, this scenery would mean that software projects are delivered on time, that the waterfall approach works, and that no matter wether the developers are talented or not.

In my opinion, the fact that models can’t be converted into code easily is addressed by the two development approaches I am more interested in:

  • Agile development

  • Model Driven Engineering (MDE)

Although both approaches are perfectly compatible, they propose different ways when it comes to use models. Both approaches see models as a communication tool, as a resource that let us, as humans, reason and elaborate solutions that solve complex problems. While agile practitioners warns you against using models for anything more than this, MDE practitioners propose to take the next step and convert models into first-class development artifacts.

The two approaches give different answers to the question of being able to create designs that can turn the coding into a predictable activity:

  • Agile developers’ answer is “no”. In fact, they propose to use many good development practices and exhaustive testing as a shield against problems when coding. Coding is not an ugly phase, neither is cleanly separated from design. There are not designers and programmers, there are developers. They will use models as a way to collaboratively obtain the best designs that solve the problem, not as a way of documenting it. And since they assume they can’t predict how the full process is going to be in earlier stages with detail, they are going to estimate and develop the project iteratively, using an empirical estimating approach.

  • The MDE answer is a little bit more complex to say, since the paradigm is in its earlier stages. But if we manage to have MDE solutions where we can define the best DSLs to describe a problem, and to specify how this input is translated into a working application, then, if these DSLs are defined in a way that are reusable in different instances of the same problem, we would be very close to being able to answer “yes” to that question.

By the way, a confluence of both approaches can be seen in the BDD (Behavioral Driven Development) frameworks: such as RSpec, Concordion or Cucumber, but I think that topic deserves a post on its own.

1 Comment

A nice iPhone tool for agile development

2009-02-16

From time to time I like to check for new applications for my iPhone. It’s pretty amazing how quickly the base of applications is growing, so I usually discover nice surprises.

So today I decided to look for application that was SCRUM-specific. Sincerely I didn’t expect to find anything, nor I had any concrete application in mind. The app store search dialog suggested ScrumTools. It’s a very very simple, but still useful, iPhone app. In its current version it offers two things:

Although books usually recommend it, I had never used a timer to control the daily meeting length. If you don’t take care, these meetings can be easily longer than the 15 minutes recommendation. Since members of the team expose their problems, discussions about how to solve things tend to appear quite often. The solution is as simple as delaying further discussions for specific meetings. The iPhone ScrumTools alerts at 10, 5 and 1 minutes remaining (the normal iPhone timer won’t do this). I thing is a very good way of focusing the meeting in answering the three questions

Screenshot of the timer of the ScrumTools for Iphone

Regarding to the Planning Poker activity, if you don’t know what it is about, you should read Mike Cohn’s book on Agile Estimating and Planning. It’s the best book on agile development I have read. Planning Poker is a technique for estimating size in software development. Basically, for each story:

  1. The story is presented and the team discuss about it.
  2. The members of the team choose a card that represents their estimation. We use story points in the set proposed by Cohn (1,2,3,5,8,13).
  3. Everyone turn their cards over at the same time.
  4. The members with the lowest and highest estimation expose their reasons, and then the estimation process is repeated.

The book tells that teams will find consensus soon. I was a bit skeptical about this after reading the book, but practice has taught me that a consensus is usually reached quickly.

Screenshot of the planning poker deck of the ScrumTools for iPhone

No Comments

Changed to WordPress

2009-02-8

I’ve changed my blogging system from Typo to WordPress. I found several problems with my Typo installation, specially regarding to performance. I was very interested in updating to the latest version of Typo, which addresses many problems (including its performance). Unfortunatelly, I didn’t manage to make it work in Site5, the hosting service that runs this blog. The last version of Typo runs on Rails 2.2. Site5 hasn’t updated to 2.2 yet. I updated my local gems of rails to version 2.2.2, but I didn’t manage to make it work (and it seems that I am not the only one having this problem).

I really love Typo and I don’t discard to use it again in the future. For now, I’m going to enjoy the benefits of using WordPress, the most widely used blogging System. I had some experience configuring a WordPress site, so I knew what to expect:

  • Extremely easy configuration
  • A huge amount of themes and plugins available

My first theme choice was Skittish (the theme I was using with Typo). The problem was that it wasn’t updated to the last version of WordPress and it needed minor fixes to work. I decided to browse the WordPress Theme Directory and this site which offers a selections of themes. Finally I found a very nice one: The Vostok theme. I was looking for something exactly like this:

  • Extremely clean and simple.
  • Maximum legibility with dark tones.

Their creators are experts in interaction design and usability. I think that a simple product like this blog theme is a proof of their philosophy and expertise. I just can thank them for sharing this wonderful theme under a GNU v2 license.

1 Comment

Errors in my post on Comparing EMF Models

2008-11-19

There was an error in the code originally posted in my article on Comparing EMF models. Thank you so much to Jim Showalter for warning me. The post has been updated with the right code. Now it’s also included an Eclipse sample project that compares two UML models.

No Comments

Generating JUnit Test Cases Dynamically

2008-09-18

In this post I’m going to talk about generating JUnit Test Cases Dynamically. In order to define tests, most JUnit’s users place their testing code inside test methods that

  • Start with test
  • Don’t have arguments

Given a TestCase object, JUnit TestSuites can dynamically extract test methods that follow these conventions and run them on the fly. However, this behavior isn’t enough in some cases. The problem of this approach is that, from the developer’s point of view, tests must be written statically.

Imagine you have a directory with many text files. For each file, you want to do the same kind of testing. Since the test result depends on each file, what you really want is to generate dynamically test cases. Of course you can have a single test method, and do all the testing there. The main problem of this approach is that Test Runners won’t be able to collect results properly: a single fail with one file would result as a failure. You won’t notice which tests are valid and which aren’t.

Generating tests cases dynamically is very simple. JUnit’s design itself is simple. This is one of the main reasons of why this framework is so successful. JUnit was created by Kent Beck and Erich Gamma. It represents an excellent oportunity to see GOF design patterns in action (in the same way that JHotDraw, another framework created by Erich Gamma as a design patterns exercise). In JUnit, a Test Case is, surprisingly, a Test Case. A Test Suite is a composite of Test Case. You can create a Test Suite as a normal object and, programmatically, populate it with as many instances of a test case as you want.

Let’s see a very simple example. Imagine you have the following structure of folders:

Folder Structure for the sample

For each folder, you want to create a Test Suite. For each file, you want to test that its extension must be .txt. In this way, you are going to create a parallel structure using test suites and test cases.

Below, the test suite is shown. It receives a directory in the constructor. It goes through all the contained files and subdirectories. For each subdirectory, a new TestSuite is created and added to the composite (recursively). For each file, a new TestCase is created and added in the same way. The actual testing is going to be done in these test case instances.

public class TextFileTestSuite extends TestSuite {
  public TextFileTestSuite(File directory) {
    super(directory.getName());
    for (File file : directory.listFiles()) {
      if (file.isDirectory())
        addTest(new TextFileTestSuite(file));
      else
        addTest(new TextFileTestCase(file));
    }
  }
}

The test case’s implementation is very straighforward. It receives the concrete file to test in the constructor. It also overrides the runTest() method, where the real testing code is placed.

 public class TextFileTestCase extends TestCase {
  private File file;
 
  public TextFileTestCase(File file) {
    super();
    this.file = file;
    setName(file.getName());
  }
 
  protected void runTest() throws Throwable {
    assertTrue(file.getName().endsWith(".txt"));
  }
 }

And that’s all. You can now check the results on a Test Runner:

 public class AllTests {
  private static final String TEST_FOLDER = "testdata";
 
  public static Test suite() {
    TestSuite suite = new TestSuite(
        "Test for net.jorgemanrubia.junitdinamically.sample");
    //$JUnit-BEGIN$
    suite.addTest(new TextFileTestSuite(new File(TEST_FOLDER)));
    //$JUnit-END$
    return suite;
  }
 }

With the exposed structure of folders, you will obtain something like this:

Test Results using Eclipse's JUnit Test Runner

No Comments

Comparing EMF Models

2008-07-6

At work, we needed a mechanism to compare EMF models. We are developing a system that uses ATL model to model transformations. We wanted to validate the transformations with JUnit unit tests. We needed a mechanism that let us compare expected models with transformed output models.

EMF Compare

I started evaluating the EMF Compare Framework. This framework targets model comparison by building a EMF model of the differences found during the comparison process. It let you evaluate differences pretty exhaustively. It evaluates source and target models by trying to match parallel elements. For related elements, matching is applied recursively.

The differences model is build of matched and unmatched elements. Matched elements let you examine the similarity of the matching (a number between 0 and 1). It seems as if two elements match just by having the same metaclass. Then, the similarity is set depending of how similar are their attributes. If the two models are identical, the root elements matching similarity is 1. If you change the value of two attributes, for example, then this precision was over 0.9. If you just change the order of two nodes, the precision was again under 1, even when the references in the metamodel weres unordered. This behavior wasn’t very good for our purposes, since models under comparison could have different orders in their references.

If you think in the semantic of the comparison, two models can be identical when their unordered references don’t have the same order, don’t they? I suspect that EMF Compare probably would let you configure different comparison strategies easily, but we found a simpler way to achieve what we wanted.

Modifying the EcoreUtil.EqualityHelper class to ignore order in references

A friend told me have a look at the equals() method provided by the org.eclipse.emf.ecore.util.EcoreUtil class. It receives two EObject elements and compare them recursively. The two models have to had exactly the same structure in order be equal (no matter wether references were ordered or unordered). Again stabbed with the same problem. A look at the source of the method revealed that it delegates completely the functionality in a helper inner class: EqualityHelper. This class code is well factorized and it can be understood easily. The comparison of two list of elements was properly contained in an equals(List, List) method. So we try to hack this method in order to ignore orders when comparing.

After spending some hours trying to make a very complex modification of the method, another friend proposed the simplest way to compare two lists: sort them both and then compare sorted lists expecting exactly the same order. The solution was as obvious as wonderfully easy to implement: we already had the exhaustive comparison so we only needed to center our efforts in sorting the lists.

The last thing we needed was to find an EObject comparison criteria in order to implement the proper java.util.Comparator. This wasn’t that easy and we finally ended up parsing the toString() method result so we can obtain the attributes list string (the attribute’s name is hard coded in the EMF generated code of each concrete toString() method).

Below the source code of the modified EqualityHelper is shown.

public class EMFComparator extends EcoreUtil.EqualityHelper {
 
  class EObjectComparator implements Comparator<EObject> {
    public int compare(EObject object1, EObject object2) {
      String targetString1 = extractComparisonString(object1);
      String targetString2 = extractComparisonString(object2);

      return targetString1.compareTo(targetString2);
    }

    private String extractComparisonString(EObject object) {
      return object.toString().replaceAll(
          object.getClass().getName(), "").replaceAll(
          Integer.toHexString(object.hashCode()), "");
    }
  }

  @Override
  public boolean equals(List list1, List list2) {
    Comparator comparator = new EObjectComparator();

    List<EObject> sortedList1 = new ArrayList<EObject>(list1);
    List<EObject> sortedList2 = new ArrayList<EObject>(list2);

    Collections.sort(sortedList1, comparator);
    Collections.sort(sortedList2, comparator);
   
    return super.equals(sortedList1, sortedList2);
  }

Conclusion

I was a bit surprised of not finding this problem solved when googling for it. I’m sure more people have had this need and have solved this problem before. When you are transforming models, you need a formal way to compare real output models and expected output models. I wouldn’t develop a complex transformation system with a lot of transformation rules without this system. Lateral effects when modifying transformation rules can break the system and being easily unnoticed. If transformations are an essential mechanism in a data loading system, like our case is, this danger is not acceptable.

Update (2008-11-19)

There was an error in the code originally posted. The method extractComparisonString() received an String object, when it should receive an EObject (it didn’t make sense). Thank you so much to Jim Showalter for warning me. I should have copy/pasted from what I coded at work, instead of rewriting at home.

You can also download an Eclipse sample project that compares two UML models. It includes the source of the comparator.

2 Comments