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

8 May 2011

Is there any sub­stan­tive dif­fer­ence between Aspect-Oriented Programming and the publish/subscribe mes­sag­ing pattern, where you ex­plic­itly publish a message at join points? (I con­sider hooks and the observer pattern to be pretty equiv­a­lent to pub/sub, too.)

To ask the ques­tion another way, is there really any point pushing for existing lan­guages to be en­hanced 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 trans­for­ma­tion weird­ness (if you’re doing it that way), the cul­tural re­sis­tance from in­tro­duc­ing AOP to lan­guage that doesn’t already have it, the need to learn a new syntax/DSL to express join points…

The main dis­ad­van­tage of hooks seems to be that you need to be able to modify ex­ist­ing code to use them. They’re also ugly if you want hooks ab­solutely everywhere, but against this they’re explicit, they can be trig­gered at ab­solutely any point, and they don’t need lan­guage support.

(Some examples: WordPress, Rails.)