That's right, not object-oriented, but aspect-oriented. What's that all about then?

Computers store data, and computer programs do stuff to the data. You can write computer programs in that way: basically as a list of things to do to some data. But those programs end up very messy (as I know from experience!). So we need a better way of representing what is going on.

Enter object-oriented programming (OOP). We represent what is going on under the surface in terms of objects. An object is the basic unit of a program. It hides (encapsulates) some data, and if you ask it nicely, it will do something and tell you what you need to know. A computer program is now a collection of objects that interact with each other to perform some complex task. This is much better, as the software developer can focus on crafting elegant objects (or, more accurately, kinds of objects, or classes). If these are well designed, the program will be robust, and you can change little bits of it without worrying that the whole thing will break.

But wait: what if we can't represent all that a program needs to do in terms of individual objects? What if there are some concerns that are relevant for every element of the program? For example, a typical program might be littered with statements dealing with logging, security, or handling errors. These are concerns that cut across the basic functionality of a program.

Enter aspect-oriented programming (AOP). This provides a way of separating out those cross-cutting concerns into separate aspects, which provide additional commands to be executed at particular points (or particular kinds of points) in the program execution.

I'm going to give it a try with the Java development I'm working on. I'll let you know how it goes. It should be useful for testing, allowing me to keep track of how many times a particular method is executed, what the values of the parameters are, how much time is spent on different steps, and what is making it go wrong, for example.

Fortunately, it's dead easy to get started. For Java, the most widely-used AOP extension is AspectJ. If you have a Java project in Eclipse, simply install the AspectJ Development Tools plugin, convert the project to an AspectJ project, and that's it. Here's "Hello world" using an aspect:

public class HelloWorld {
    private void sayHello() {
        System.out.println("Hello");
    }

    public static void main(String[] args) {
        new HelloWorld().sayHello();
    }
}

public aspect World {
    pointcut greeting():
        execution(* HelloWorld.sayHello(..));

    after() returning: greeting() {
        System.out.println("World!");
    }
}

The sayHello method executes, and then the aspect interrupts the execution and prints "World!".

Part of the attraction of AOP to me is that it has resonances with the philosophy of Herman Dooyeweerd, who worked on a model of reality called the Theory of Aspects, in which the whole of reality can be described in terms of (around) 15 (cross-cutting) aspects. In fact, I first heard of AOP via Andrew Basden's page on the topic.



comments powered by Disqus