• Home
  • >
  • Tech News
  • >
  • What is Aspect Oriented Programming? Benefits, drawbacks, common terms

What is Aspect-oriented programming, its benefits and harms, and what terms you should know when using AOP? All will be answered in today’s article.

While building application programs, we need to take care of many software-related issues. For example, we make an account creation registration system for a bank. In addition to the main job of allowing users to create accounts (core concern), the system also has to ensure other issues (cross-cutting concerns) such as user authentication, constraint checking, transaction management, and processing. Exception handling, logging, debugging, measuring application performance, etc.

As you can see, the logic of our program has to do a lot of things like logging, opening/closing transactions, handling exceptions, etc. When there are many similar methods in the application, our code is tightly coupled into each other, duplicate code, a fragment in many places, difficult to modify and add new logic, etc. To solve this problem, we can use the Aspect-oriented programming technique.

what-is-aop

What is Aspect-oriented programming (AOP)?

Aspect-oriented programming (AOP) is a programming paradigm in computer science that aims to increase modularity by separating cross-cutting concerns. It accomplishes this by adding additional behavior to existing code (a recommendation) without modifying the code itself, instead of specifying which code is modified separately via a “pointcut” specification, such as “log all function calls when the function’s name begins with’s.” This allows non-essential business logic behaviors (such as logging) to be added to a program without clogging the code essential to the functionality.

Aspect-oriented programming (AOP) refers to methods and tools that support the modularization of concerns at the source code level. In contrast, aspect-oriented software development refers to an entire engineering discipline.

Breaking down program logic into distinct parts is what aspect-oriented programming entails (so-called concerns, cohesive areas of functionality). Almost all programming paradigms support some level of encapsulation and grouping problems into separate, independent entities by providing abstractions (e.g., functions, procedures, modules, classes, methods) that can be used to implement, abstract, and compose these concerns. Some issues “cut across” multiple abstractions in a program and defy these implementation methods. These are known as cross-cutting concerns or horizontal concerns.

Read More:   6 Scary Outage Stories from CTOs – InApps 2022

Concerns that are typically cross-cutting include:

  • Caching
  • Validation of data
  • Transaction Processing Logging
  • Monitor access control 
  • Handling Errors
  • Session administration
  • Proof of input/output

Dominant Frameworks in AOP

Aspect-oriented programming includes programming methods and frameworks that support and implement code modularization. Let’s take a look at the three most common AOP frameworks:

dominant-frameworks-in-aspect-oriented-programming

  • AspectJ is a Java programming extension developed at the PARC research center. It has a Java-like syntax and IDE integrations to display a cross-cutting structure. It has its compiler and weaver, and when used, it allows you to use the entire AspectJ language.
  • JBoss: JBoss’s open-source Java application server is used for Java development.
  • Spring: It implements AOP using XML-based configuration and annotations that are interpreted using an AspectJ-supplied library for parsing and matching.

Aspect-oriented programming concepts and terminology

Joinpoint: Thousands of opportunities or points to apply Advice are available in an application. These are referred to as join points. For example, Advice can be applied whenever a method is invoked, an exception is thrown, or at any other issue. However, Spring AOP currently only supports method execution join points (advising the execution of methods on Spring Beans).

Advice: The job an Aspect is supposed to do, or the action the Aspect takes at a specific point. Advice is classified into five categories: before, after, around, after throwing, and after returning. Let’s go over each of the five types briefly.

  • Before: Executes before the advised method is called. The @Before annotation indicates it.
  • After: Executes after the advised method has finished, regardless of whether it was successful or not. The @After annotation indicates it.
  • After returning: Executes after the recommended method has been completed successfully, i.e., without runtime exceptions. The annotation @AfterReturning indicates it.
  • Around: This is the most potent piece of Advice. Because it wraps around and runs Before and After the recommended method. This type of Advice is used when we need to access a form or database frequently, such as caching. The @Around annotation indicates it.
  • After throwing: After the recommended method throws a Runtime Exception, this method is called. The annotation @AfterThrowing indicates it.
Read More:   Update Building a Hacker News Style Voting App with Orchestrate & Node.js

aspect-oriented-programming

Pointcut: Because it is impossible to apply Advice at every point of the code, the selected join points where Advice is finally applied are referred to as the Pointcut. These pointcuts are frequently specified using explicit class and method names or regular expressions that define a matching class and method name pattern. Let’s look at how it helps to reduce repeating code by writing once and using it at multiple points.

Introduction: It entails adding new methods and fields to a type. It enables you to add a new interface to any recommended object.

The target object: It is the object which is being advised by one or more aspects. Because Spring Aspect-oriented programming is implemented using runtime proxies, it is also known as the proxied object in spring.

Aspect: The Aspect is the class that implements the JEE application’s cross-cutting concerns (transaction, logger, and so on). It can be a regular class configured via XML or an annotated class with @Aspect Interceptor: It is an aspect with only one piece of Advice.

AOP Proxy: It is used to implement Aspect-oriented programming framework-created aspect contracts. The spring framework will be a JDK dynamic proxy or a CGLIB proxy.

Weaving: It is the process of connecting Aspect with other application types or objects to create a recommended thing. Weaving can take place at compile, load, or runtime. Spring AOP weaves during runtime.

Benefits and drawbacks of Aspect-oriented programming

Benefits of Aspect-oriented programming

  • Accept modularity.
  • Reduce the coupling of dependencies.
  • Make code more readable, reusable, and maintainable.

Drawbacks of Aspect-oriented programming

  • Because they are not included in programming languages, they must rely on libraries to function.
  • Debugging can be difficult because there is more to code flow than meets the eye.
  • Require disciplines to refrain from overuse.

Aspect-oriented programming is regarded as a “dark art” because it can intervene in your code and perform some black magic behind the scenes in the OOP world. It can, for example, change your method, replace the result, or even prevent your code from running.

Read More:   One Bank’s Path for Moving Deep Legacy Infrastructure into Cloud Native Operations – InApps Technology 2022

Despite its immense power, we must use AOP with caution and understanding. Unnecessary use of AOP in your application can be dangerous and result in a slew of unexpected errors. Aspect Oriented Programming is not a perfect substitute for Object-Oriented Programming. Instead, they serve as a companion.

 

Rate this post
I am Tam, Founder & CEO of InApps Technology, ranked 1st in Vietnam and 5th in Southeast Asia in Application Development and Custom Software Development. We build a team with a people-centered culture that serves our customers with the WOW experience. I have helped hundreds of startups and brands to succeed with our robust technology solution. Besides, I have 5,000+ connections with C-Levels on LinkedIn and 10,000+ other professionals in communities. I hope to bring BIG VALUES to our right partners and customers. What I can help: – World-class software development service. – Building a dedicated talent team for only 30% of your local vendors’ cost. – Consulting tech startup solutions comprehensively and systematically. – Growth-hacking marketing solution. If you read here, don’t hesitate to contact me for further advice.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download

      Success. Downloading...