WTF is AOP
AOP or Aspect Oriented Programming
http://en.wikipedia.org/wiki/Aspect-oriented_programming
Aspect-oriented programming entails breaking down program logic into distinct parts (so-called concerns, cohesive areas of functionality). Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., functions, procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they “cut across” multiple abstractions in a program.
Logging exemplifies a crosscutting concern because a logging strategy necessarily affects every logged part of the system. Logging thereby crosscuts all logged classes and methods.“
In a nut shell : AOP lets you add behaviour to code without needing to change the code. Magic.
A good presentation on this from a Javascript point of view : http://www.slideshare.net/SpringCentral/ao-ping-yourjavascript
A brilliant analysis of different language approaches here : http://www.garfieldtech.com/blog/language-tradeoffs
Aspect-oriented approaches emphasize Modifiability, Extensibility, and Expediency at the expense of Testability, Verifiability, and arguably Understandability.
Key concepts
- Joint points = Where some other code can jump in and change things.
- Pointcuts = The code that does the jumping in.
Stereotypical AOP examples
- Logging
- Profiling
- Transaction boundaries
- Security
AOP in Javascript
- Actually really easy, as it just replaces the method!
- Several libs / frameworks to do this :
- AOP in 50 LOC - https://github.com/briancavalier/aops2gx-2013/tree/master/src
- cujoJS’s meld - https://github.com/cujojs/meld
- Dojo’s dojo/aspect - http://dojotoolkit.org
- Twitter FlightJS - https://github.com/flightjs/flight
- javascript-hooker - https://github.com/cowboy/javascripthooker
- dcl - https://github.com/uhop/dcl
They all work in basically the same kind of way, but have their own take on things.
Things like AOP
Drupal Hooks
Much of Drupal’s modular power comes from its hook system, which really can be thought of as a AOP hack. Any module or code can offer up hook points (aka joint points).
http://stackoverflow.com/questions/130467/how-does-aop-work-in-drupal
Other modules can then implement these hooks (pointcuts).
HapiJS extension points
HapiJS’s server exposes what it called "extension points” (joint points) which are events fired buy the server at parts of the Request lifecycle.
http://spumko.github.io/resource/api/#server-ext-event–method—options–
You can write plugins which extend from these points. (pointcuts)