HAYCORN — 8 May 2011

Why Aspect-Oriented Programming? What's wrong with pub/sub (or hooks)?

Is there any substantive difference between Aspect-Oriented Programming and the publish/subscribe messaging pattern, where you explicitly publish a message at join points? (I consider hooks and the observer pattern to be pretty equivalent to pub/sub, too.)

To ask the question another way, is there really any point pushing for existing languages to be enhanced to support AOP when you could just use pub/sub and get nearly all the benefits? With pub/sub you avoid all the syntax weirdness, the source code transformation weirdness (if you’re doing it that way), the cultural resistance from introducing AOP to language that doesn’t already have it, the need to learn a new syntax/DSL to express join points…

The main disadvantage of hooks seems to be that you need to be able to modify existing code to use them. They’re also ugly if you want hooks absolutely everywhere, but against this they’re explicit, they can be triggered at absolutely any point, and they don’t need language support.

(Some examples: WordPress, Rails.)