The Artima Developer Community
Sponsored Link

Indeterminate Heuristics
AOP Idiom - Refactor First
by Dale Asberry
September 30, 2004
Summary
AOP can do a lot of things but it can't clean up poorly structured code.

Advertisement

I'm a lurker on the AspectJ-Users and AspectJ-Dev mailing lists. Good nuggets often pop up there that should be disseminated to a larger audience.

Background

Sven Apel was asking the list for a way to advise a for-loop construct. Ron Bodkin pointed out that "to use AspectJ with code like this, you could use the extract method refactoring, which would let you name a helper method to describe the post inner-loop behavior you want to advise."

Solution

Gregor Kiczales responded with this code snippet:

void simulate() {
        ...
        processPeers(peers);
}

void processPeers(Iterator/*<Peer>*/ peers) {
        while(peers.hasNext()) {
                Peer p = (Peer)peers.next();
                processPeer(p);
        }
}

void processPeer(Peer p) {
        while(!p.finished()) {
                // process peer data
                // perform actions
        }
}

Conclusion

Follow good software engineering practices first. In this case, the "Extract Method" refactoring will make the code cleaner, as well as, provide a more natural pointcut that can be used with AOP.

Talk Back!

Have an opinion? Be the first to post a comment about this weblog entry.

RSS Feed

If you'd like to be notified whenever Dale Asberry adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

R. Dale Asberry been hacking since 1978, professionally since 1990. He's certified in Java 1.1 and has a four digit MCP number. He discovered Jini at the 2000 JavaOne and has been building incredibly cool, dynamic, distributed architectures ever since! Over time, he's discovered several principles that have contributed to his success - they are the Princples of: Enabling Others, Simplicity, No Complaining, Least Work, Least Surprise, Least Damage, and "It Just Works".

This weblog entry is Copyright © 2004 Dale Asberry. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use