Archive for February, 2007

Lesson from Object Bootcamp - The Null-Object Pattern

I thought I would share a few of the things that I learned while going through Object Bootcamp last week. The first, and probably the biggest thing was the idea of memory equivalence. This can be show easily by also showing another thing that I learned which was how to use the Null-Object design pattern. This is a pattern that I have read about, but have never really seen used until now.

The Null-Object pattern is used to return a null object instead of a null. The former is better since it removes the chance of encountering a NullPointerException. The idea is to return an object that basically does nothing, i.e., its methods simply return default, useless data. The trick is in how to implement this object.

Say we have an email object that represents an email address and can send an email when the appropriate method is called; however, the email address may or may not exist. So instead of returning null, we want to return a null-object when we do a search. So here is our email object:

public class email {
  private String emailAddress;

  public boolean sendEmail(String message) {
    // send out email using Javamail or something
  }
}

Now here is the same class utilizing the null-object pattern, the trick is the static instantiation.

public class Email {

  public static Email noEmailAddress() {
    public boolean sendEmail(String message) {
      return false;
    }
  };

  private String emailAddress;

  public boolean sendEmail(String message) {
    // send out email using Javamail or something
  }
}

The sensible default is to always answer false (i.e., the email was not sent) when sending the sendEmail() message to the Email object. This way, it does what we expect and doesn’t break anything.

*Update:* ignore the following since, although it does make the code more readable, it breaks with the intention of the null-object pattern. As pointed out to me in the first comment.

Another cool feature of using the null-object pattern is that we can now write code that looks like this:

if (returnedEmailAddress==noEmailAddress) {
  // do something about it
}

Instead of checking to see if the result is null.

if (returnedEmailAddress==null) { //do something }

This makes the code a lot more readable and understandable. Just by looking at the code, we can understand immediately what happened. The trick to this is that we only ever instantiate one noEmailAddress object. This allows us to check that the two references point to the same space in memory and results in a very nice equivalency check instead of having to use returnedEmailAddress.equals(noEmailAddress).

Pair Programming - Learning Accelerated

For the past week I have been having my first real taste of pair programming and “real” test-driven development. One thing that I have noticed is the huge increase in learning that it provides. As a programmer, there is a limit to what you can teach yourself. By pairing, you remove that limit and accelerate the learning. I have learned things this week that I would have never learned on my own (and the week isn’t even over yet). My coding skills have grown more in a week then they did in a month at my last employer.

Immutable Objects

I am back in Chicago for more training. This time it is Object Bootcamp. One thing that we touched on today was the idea of immutable objects. Put simply, this means that the state of an object cannot be changed. From a conceptual point of view, this makes a lot of sense. If you have a Money and you create a new object worth $5.00 there is no way that you should be able to change that into $2.00. If you want to have $2.00, you need to create a new object since in reality you would no longer have $5.00.

The trick comes when you need to scale this up to more complex domain objects which have more then one or two attributes. So for example, how does this translate when you have a Class that represents a person. If that person’s name changes, does that make them a new person? Probably not. However, if their address changes, and the address is represented by an object composited within Person, then you can make a very strong argument that it represents a new address. Therefore, a new Address object needs to be instantiated.

The same argument could be made for a person’s email address or phone number.

This idea becomes important when you think about creating multi-threaded systems. Having all of your domain objects immutable means that they are all read-only. This means that you really do not have any shared resources between threads and it reduces the complexity of the entire application.

I am still thinking all of this through and have yet to try any of it on a real life application. In theory though it makes a lot of sense, but I am not sure how well it will work in practice. I think there will always be some information in domain model objects, or any class for that matter, that it makes sense to have change and thus violates this idea of having all objects be immutable.

Two finger scrolling

I am not sure what it is called, but the ability, on my Macbook Pro, to use two fingers on the touchpad to do things like scrolling just makes so much sense. The only problem I have with it is that when I start using a PC, I find myself trying to scroll with two fingers. It just makes so much sense and, in terms of muscle memory, is incredibly easy to pick up and remember. I also find that even when I have a mouse attached, I still end up scrolling with two fingers instead of using the mouse wheel. When it comes to design and usability, Apple gets it right.

The future of Human-Computer Interaction

The video below demonstrates the power of multi-touch, the technology that apple put into the iPhone, as it should be used. It is cool in a phone, but when used as the interface for an entire computer, it has the potential to change the way we interact with and think about computers. If you have ever seen Minority Report, this is it. This is the future.

Read more »

Agile or smart people - which is better from client’s perspective

I had an interesting discussion with another ThoughtWorker yesterday about what is more important to a client: the fact that ThoughtWorks uses an Agile process or that we try to employ smart people. He voted for Agile and I voted for the smart people.

I can definitely see his point of view, the client wants results and Agile is what we use to achieve those results. On one level this makes a lot of sense; however, I would argue that it is actually the people that make the difference. I have read on another blog that smart/proficient/great developers do not use Agile. Instead, for each project they are on, they assemble a bunch of tricks that they know will work and that collection of tricks and techniques becomes the process. As such, they are able to walk into any situation and be successful.

I personally tend to agree with this opinion. Agile is only our current thinking; it is an expression of the current state of thinking on how to deliver quality software successfully. In a few years it may be replaced by a new way of thinking (in fact, that is already starting to happen with lean). Good developers are able to adjust to these changes and still deliver a quality solution regardless of the current trend in software development thinking and practice.

On the other hand, since I have no real experience dealing with clients, I could be totally out to lunch on this.

On the subject of DAOs and Interfaces

Over the last little I have been wondering about this whole design principle of designing to interfaces and not concrete classes. I have no problem with the principle; people far more experienced then me created it. I do have a small question though: when using the DAO pattern, how do you stop from having an explosion of Interfaces?

The last project I was working on had about 6 or 7 DAOs. Each of these DAOs implements an interface. That means that for each class in the persistence layer there was an interface and a concrete implementation. For each interface there is only one class and there will probably only ever be one concrete class implementing each interface.

So, my simple question is: does this make sense? This pattern also shows up in other layers and parts of the code. I understand the need for interfaces and the need to hide the parts that change behind an interface or abstract class, but putting an interface in front of almost every class that exposes a public interface makes no sense to me. How do other people expose their DAOs to the rest of their applications?

So far two solutions have come to mind. The first is to use the Facade pattern and hide several related DAOs behind a single class that implements a single interface. The second solution is to not use the DAO pattern. The first solution makes a lot of sense to me, but my fear is that it could result in facades with a very large number of methods.

I am not sure about the second method. The only way of implementing that makes any sense is by following Rails and moving the responsibility for saving, updating, and finding objects into the domain model and out of the persistence layer. This means that each domain object would be responsible for saving, updating or finding itself. Which on one level makes a lot of sense; however, it ties the persistence of those layers into one technology such as a Hibernate.

Next Page »