Refactoring to use-cases

A case study



Przemysław::Kowalczyk
a.k.a. szeryf

about me

Table of contents

  1. The traditional way

    MVC considered harmful?!

  2. The new way

    DCI, Objects on Rails, Use-cases...

  3. Case-study

    Code or it didn't happen...

the traditional way

MVC
considered harmful?

Rails made me do it

Jamis Buck, 2006

obese model,
not so skinny controller

there's more than Model, View & Controller

MVC makes use-cases difficult to read

use-case spread over many files

shotgun surgery

lack of cohesion

Single Responsibility Principle

class has exactly one reason to change

Robert C. Martin, Agile Software Development, Principles, Patterns, and Practices

AR::Base already has a responsibility

OOP makes use-cases difficult to read

Classes dictate methods of objects

A new hope

Data
Context
Interaction

Data

Context

Interaction

DCI in example

Architecture
The Lost Years

by Uncle Bob Martin

http://www.youtube.com/watch?v=WpkDN78P884

use-cases are your application,
Rails isn't

Architecture according to Uncle Bob

Architecture according to Uncle Bob

Good architecture allows to defer decisions

...maximizes number of decisions not made

It's Business Time by Jim Gay

Presented at wroc_love.rb: http://www.youtube.com/watch?v=lhFSc0dWsto

Objects on Rails

a must-read! => objectsonrails.com

A new hope

This will be mainstream in 5 years

(except Java)

examples

caveat emptor:
work in progress

before

role.rb

Single Responsibility Principle?

§1 Role is sending notifications!

§2 Role is producing HTML!

§3 Role has a state machine

§4 Role creates activity items

§5 Role creates roles

§6 Role has security holes

§7 Role has backdoors

after

Project Membership Request

Project Membership Acceptance

Plan Change

how to use?

Roles Controller

Role

helpers

Creates Activity Items

Uses Transaction

Dependency Injection

how to test?

Project Membership Acceptance Test

Test Helper

Y U NO rake test?

lib/tasks/tests.rake

Y U NO autotest?

~/.autotest

pros

use-cases easy to follow

easier to modify

easier to reason about

dependencies under control

crazy fast tests

units

$ time rake test:units
# Running tests:

..................................................................................................................................................................

Finished tests in 33.363820s, 4.8855 tests/s, 8.5422 assertions/s.

163 tests, 285 assertions, 0 failures, 0 errors

real  0m51.735s
user  0m43.616s
sys   0m2.795s

units

$ time rake test:units
# Running tests:

..................................................................................................................................................................

Finished tests in 33.363820s, 4.8855 tests/s, 8.5422 assertions/s.

163 tests, 285 assertions, 0 failures, 0 errors

real  0m51.735s
user  0m43.616s
sys   0m2.795s

use cases

$ time rake test:usecases
# Running tests:

................................................

Finished tests in 0.081814s, 586.6967 tests/s, 1821.2042 assertions/s.

48 tests, 149 assertions, 0 failures, 0 errors, 0 skips

real  0m6.375s
user  0m5.115s
sys   0m1.175s
    

use cases

$ time rake test:usecases
# Running tests:

................................................

Finished tests in 0.081814s, 586.6967 tests/s, 1821.2042 assertions/s.

48 tests, 149 assertions, 0 failures, 0 errors, 0 skips

real  0m6.375s
user  0m5.115s
sys   0m1.175s
    

easy to test all scenarios

cons

no Rails goodies

tedious requires

brittle tests?

Thank you!

Where do I find...

/