sábado, enero 08, 2011

No Me Digas Más Y Márchate, No Llames Amor A Tú Hipocresía

I am a huge fan of Clean Code Book written by Robert Martin Clean Code: A Handbook of Agile Software Craftsmanship. I think it is a 'must have' that every developer should read at least one time. Someof recurrent themes in the book is following a rule called KISS (Keep It Simple, Stupid) [Tangling] and DRY (Don't Repeat Yourself) [Scattering], and a full chapter about naming. Now I am going to explain how to apply this principle when developing Aspects with Spring AOP.

AOP enables a clean and simple solution for Tangling and Scattering problem. Assuming you have developed one aspect using Spring AOP, I will explain how to make your aspects look cleanly.

I suppose that a typical Aspect is written like (SpringSource example):


@Aspect
public class BeforeExample {

  @Before("execution(* com.xyz.myapp.dao.*.*(..))")
  public void doAccessCheck() {
    // ...
  }

}


First of all, a naming problem is encountered. We are mixing the pointcut with the advice. Moreover when you are reading this aspect, if you have not written the pointcut expression, you have the problem of interpreting the pointcut, for knowing which classes/methods does apply the doAccessCheck logic.
How about creating a method with an expressive name, that allows us to know when doAccessCheck() happens?


@Aspect
public class BeforeExample {
  @Before("daoMethods()")
  public void doAccessCheck() {
    // ...
  }
  @Pointcut("execution(* com.xyz.myapp.dao.*.*(..))")
  public void daoMethods(){} //Empty Method
}

Read now when is executed doAccessCheck() .

"Before [@Before] dao methods ["daoMethods()"]  do access check [doAccessCheck()]"

Yes! now this aspect is readable and understandable. Also without knowing AOP language, you can understand what this code does.

Moreover of using Named Pointcuts, some Best Practices should be followed:


  • Use the simplest possible advice. Avoid using @Around.
  • Use strongly-typed context when possible. Avoid getting data from join point object.

@Pointcut("execution(void com.xyz.myapp.Server+.start(java.util.Map)) && args(input)")
public void serverStartMethod(Map input){}

@Before("serverStartMethod(input)")
public void logServerStartup(Map input) {
...
}


  •  Create complex pointcut expressions from simple ones.



@Pointcut("execution(public * *(..))")
public void anyPublicOperation(){}

@Pointcut("within(com.xyz.someapp.trading..*)")
public void inTrading() {}

@Pointcut("anyPublicOperation() && inTrading()")
public void tradingOperation() {}

  • Use scoping designators (within, withincode) for fast matching. Avoid using kinded designators (execution)  or contextual designators (this, target) alone.

Now you can write your Spring AOP Advices cleanly and for optimal performance.

3 comentarios:

Anónimo dijo...

Urteter nuytre: http://intimvip.xhost.ro

Anónimo dijo...

Thanks for that book man, Its indeed useful. keep the good work going.

Javin
How HashMap works in Java

Alex dijo...

Thank you for your comment, I think like you that this book is a "must have" it talks about simple facts, but facts that are forgotten when you are developing.

Alex.