Aspect Orientated Programming in Ruby

31
Aspect Orientated Programming in Ruby Matthew Ford [email protected]

description

Talk at EURUKO2008

Transcript of Aspect Orientated Programming in Ruby

Page 1: Aspect Orientated Programming in Ruby

Aspect Orientated Programming in

RubyMatthew Ford

[email protected]

Page 2: Aspect Orientated Programming in Ruby

Matthew Ford

• Eclipse project AJDT @ IBM (AspectJ Development Tools)

• New Bamboohttp://new-bamboo.co.uk

Page 3: Aspect Orientated Programming in Ruby

Outline

• Introduction to Aspect Orientated Programming (AOP)

• Why use AOP?

• AOP in Ruby

• Demo

• Q & A

Page 4: Aspect Orientated Programming in Ruby

Intro to AOP

• AOP is a new way of thinking about software design.

Page 5: Aspect Orientated Programming in Ruby

• What is the ultimate goal when designing software?

• Aim to achieve a one-to-one mapping of design concepts to implementation code

Software Design

Page 6: Aspect Orientated Programming in Ruby

Your app now

PostsUsers

Authentication

Logging

Page 7: Aspect Orientated Programming in Ruby

With AOP

PostsUsers

Authentication

Logging

Aspects

Page 8: Aspect Orientated Programming in Ruby

What is AOP?

• A technique for modularising cross cutting concerns

Page 9: Aspect Orientated Programming in Ruby

Cross Cutting Concerns

• Profiling

• Persistence

• Transactions

• Access Control Lists

• Exception Handling

Page 10: Aspect Orientated Programming in Ruby

Scattered

• Code where a design concept is spread over a number of models

• one-to-n

• DRY?

Page 11: Aspect Orientated Programming in Ruby

Tangled

• Code which deals with multiple design concepts

• n-to-one mapping

• Hard to maintain?

Page 12: Aspect Orientated Programming in Ruby

The Real World

• Some requirements don’t always map nicely in OOP

• n-to-n

• scattered and tangled

Page 13: Aspect Orientated Programming in Ruby

Spaghetti Code

Page 14: Aspect Orientated Programming in Ruby

AOP to the rescue

• Aspects

• AOP is essentially a DSL built on top of the language

Page 15: Aspect Orientated Programming in Ruby

Modular

Page 16: Aspect Orientated Programming in Ruby

Wait! Ruby has Metaprogramming

Page 17: Aspect Orientated Programming in Ruby

So why use AOP in Ruby?

• AOP provides another level of abstraction

• Clear syntax for describing points in your application where to inject code

• Modularises behaviour

Page 18: Aspect Orientated Programming in Ruby

AOP in Ruby

• Aquarium is a Ruby AOP framework

• DSL for describing cross cutting behaviour with aspects

Page 19: Aspect Orientated Programming in Ruby

Terminology

• Aspect:

• Join Point - point in the program

• Pointcut - matches a number of join points

• Advice - behaviour evoked at pointcut

Page 20: Aspect Orientated Programming in Ruby

Aspect

Join point Join point

Pointcut

Page 21: Aspect Orientated Programming in Ruby

Result of matched aspect

Page 22: Aspect Orientated Programming in Ruby

Demo Time!

Page 23: Aspect Orientated Programming in Ruby

Ruby Advising Java(and visa versa)

• Latest release of Aquarium can run on JRuby

• Ruby aspects can advise Java

• Java aspects can advise Ruby code

Page 24: Aspect Orientated Programming in Ruby

Here Be Dragons

Page 25: Aspect Orientated Programming in Ruby

I’m going to use aspects for everything!

Page 26: Aspect Orientated Programming in Ruby

Don’t

Page 27: Aspect Orientated Programming in Ruby

Summary

• Aspects are cool

• Abstract and modularise cross cutting concerns

• Know when to use them!

Page 29: Aspect Orientated Programming in Ruby

Attribution

• Spaghetti pic - http://www.flickr.com/photos/cstein96/2344488576/in/set-72157604157601577/

• Dragon pic - Flight of the Conchords- Albi

• Squicken pic - humandescent.com

Page 30: Aspect Orientated Programming in Ruby

Q & A

Page 31: Aspect Orientated Programming in Ruby

Thanks!