Why Should We Use Aspect-Oriented Programming ?

getspooky

Yasser Ameur el idrissi

Posted on August 13, 2020

Why Should We Use Aspect-Oriented Programming ?

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a pointcut specification, such as "log all function calls when the function's name begins with set. This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code, core to the functionality. AOP forms a basis for aspect-oriented software development.

Okay it might look scary when you read the definition. No worries I got you covered in this article. Let’s deep dive into the some of issues that you would face when writing the code in a method/function.

Let’s look at the following example pseudo-code :

public notify(toPerson: Person, data: string): void {
  if(toPerson.isBlockList()) {
    throw new UserBlockedException();
  }
  send(toPerson,data);
} 
Enter fullscreen mode Exit fullscreen mode

It is simple method whose core functionality is send notifation message. But it lacks certain considerations so lets add logging for diagnostics to the above notify method. The code might look like this

public notify(toPerson: Person, data: string): void {
  Logger.info(`Entered sendMessage ${toPerson.name}`);
  if(toPerson.isBlockList()) {
    Logger.warn(`${toPerson.name} is blocked by recepient"`);
    throw new UserBlockedException();
   }
   send(toPerson,data);
   Logger.info('Exiting sendMessage');
} 
Enter fullscreen mode Exit fullscreen mode

You see that there are bits of code for logging that have become tangled with the core business functionality. These cross-cutting concerns are almost same for most of the methods. Security, logging, API analytics (ex: to measure how much time each method took) etc are some of the examples for cross-cutting concerns.

Voila!, AOP solves the issue by encapsulating the cross-cutting concerns in a modular element called aspect. An Aspect is just like any other modular implementation (like a class) in Object-oriented programming(OOP).

Common terminology in AOP :

  • An Aspect is module of code that encapsulates the cross-cutting concerns.

  • Join point The point in the program that has to be subjected to an aspect. Join point can be method level, constructor level, field level.

  • Advice Specifies the action that should be executed on the join point and also specifies when should the action be executed, like should it be applied before entering the method, after returning from the method, after an exception from method , both before and after the method etc. An Aspect has group of advices.

Conclusion

Aspect-oriented Programming is an extension of Object-oriented Programming. AOP complements Object-oriented programming by increasing modularity. Now have a look at the textbook definition that I have mentioned earlier, I hope that it would not be scary instead be more meaningful. I would suggest you to practice and use AOP wherever possible. This is one of paths for being better programmer.

💖 💪 🙅 🚩
getspooky
Yasser Ameur el idrissi

Posted on August 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related