The Artima Developer Community
Sponsored Link

Weblogs Forum
AOP Idiom - Refactor First

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Dale Asberry

Posts: 161
Nickname: bozomind
Registered: Mar, 2004

AOP Idiom - Refactor First (View in Weblogs)
Posted: Sep 30, 2004 8:53 AM
Reply to this message Reply
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.

Topic: Rete Networks and Tuple Spaces Previous Topic   Next Topic Topic: Java and coolness, a discussion

Sponsored Links



Google
  Web Artima.com   

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