Now that you’ve seen what it is and what it does, you’ve probably guessed why people would want to use Aspect-Oriented Programming, but let’s quickly do a round-up:

  • Great way to encapsulate cross-cutting concerns. I’m a big fan of encapsulation because it means you get easier to read and easier to maintain code that can be re-used all across your project.
  • Flexible logic. The logic around your implementation of the advice and pointcuts can give you a lot of flexibility when it comes to injecting your aspects. This in turn can help to dynamically turn on and off different aspects (pun definitely intended) of your logic.
  • Re-use aspects across projects. You can think of aspects as components, small and decoupled pieces of code that can run anywhere. If you write your aspects correctly, you can share them across different projects with ease.

Because not everything is perfect, some detractors speak against this paradigm.

Their main problem with it is that its main benefit is actually hiding logic and complexity, potentially causing side effects without being too clear about it.

And if you think about it, they’re kind of right, AOP gives you a lot of power to add unrelated behavior into existing methods or even replace their entire logic. Of course, that might not be exactly why this pattern was introduced, and it’s certainly not the intention of the example I provided above.

However, it does provide you with the ability to do whatever you want, and that, coupled with a lack of understanding of good programming practices, can cause a really big mess.

And without trying to sound too cliched here, paraphrasing Uncle Ben:

With big power, comes great responsibility

And with AOP comes the obligation to understand software development best practices if you want to use it correctly.

But if you ask me, just because with this tool you can cause a lot of harm, it doesn’t mean it’s bad, because you can also cause a lot of good (i.e you can extract a lot of common logic into a centralized location and inject it wherever you need, with a single line of code). That to me is a powerful tool worth learning about and definitely, worth using.