Aspect Oriented Programming 101

Halil İbrahim ŞİMŞEK
3 min readApr 8, 2021

--

Aspect oriented programming(AOP) is a paradigm that promotes “Separation of Concerns(SoC)” in a software system.

Quick reminder, what was the “Separation of Concerns” , briefly “ Realization of problems domain into separate units of software.”(Dijkstra, Parnas). SoC provide us better analysis and understanding over system, maintainability and reusability.

But wait a minute, we achieve these with Object oriented programming(OOP) , or we don't?

In OOP, decomposition is focused on notion of class and it does not care about the behaviours which are unrelated with business logic. These behaviours may be logging , caching validation, transacion etc.. which span over many modules. In AOP, these behaviours called as “Cross-cutting Concerns”.

public class OOP{

public void anyTask(parameters){

//log the parameters

//check some precondition

//timer for performance

Businnes logic impl

//log result

//timer end

}

}

As it seen code snippet above, log, auth, performance test or precondition checks are not localized, these code pieces usually spans over classes.

Indications of cross-cutting ?

  • Code Scattering : Implementation of a concerns (logging,auth etc.) not modularized and implemented same piece of code many classes.
  • Code tangling : It occurs when there is a mixing of cross-cutting concerns with the application’s business logic. In such cases it is hard to catch the what is business logic. As it seen snippet above just 1 over 7 lines is responsible for business logic, remaining lines are giving solution for other unrelated concerns.

Target of the AOP is improve modularity by organizing these “Cross-cutting Concerns” which have to be applied at many places but don’t have anything to do with business logic. It keeps these concerns out of main code(business logic) and define them vertically. Then we name this separated piece of code as ASPECT. Aspect is bassically a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement.

As it seen in diagrams above, AOP is not something that we use instead of OOP but it complements the OOP.

What are the benefits of AOP?

  • Improvement over modularity
  • Increase in adaptability
  • Higher level of abstraction
  • Less tangled and scattered code
  • Increase in maintability

In the next article i will demonstrate how AOP is implemented over Java.

--

--

No responses yet