AOP and Dynamic Languages: Contradiction in Terms or Match Made in Heaven? 34
Consider this quote from Dave Thomas (of the Pragmatic Programmers) on AOP (aspect-oriented programming, a.k.a. aspect-oriented software development - AOSD) :
Once you have decent reflection and metaclasses, AOP is just part of the language.
People who work with dynamic languages don't see any need for AOP-specific facilities in their language. They don't necessarily dispute the value of AOP for Java, where metaprogramming facilities are weaker, but for them, AOP is just a constrained form of metaprogramming. Are they right?
It's easy to see why people feel this way when you consider that most of the applications of AOP have been to solve obvious "cross-cutting concerns" (CCCs) like object-relational mapping, transactions, security, etc. In other words, AOP looks like just one of many tools in your toolbox to solve a particular group of problems.
I'll use Ruby as my example dynamic language, since Ruby is the example I know best. It's interesting to look at Ruby on Rails source code, where you find a lot of "AOP-like" code that addresses the CCCs I just mentioned (and more). This is easy enough to do using Ruby's metaprogramming tools, even though tooling that supports AOP semantics would probably make this code easier to write and maintain.
This is going to be a long blog entry already, so I won't cite detailed examples here, but consider how Rails uses method_missing
to effectively "introduce" new methods into classes and modules. For example, in ActiveRecord, the many possible find
methods and attribute read/write methods are "implemented" this way.
By the way, another excellent Ruby framework, RSpec used method_missing
for similar purposes, but recently refactored its implementation and public API to avoid method_missing
, because having multiple frameworks attempt to use the same "hook" proved very fragile!
Also in Rails, method "aliasing" is done approximately 175 times, often to wrap ("advise") methods with new behaviors.
Still, is there really a need for AOP tooling in dynamic languages? First, consider that in the early days of OOP, some of us "faked" OOP using whatever constructs our languages provided. I wrote plenty of C code that used struct
s as objects and method pointers to simulate method overloading and overriding. However, few people would argue today that such an approach is "good enough". If we're thinking in objects, it sure helps to have a language that matches those semantics.
Similarly, it's true that you can implement AOP using sufficiently powerful metaprogramming facilities, but it's a lot harder than having native AOP semantics in your language (or at least a close approximation thereof in libraries and their DSLs).
Before proceeding, let me remind you what AOP is for in the first place. AOP is essentially a new approach to modularization that complements other approaches, like objects. It tries to solve a group of problems that other modularity approaches can't handle, namely the fine-grained interaction of multiple domain models that is required to implement required functionality.
Take the classic example of security management. Presumably, you have one strategy and implementation for handling authentication and authorization. This is one domain and your application's "core business logic" is another domain.
In a non-AOP system, it is necessary to insert duplicate or nearly duplicate code throughout the system that invokes the security subsystem. This duplication violates DRY, it clutters the logic of the code where it is inserted, it is difficult to test, maintain, replace with a new implementation, etc.
Now you may say that you handle this through a Spring XML configuration file or an EJB deployment configuration file, for example. Congratulations, you are using an AOP or AOP-like system!
What AOP seeks to do is to allow you to specify that repeated behavior in one place, in a modular way.
There are four pieces required for an AOP system:
1. Interception
You need to be able to "intercept" execution points in the program. We call these join points in the AOP jargon and sets of them that the aspect writer wants to work with at once are called pointcuts (yes, no whitespace). At each join point, advice is the executable code that an aspect invokes either before, after or both before and after ("around") the join point.
Note that the most powerful AOP language, AspectJ, let's you advise join points like instance variable reads and writes, class initialization, instance creation, etc. The easiest join points to advise are method calls and many AOP systems limit themselves to this capability.
2. Introduction
Introduction is the ability to add new state and behavior to an existing class, object, etc. For example, if you want to use the Observer pattern with a particular class, you could use an aspect to introduce the logic to maintain the list of observers and to notify them when state changes occur.
3. Inspection
We need to be able to find the join points of interest, either through static or runtime analysis, preferably both! You would also like to specify certain conditions of interest, which I'll discuss shortly.
4. Modularization
If we can't package all this into a "module", then we don't have a new modularization scheme. Note that a part of this modularization is the ability to somehow specify in one place the behavior I want and have it affect the entire system. Hence, AOP is a modularity system with nonlocal effects.
Okay. How does pure Ruby stack up these requirements? If you're a Java programmer, the idea of Interception and Introduction, where you add new state and behavior to a class, may seem radical. In languages with "open classes" like Ruby, it is trivial and common to reopen a class (or Module) and insert new attributes (state) and methods (behavior). You can even change previously defined methods. Hence, Interception and Introduction are trivial in Ruby.
This is why Ruby programmers assume that AOP is nothing special, but what they are missing are the complete picture for Inspection and Modularization, even though both are partially covered.
There is a rich reflection API for finding classes and objects. You can write straightforward code that searches for classes that "respond to" a particular method, for example. What you can't do easily is query based on state. For example, in AspectJ, you can say, "I want to advise method call X.m when it is called in the context flow ('cflow
') of method call Y.m2 somewhere up the stack..." Yes, you can figure out how to do this in Ruby, but it's hard. So, we're back to the argument I made earlier that you would really like your language to match the semantics of your ideas.
For modularization, yes you can put all the aspect-like code in a Module or Class. The hard part is encapsulating any complicated "point cut" metaprogramming in one place, should you want to use it again later. That is, once you figure out how to do the cflow
pointcuts using metaprogramming, you'll want that tricky bit of code in a library somewhere.
At this point, you might be saying to yourself, "Okay, so it might be nice to have some AOP stuff in Ruby, but the Rails guys seem to be doing okay without it. Is it really worth the trouble having AOP in the language?" Only if AOP is more applicable than for the limited set of problems described previously.
Future Applications of AOP??
Here's what I've been thinking about lately. Ruby is a wonderful language for creating mini-DSLs. The ActiveRecord DSL is a good example. It provides relational semantics, while the library minimizes the coding required by the developer. (AR reads the database schema and builds an in-memory representation of the records as objects.)
Similarly, there is a lot of emphasis these days on development that centers around the domain or features of the project. Recall that I said that AOP is about modularizing the intersection of multiple domains (and recall my previous blog on the AOSD 2007 Conference where Gerald Sussman remarked that successful systems have more than one organizing principle).
I think we'll see AOP become the underlying implementation of powerful DSLs that allow programmers who are not AOP-literate express cross-cutting concerns in domain-specific and intuitive languages. AOP will do the heavy lifting behind the scenes to make the fine-grained interactions work. I really don't expect a majority of developers to become AOP literate any time soon. In my experience, too many so-called developers don't get objects. They'll never master aspects!
Shameless Plug
If you would like to hear more of my ideas about AOP in Ruby and aspect-oriented design (AOD), please come to my talk at SD West, this Friday at 3:30. I'm also giving a full-day tutorial on AOD in Ruby and Java/AspectJ at the ICSE 2007 conference in Minneapolis, May 26th.
Trackbacks
Use the following link to trackback from your own site:
http://blog.objectmentor.com/articles/trackback/5268
Richard Mille RM 016 the glowing fires and second later
Remove DRM protection from all video and audio files.
I agree with one of the other posters – you write very well indeed.
Thanks for writing this blog post, it was informative, enjoyable, and most importantly – a good length!
Is it really worth the trouble having AOP in the language?
This site rocks, thanks for this great posting, I have bookmarked for future reference.:)
We are the professional jacket manufacturer, jacket supplier, jacket factory, welcome you to custom jacket.
You have a cool web site. It is almost the first time I encounter something like that. In fact I’m thinking of creating my own one.Google’s results pages are simple, and that’s why they work. Simple.So thanks for it.
Our backup software can help you take a snapshot for your contacts and SMS. Your important personal information will be never lost.
TopCombine Follow ipad bag the detail tips below, you can increase the laptop battery life of a year or more. Game Controllers first thing you should care about the USB Gadgets END!66666666
Thank you for the sensible critique. Me & my neighbor were preparing to do some research about that. We got a good book on that matter from our local library and most books where not as influential as your information. I am very glad to see such information which I was searching for a long time.This made very glad
Well done to Tim for promoting those rights and freedoms and well done and good luck to Anton Hysen
I’m amazed by this post. A really good one. Thanks guys. roof nokomis
Toutes les Basket Puma sont originales et directement de l usine Toutes les Pas cher Puma Homme,Femmes,Enfants chaussures magasin sont 30-70% de remise et livraison gratuite.
If only something like this came out sooner! Definitely mem’ing this
Perfect! Thanks for putting this together.
definetely nice article.. Thanks
Okey oynamak hiç bu kadar zevkli olmadi. Online ve 3 boyutlu okey oyunu oyna ve turnuvalara sende katil.
thanks for your great information
I really love your post Thank you :-)
Beats by dr dre studio with look after talk in white. extra attributes on Monster Beats By Dr. Dre Pro Headphones Black a specific tri-fold design and design and carrying circumstance which make for compact and uncomplicated safe-keeping when not in use. Beats by dr dre solo .
hiç bu kadar zevkli olmadi. Online ve 3 boyutlu
hiç bu kadar zevkli olmadi. Online ve 3 boyutlucheap beats by dre beats by dre store
Fail “McMansions, ” anthropological PV Module kids located at Northwestern Higher educatoin institutions for Illinois experience engineered consequently they are creating a 128-square-foot family home.
All the plan can be to Solar Module experience a good deal that men are able to get just by by means of very low breathing space.
Many thanks for this learning.. the usefulness is of it is absolutely great!
I’ve been going to shopping malls since I was on General Hospital.
It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested on. The points that the data stated are all first hand on actual experiences even help more. Go on doing what you do as we enjoy reading your work.
It’s such an honor to offer the opportunity to sign up for the debate of this excellent web log! I wish to prolong my thank you for this kind of.
If you want to see the mind blowing article with real facts and figures, this has really tremendous impacts on readers and I admire the writing skill of the author.
well. you guys really give us the sample of C++ programing skill. So. why not try this method and do a better code. next time. have another try.
It was a good read on my end and this information are very much valuable and significant to what I am researching. Thanks for the insight.
It’s a great pleasure to visit your website and to enjoy your excellent work! .
AOP and Dynamic Languages: Contradiction in Terms or Match Made in Heaven? 32 hoo,good article!!I like the post!15
AOP and Dynamic Languages: Contradiction in Terms or Match Made in Heaven? 33 good post176