refactor your specs! Øredev 2013

Post on 20-Jun-2015

1.098 Views

Category:

Economy & Finance

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Watch the video online: http://vimeo.com/79181633 Even in an agile world, specifications often go too far and describe solutions with too much details; all these premature decisions constraint the implementation and remove opportunities. There is a remedy: refactoring the specs, even before refactoring the code. In the TDD cycle, refactoring is the art of restructuring the code to make it simpler, without changing its behavior at runtime. A key part of refactoring is to recognize and extract duplications. Refactoring is very useful at the code level, and it is even more powerful when applied during business analysis or functional architecture. We will show how the practice of refactoring directly "at the business domain level" can simplify the problem, and therefore the resulting implementation code, by orders of magnitude. This means much less code to write, to test and to maintain, and much less defects as a result. We will introduce 5 patterns on how to refactor at the business-domain level, such as "Make It Systematic" and "Degenerate Case". We will also explain some limits and the required mindset. This approach of refactoring has been used on several real-world projects and is derived in particular from DDD and from Specification by Example.

TRANSCRIPT

Refactor your

Specs!Cyrille Martraire - @cyriux -

what if specs could be improved?

Specs

Specs

Specs

Passionate developer

PARISSince 1999

@cyriux

Paris Software Craftsmanship Community

http://www.meetup.com/paris-software-craftsmanship/

TDDBDDDDDLegacy

Software Craftsmanship

"Raising the bar"

"Working code is not enough"

Software Craftsmanship

What about the way we

specify?

Once upon a time...

A project...

We Want:

Financial Calculations

Here are:

The formulas

Case 1

Code It!

(with BDD)Given a floating r

ate bond on EURIBOR 3M

And a nominal of 15M EUR

And an issue date of 2011/06/15

And an end date of 2012/06/14

And an SEMI_ANNUAL calculation perio

d

When the EURIBOR 3M evolves:

| 2011/09/15 | 3.5% |

| 2012/03/15 | 4.0% |

Then the cash-flows are:

| 2011/12/13 | 23000 |

| 2012/06/14 | 25500 |

DONEdouble sum = 0;for (CashFlow cf : cashflows){ double df = 1 + rate * t; sum += cf.getAmount() / df;}return sum;

Case 2

DONEdouble sum = 0;for (CashFlow cf : cashflows){ double df = pow(1 + rate), t); sum += cf.getAmount() / df;}return sum;

double sum = 0;for (CashFlow cf : cashflows){ double df = pow(1 + rate), t); sum += cf.getAmount() / df;}return sum;

double sum = 0;for (CashFlow cf : cashflows){ double df = 1 + rate * t; sum += cf.getAmount() / df;}return sum;

Spot the difference

double sum = 0;for (CashFlow cf : cashflows){ double df = pow(1 + rate), t); sum += cf.getAmount() / df;}return sum;

double sum = 0;for (CashFlow cf : cashflows){ double df = 1 + rate * t; sum += cf.getAmount() / df;}return sum;

Spot the difference

Duplication?

TDD-ish

http://agileinaflash.blogspot.fr/2009/02/red-green-refactor.html

Remove duplication

Refactor!

pow(1 + rate), t)

double sum = 0;for (CashFlow cf : cashflows){ double df = sum += cf.getAmount() / df;}return sum;

Cases 1 or 2

1 + rate * t

Cases 1 or 2

What business sense?What name?

Ask the experts

Oh, I see what you mean...

This is the ‘convention for

interests calculation’

Thx, we can grow our

Ubiquituous Language now!

Cool.

...

Next

...

?

What’s its name?

We call that the ‘coupon’, or just

‘cashflow’

Thx!

...

Cashflows

Putting it all together

One formulaMany possibilities

...

...

Combinations

...

cashflow ...IR convention

Combinations

Combinations

Combinations

Discover Domainin-depth

RibbonCostume

SystematicRibbon

Costume+=

Cute kitty

A model of the domain

Combinations

pattern 1

SystematicCombination

large variety from a few primitives

4ribbons

+4costumes

4+4 -> 4*4

4+4 -> 4*4

4+4 -> 4*4Less codeLess bugsYou win!

Changes in one place

Testing

HarderEasy

Technically:

- Strategy pattern applied several times- A (rigid) domain-specific language

Suggestion by combination

Suggestion by combination

Predictive model

Extrapolate

Suggestion by combination

ImpossibleIt’s valid

Yes, great idea!

Suggestion by combination

Challenge

the model

Break the model[Chris Matts]

examples -> model -> more examples

Beware!Don’t go too

genericDon’t be

smarter than business

peopleMust make business sense (or just a little bit...)

Not just for fun

And now for some more patterns...

pattern 2

Different, but not really

same purpose, same thing

1. Concrete examples2. visualize3. Recognize refactoring

opportunities

Plain Text"On each anniversary date, the bond pays a coupon""At the end of each period, the swap pays a cashflow"

Recognize opportunities

"On each anniversary date, the bond pays a coupon"At the end of each period, the swap pays a cashflow"

Same scenario!?

More visuallybond

swap

time

time

$

$

time

time

$

$

Equivalentbond

swap

For a fund manager

time

time

$

$

Same purposebond

swap

Same scenario!

Tip: Focus on one context

=

≠≠

Systematic in this context

=

≠≠

Legal

MoneyManagement

Systematic in this context

=≠

Transport Regulation

Cargo Shipping

Systematic in this context

=≠

Transport Regulation

Cargo Shipping

DDD Bounded Contexts

pattern 3

Degenerate Case

Nothing is just a special case of something

Tax calculation

VAT

VAT on net price

Not Applicable

Calculation

?

Introduce Special CaseDo-Nothing is a special case of

calculation

