InfoQ

Interview

AOP Refactoring with Ramnivas Laddad

Interview with Ramnivas Laddad on May 07, 2007 08:00 AM

Community
Java
Topics
AOP
Tags
AspectJ
Summary
AOP expert Ramnivas Laddad explains how to use Aspects for refactoring cross-cutting concerns within classes (not just across classes) for things like reducing boiler plate code and potential for mistakes. How to recoganize and refactor such logic into aspects is covered, as well as applying aspects for resource management and concurrency control.

Bio
Ramnivas Laddad is an author, speaker, consultant, and trainer specializing in aspect-oriented programming and J2EE. His most recent book, "AspectJ in Action: Practical aspect-oriented programming" (Manning, 2003), has been labeled as the most useful guide to AOP/AspectJ. Ramnivas works for Interface21, providers of Spring.
Ramnivas can you please introduce yourself and tell us what you're up to?
How would one go about adopting AOP?
What are refactoring aspects?
Can you give us some examples on what kind of code we can refactor into aspects?
Most people think of aspects for modularizing cross-cutting concerns across objects. But you're talking about cross methods. Isn't that going a bit too far?
When one should put an aspect within an object versus externalized?
You mentioned in the past that concurrency and resource management are also good applications for aspects. Can you explain ?
What about concurrency? How can aspects be used to refactor concurrency control?
Isn't it dangerous to lose the explicitness of putting in the code? If you get used to not coding in these constructs and you're used to relying on aspects, what if you forget to assign your point-cut?
You mentioned that you could find yourself doing repetition even within aspects. Can you give us an example? How would you solve that problem?
Any final words on AOP refactoring?
show all  show all
Not a good example by Cristian Herling Posted May 8, 2007 4:55 PM
Re: Not a good example by Ramnivas Laddad Posted May 8, 2007 9:09 PM
Re: Not a good example by Cristian Herling Posted May 9, 2007 9:16 AM
  1. Back to top

    Not a good example

    May 8, 2007 4:55 PM by Cristian Herling

    "Whole try-catch blocks repeated in many methods" and "code that repeats across multiple methods in one class" are a code smell, a sign of bad design. There are valid OOP ways to tackle these problems, we don't need to throw AOP into this mix for solving this problem (if anything, the problem becomes more complex). I find AOP to be most successful at injecting behavior which should be kept outside of objects into the objects themselves. This interview is an example of injecting object-specific behavior into an object thru aspects, I think that we are using a sledge-hammer to knock out a mosquito.

  2. Back to top

    Re: Not a good example

    May 8, 2007 9:09 PM by Ramnivas Laddad

    C H, Take a look at http://www.theserverside.com/tt/articles/article.tss?l=AspectOrientedRefactoringPart1 and http://www.theserverside.com/tt/articles/article.tss?l=AspectOrientedRefactoringPart2. Which valid OOP ways can tackle the examples discussed? We can have beneficial discussion if you can show OO-based versions of the classes shown. Then we can compare and contrast two ways of doing the same thing. -Ramnivas

  3. Back to top

    Re: Not a good example

    May 9, 2007 9:16 AM by Cristian Herling

    The example you mention is actually valid, I wished that in the interview you would have gone a bit more in detail, mentioning only that you would use AOP for getting rid of code that is repeated across methods is a bit confusing. If anything it raises the possibility of bad design (this is the first thing I think of when I hear these words). I would have added the link from your reply to the interview and would have referred to it during the interview, it would have made a few things a lot more clearer. I have 2 things to say about this example however: 1) The aspect that you apply to the methods in questions is really a small part of a large cross-cutting concern: security. I get the feeling that the "micro-cross-cutting concern" should actually be part of a larger cross-cutting concern. If you are using a security micro-cross-cutting concern in Account.java and another one in PurchaseOrder.Java and yet another one in PeopleDirectory.java, you are basically spreading security handling all over the code-base. I get the feeling that these micro-cross-cutting concerns should be consolidated into one larger concern. Please let me know what you think about this. 2) I think you can still go OOP in order to solve the problem by using decorators (declare interface Account and implementations SecuredAccount and RegularAccount, SecuredAccount having a link to an Account). Decorators don't react well to interface changes: let's say that you need to add another method to the interface Account, all the implementations will have to be changed, but this is the overhead for using them. I still think that the example of micro-cross-cutting concerns takes AOP a bit too far. However, I found the examples at the beginning of the interview very interesting because they apply AOP to cross-cutting concerns.

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.