Introduction To Aspect Oriented Programming

in #steemstem7 years ago

programming.jpg

What is Aspect Oriented Programming? Why use it? How can I implement it in my software? Let's try and answer !

The Paradigm

Aspect Oriented Programming, AOP for short, is a perhaps less well known programming paradigm, unlike object oriented programming, procedural, imperative, functional. It aims to solve a big issue that arises when designing a piece of software, namely separations of concern. Separation of concern is one of those pesky issues that seems very sensible in theory, but it's tricky to implement, especially in large scale products. Separating concerns (or features) into modular independent blocks has many benefits, namely code reusability. It makes a project easy to maintain and upgrade. Having this in mind, it's worth pointing out the fact that, when dealing with badly designed product, the time it takes for a developer to add or debug a feature is more or less exponential as a function of project size.

Why use it?

As I mentioned in my previous paragraph, AOP comes in handy when wanting to separate different features in your software. Nearly all programming paradigms have some sort of system that helps a developer do this. For example, we have encapsulation and the grouping of code into classes, provided by object oriented programming. We have functions, given by procedural programming. However, what happens when we have a feature, let's say a logging feature that is ever present throughout our code? This feature "cuts across" multiple abstractions and cannot be separated using the other paradigm's methods. Let's take a moment and describe an example. Say we have a banking application, that is separated into an accounting module, an atm module, a retail module, a database module and a logging module. The first four modules are separated, using standard object oriented methods, but then we have the logging, which needs to know about all the other four. A diagram would look like this:

AspectArchitecture.png

This can seem a bit daunting, but we have done is not that complicated to understand. We have eliminated calling out the logger from our base code. Instead, we have constructed an Aspect (back on that in a bit), that knows when to invoke our code and log the data.

What is an Aspect?

We can imagine an aspect as being an additional class, that modifies the behavior of our base code by using advice and pointcuts. An advice represents additional code and a pointcut represents the location where our advice is executed.

Aspect.png

Use Cases

We can use AOP every time we have a feature that is tightly coupled into our code. Usual examples include:

  • Logging
  • Exception Handling
  • Debugging
  • Caching
  • Testing

Implementation

Many different programming languages have direct or indirect support for AOP. Java has AspectJ, .Net Framework with C#, Visual Basic have Post Sharp, Javascript has AspectJS, Python with aspectlib etc. I would very much like do to a demo of AOP in a future post, go through the code, maybe in the language you guys decide in the comments!

Conclusion

As we have seen in this, I would say very gentle introduction, AOP gives us a powerful tool in making our software more modularised. It is by no means a perfect solution, it has drawbacks, like writing extra code, and peculiar control-flow, that may resemble the much maligned go to statements of old. It is however a good concept for software engineers to know and maybe use it, under the right circumstances.

Sources

Sort:  

What an exceptionally professional write up, well done, the potential for use cases in debugging i find the most fascinating, if it is that it develops in the code line, it can automatically be initiated, quite innovative and adaptive.

Just dropped a new post myself, about the latest 24 Hour Sydney Train Strike check it out and follow back if you get a chance.

Thank you for your kind words! You are correct, debugging is a great usecase. I hope to make an AOP tutorial in the next days and showcase some features.

Do you think the originalworks bot is dead?

I sure hope not, but I haven't received an upvote in days from it..

On its profile page it says its being reconfigured

This post has received a 12.91 % upvote from @moneymatchgaming thanks to: @prometheus21. Upvote this Post to Support the MMG Community on Steemit! :)

This post has received a 4.28% upvote from @lovejuice thanks to @prometheus21. They love you, so does Aggroed. Please be sure to vote for Witnesses at https://steemit.com/~witnesses.

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly – Featured Posts are voted every 2.4hrs
Join the Curation Team Here

Thanks for your support!
the vincentb effect

Coin Marketplace

STEEM 0.19
TRX 0.17
JST 0.033
BTC 64188.14
ETH 2766.12
USDT 1.00
SBD 2.66