SystematicVAT

VAT on net price

Not Applicable

Calculation

Technically:

- Null Object pattern (Fowler)

pattern 3 bis

Optional Element

Systematic thanks to an optional element

WorkflowsCredit score OK?

Compute Credit Limit

References Check

Record Outcome

UK/US credit application

France credit application Credit

score OK?

Compute Credit Limit

Record Outcome

Recognize opportunity

Credit score OK?

Compute Credit Limit

References Check

Record Outcome

UK/US credit application

France credit application Credit

score OK?

Compute Credit Limit

References Check

Record Outcome

Credit score OK?

Compute Credit Limit

References Check

Record Outcome

credit application []Optional step

Systematic

pattern 3 ter

Manual as special case of automatic

Systematic thanks to a alternate element

WorkflowsCredit score OK?

Compute Credit Limit

References Check

Record Outcome

Automatic credit application

Manual credit application

Compute Credit Limit

References Check

Record Outcome

Compute Credit Limit

References Check

Record Outcome

Manual step

Systematic

[ ]

pattern 4

Not my business

Systematic by moving non-intrinsic logic away

Toys inventory50x

100x

25x

EUR 1.50

USD 3.25

MUR 5.70

Total value?

"Depends on the product"

EURnon-EUR

MUR, IQD...

qty * price

qty * price * fx rate

qty * price * fx rate * fx rate

ccy/EUR

ccy/USD

USD/EUR

"Depends on the product"

product

productexcept MUR, IQD...

product

EURnon-EUR

MUR, IQD...

qty * price

qty * price * fx rate

qty * price * fx rate * fx rate

ccy/EUR

ccy/USD

USD/EUR

"Depends on the product"

product

productexcept MUR, IQD...

product

product

currency

EURnon-EUR

HKD, ZAR...

qty * price

"Depends on the currency"

product

productexcept HKD, ZAR...

product

* currency conversion

qty * price * currency conversion

Systematic

(See Currency

Conversions business

rules elsewhere)

pattern 5

Principlesover rules

Systematic at the underlying principle level

Try it yourself

http://codekata.pragprog.com/

Supermarket pricing

Code Kata

$0.65

3 for $1 (what if I buy 4, or 5?)

$1.99/pound (cost for 4 ounces?)

$0.65

$0.651 2 3 4

$0.65 $1.00

$1.65 +

suggest buy 6

$1.30 +

suggest buy 3

$1.30

Pricing by quantitydecision tree

$0.65Principle:

"What’s best for the

customer"

$0.65Principle:

"What’s best for the

customer"Could be implemented literally...

Technically:

- Declarative code (brute force)- Rules engine...

each pattern =

opportunity to simplify

each pattern =

opportunity of simplification

missed

Traditionally

SpecificationsDescribe the system we

want to build

SpecificationsOnce built,

specs become documentation

SpecificationsOnce built,

specs become documentation

obsolete

Now we know better...

User StoriesAs a new visitorI want to registerIn order to receive spam

points: 1015

ConversationsI want this button

more to the left

...

BDD scenariosGiven I’m logged inWhen I click the Logoff buttonThen I’m logged off

Perfect for many cases

Richer domain,

more specs!

Equations

Rules

Taxes shall not be

changed retroactively

State machines

Workflows

Common situation

+more details

Given I’m logged inWhen I click the Logoff buttonThen I’m logged off

Often far from

perfect

Often far from

perfect

Too much upfront

One-way

Boring& Repetitive

So How to

Really improve this specs thing?

Refactor your specs

Refactor your specs

the way you think about

The 3 amigos

Just tell me what to do

No, we need your skills to simplify and suggest

abstractions

TDD, refactoring, patterns...

Just gimme examples, I’ll

reverse it all in TDD

No, we need a true business perspective

DDD Ubiquitous Language

I can only help once it’s

done

No, you’re even more useful along the road, with your skills

on challenging everything

3 amigos & specification workshops

Tester in India?

2+1 amigos

IRL LOL

I need specs to derive test

cases

Why not collaborate during the workshops? We’ll do the test cases

at the same time

Specifications by Example

Why not collaborate during the workshops? We’ll do the test cases

at the same time

Specifications by Example

scenarios

EmailExcel

BDD toolWhatever

My job is to solve problems &

tell what to do

"90% of timeBA’s bring solutions"

- former head of BA’s

Specs as "Amateur Design"

Your job is to challenge the problem in-depth

Feature Injection, Impact Mapping

If I don’t write a document I’m

useless!

Relax! The document is always late to the party

Real work is happening informally, while we talk.

And you also have to facilitate and explain the business

domain to everyone

Train the team

new collaboration

Specs -> Conversationsearly and frequent

between all 3 amigos

Specs -> conversationsearly and frequent

But what about

documentation then?

Living documentation[Gojko Adzic]

ExamplesIntent

(Free comments)

Living documentation

Intent

Living documentation

Concrete examples

Living documentationFree comments

Living documentation

Visible Workings [Brian Marick]

Custom DocletRuntime exportLaTex export...

- Annotate domain-relevant classes

- Custom Doclet to export Excel-formatted glossary of every domain concept

Source code as reference

Source code as referenceSent directly to end customers

Runtime exports

Runtime exports

\sum_{i=0}^{n}\frac{ }{ }

1+rt(1+r)^{t}

(1+r)^{\frac{t_1 - t_0}{360}}1+r.\frac{t_1 - t_0}{365}

Think critically

Challenge things

Refactor the code

Refactor at the domain level too

Exploit opportunitiesAs long as it’s in line with

the business domain

- Introduce Just-In-Time specification workshops with the 3 amigos

- Consider Living Documentation

What about you?

Questions?Feedback?

@cyriux

Merci

top related