Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aspect-Oriented Programming in Clojure

Tags:

jvm

clojure

aop

How to achieve Aspect-Oriented Programming in Clojure? Do we need AOP in Clojure?
Let's say we want plain vanilla Clojure solution (no AspectJ).

like image 268
Chiron Avatar asked Apr 06 '11 21:04

Chiron


People also ask

What is meant by Aspect Oriented Programming?

Aspect-oriented programming (AOP) is an approach to programming that allows global properties of a program to determine how it is compiled into an executable program. AOP can be used with object-oriented programming ( OOP ). An aspect is a subprogram that is associated with a specific property of a program.

Is AOP still used?

It is still in use mainly for logging and opening/closing database translations by several java frameworks (Spring for example, Roo). Problem with AOP is that if you overuse it, your program will be hard to maintain..

What is the reason to 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.

What is aspect oriented model?

1. Aspect oriented modeling (AOM) techniques allow system developers to describe solutions that crosscut a design in separate design views called aspects. Learn more in: Aspect-Oriented Analysis of Security in Distributed Virtual Environment.


2 Answers

Aspect-Oriented Programming is typically used to add cross-cutting functionality to code that would otherwise get hopelessly intertwined with business logic. A great example is logging - you don't really want logging code scattered everywhere in your code base.

You don't really need AOP in Clojure because it's easy to achieve this with other techniques in Clojure.

For example, you can use higher-order functions to "wrap" other functions with cross cutting functionality:

; a simple function - the "business logic"
(defn my-calculation [a b]
  (+ a b))

; higher order function that adds logging to any other function
(defn wrap-with-logging [func]
  (fn [& args]
    (let [result (apply func args)]
      (println "Log result: " result)
      result)))

; create a wrapped version of the original function with logging added
(def my-logged-calculation (wrap-with-logging my-calculation))

(my-logged-calculation 7 9)
=> Log result:  16
=> 16
like image 55
mikera Avatar answered Jan 01 '23 05:01

mikera


AOP IMHO is just an artifact of certain kinds of static programming languages. AFAIKS it's usually just a bunch of non-standard compiler extensions. I've not yet seen any application of AOP that can't be solved better & natively in more dynamic languages. Clojure is certainly dynamic enough, and that's without even considering macros.

I may be wrong, but if so, I'd need to see an actual AOP use case that can't be implemented just as well in pure clojure.

Edit: just to be clear: I refuse to see things like elisp's advice as aspect oriented. In dynamic languages, those are just techniques to be used whenever you need them, with no need for language support other than rebinding of function definitions - which all lisps support anyway.

There's no need to treat them as special - you can easily define your own defadvice-like function in clojure. See for example, compojure's wrap! macro, which is actually deprecated since you generally don't even need it.

like image 31
Joost Diepenmaat Avatar answered Jan 01 '23 05:01

Joost Diepenmaat