Your SlideShare is downloading. ×
0
Moving away from
legacy code
with BDD
Who?
!
BDD Evangelist

!
BDD Practice Manager @Inviqa

!
Creator of Behat, Mink, PhpSpec2,
Prophecy

!
Contributor to Symf...
This talk is about
•

Solving purely technical “TCIAM” problem with agile
business analysis and discovery processes

•

Bu...
This talk is not about
•

Greenfield projects

•

Solutions for everyone

•

How to write code
This talk is not about
•

Greenfield projects

•

Maintenance-mode projects

•

Solutions for everyone

•

How to write co...
Legacy projects
How most developers see
their next project
My next
project
My next
project

His actual next
project
Agile, TDD, BDD,
General QA, etc…

// TODO: refactor this
later
Is it really that bad?
If the project can afford at least one full-time
specialist on a payroll that whines how horrible
this project is, then su...
// TODO: refactor this
later
// TODO: refactor this
later
// TODO: refactor this
later
This world is full of brilliant projects that nobody
wants to whine about. Sadly, it’s often simply
because there’s no one...
The truth is:
You deliver value!
Just not as effectively as you could
Agile, TDD, BDD,
General QA, etc…

// TODO: refactor this
later
How?
Three options
1. Rewrite an entire application using “the right way”
2. Do functional refactoring
3. Do business-oriented ...
#1: Full Rewrite
#1: Full Rewrite
•

Scrum / Kanban

•

TDD / BDD / DDD / Pair-programming

•

New everything

•

Mirroring functionality
#1: Income
6 Months later…
#1: Almost there…
#1: Full Rewrite

Just spaghetti, please: 4 man years

Full London meal (TDD, BDD, Agile, QA): ??? man years
#2: FUNCTIONAL
Refactoring
#2: Functional Refactoring
•

Blackbox testing

•

New routing

•

New templating system

•

Migration of model layer (MyS...
#2: Income
6 Months later…
#2: Ta-da!!!
“Exactly what did you do here?”

– Your client. (most likely)
#3: BDD PIPELINE
AKA Business-Oriented rewrite
Why do some legacy
projects suck?
Cost

Because of the
cost of change
Time
… Of change where?
Why do applications
change?
Welcome to the
wonderland
of Agile Business Analysis
Questionnaire
1. What is the goal and minimal valuable product?
2. What is the minimal set of features to support it?
3. W...
“BDD Pipeline”
1. Impact Mapping
2. Feature Mapping
3. Prioritisation
4. Example Workshop
5. Full-stack BDD
1. What is the Goal &
minimal valuable product?
Impact Mapping
“Impact mapping is a strategic planning technique
that prevents organisations from getting lost
while building products an...
Four levels of Impact Map
1. Why? are we doing all this (rewrite)? What is the
goal we’re trying to achieve?
2. Who? will ...
MVP
impactmapping.org
2. What is the minimal set
of features to support it?
Feature Mapping
“Feature mapping is a backlog grooming
technique. It is a graphical process which helps
teams in finding features that are...
Three levels of feature map
1. What? is the minimal marketable feature?
2. Who? will be impacted by this feature?
3. What?...
Product Backlog
In order to buy more products
As a customer
I need to have a product autocompletion in the search field
3. Which features are
more likely to change?
Prioritisation workshop
In order to maintain my shopping history
As a site visitor
I need to be able to register on this site
In order to maintain my shopping history
As a site visitor
I need to be able to register on this site

benefit
!
actor
SELECT s.*
FROM backlog as s
ORDER BY s.role, s.benefit
LIMIT 25
4. How fully those features
should be implemented?
Example workshops
Three layers of a User-Story
•

Business rule(s)

•

Communication

•

Acceptance criteria
Three layers of a User-Story

•

Business rule(s) == Acceptance criteria

•

Communication
Three layers of a User-Story

•

Business rule(s) == Acceptance criteria

•

Communication == Examples
Three layers of a User-Story

•

Business rule(s)

•

Communication == Examples == Acceptance criteria
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be able to register on this sit...
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be able to register on this sit...
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be able to register on this sit...
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be able to register on this sit...
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be able to register on this sit...
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be able to register on this sit...
5. How to avoid
gold plating?
Delivery
Architecture
Architecture
HTTP layering
[GET] /products

Infrastructure

Legacy system
[GET] /products
[GET] /products/123

Infrastructure

Legacy system
[GET] /products
[GET] /products/123

New system

Infrastructure

Legacy system
[GET] /products
[GET] /products/123

New system

Infrastructure

Legacy system

New system
Implementation
Implementation
Full-stack BDD
Stories
Examples

Describe

Design

Implement
Stories
Examples

Describe

Scenario-BDD
Design

Implement
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be able to register on this sit...
Feature:
in order
as a
i need

Scenario: Successful registration when visitor provides
Given I am on the
When
And I fill i...
assertEquals(Your Feature, Your App)
Setup
1. Dump your sprint features into text files
2. Put those text files into the `features/` folder inside
project
3. I...
Feature Context
<?php
!
use BehatBehatContextClosuredContextInterface,
BehatBehatContextTranslatedContextInterface,
BehatB...
First run
$> bin/behat
...
You can implement step definitions for undefined steps with these snippets:
!
/**
* @Then I sho...
Append snippets
$> bin/behat --append-snippets
Feedback loop
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be...
Stories
Examples

Describe

Scenario-BDD
Design

Implement
Colour it Red
Colour it red
/**
* @Given /^I am on the homepage$/
*/
public function iAmOnTheHomepage()
{
$crawler = new SomeCrawlerLibC...
Colour it red
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to be...
Stories
Examples

Describe

Scenario-BDD
Design

Implement
Change the message
As quickly as possible
Change the message
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need ...
Change the message
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need ...
Change the message
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need ...
Change the message
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need ...
behat.org
Change the message
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need ...
Stories

Spec-BDD

Examples

Describe

Design

Implement
Setup

1. Install phpspec via composer
2. use it
Stories

Spec-BDD

Examples

Describe

Design

Implement
Prepare your first spec
$> bin/phpspec desc Acme/Userbase/User
Describe your first message
<?php
!

namespace specAcmeUserbase;
!

class User extends ObjectBehavior

{
function it_is_re...
Colour it red
$> bin/phpspec
!
Class “AcmeUserbaseUser” does not exist. Create? [Y/n]
y
!
Method `AcmeUserbaseUser::isRegi...
Stories

Spec-BDD

Examples

Describe

Design

Implement
Stories

Spec-BDD

Examples

Describe

Design

Implement
Stories

Spec-BDD

Examples

Describe

Design

Implement
phpspec.net
Verify feature
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to b...
Verify feature
$> bin/behat
!
Feature: registration
in order to maintain my shopping history
as a site visitor
i need to b...
Stories
Examples

Describe

Design

Implement
#3: Income
6 months later…
#3: Same ashtrays, better car
We do that for clients
http://

.com

We do that for clients
And teach others in those rare
moments when we don’t
Moving away from legacy code with BDD
Moving away from legacy code with BDD
Moving away from legacy code with BDD
Moving away from legacy code with BDD
Moving away from legacy code with BDD
Upcoming SlideShare
Loading in...5
×

Moving away from legacy code with BDD

9,540

Published on

Greenfield projects are awesome – you can develop highest quality application using best practices on the market. But what if your bread actually is Legacy projects? Does it mean that you need to descend into darkness of QA absence? This talk will show you how to be successful even with the oldest legacy projects out there through the introduction of Agile processes and tools like Behat.

Published in: Technology

Transcript of "Moving away from legacy code with BDD"

  1. 1. Moving away from legacy code with BDD
  2. 2. Who? ! BDD Evangelist ! BDD Practice Manager @Inviqa ! Creator of Behat, Mink, PhpSpec2, Prophecy ! Contributor to Symfony2, Composer ! Host of the “Elephant in the Room” podcast
  3. 3. This talk is about • Solving purely technical “TCIAM” problem with agile business analysis and discovery processes • Building a delivery strategy on the idea of constant change • Real-life experience
  4. 4. This talk is not about • Greenfield projects • Solutions for everyone • How to write code
  5. 5. This talk is not about • Greenfield projects • Maintenance-mode projects • Solutions for everyone • How to write code (well, mostly)
  6. 6. Legacy projects
  7. 7. How most developers see their next project
  8. 8. My next project
  9. 9. My next project His actual next project
  10. 10. Agile, TDD, BDD, General QA, etc… // TODO: refactor this later
  11. 11. Is it really that bad?
  12. 12. If the project can afford at least one full-time specialist on a payroll that whines how horrible this project is, then surely it did something right.
  13. 13. // TODO: refactor this later
  14. 14. // TODO: refactor this later
  15. 15. // TODO: refactor this later
  16. 16. This world is full of brilliant projects that nobody wants to whine about. Sadly, it’s often simply because there’s no one left to pay for that.
  17. 17. The truth is: You deliver value! Just not as effectively as you could
  18. 18. Agile, TDD, BDD, General QA, etc… // TODO: refactor this later
  19. 19. How?
  20. 20. Three options 1. Rewrite an entire application using “the right way” 2. Do functional refactoring 3. Do business-oriented rewrite using “BDD pipeline”
  21. 21. #1: Full Rewrite
  22. 22. #1: Full Rewrite • Scrum / Kanban • TDD / BDD / DDD / Pair-programming • New everything • Mirroring functionality
  23. 23. #1: Income
  24. 24. 6 Months later…
  25. 25. #1: Almost there…
  26. 26. #1: Full Rewrite Just spaghetti, please: 4 man years Full London meal (TDD, BDD, Agile, QA): ??? man years
  27. 27. #2: FUNCTIONAL Refactoring
  28. 28. #2: Functional Refactoring • Blackbox testing • New routing • New templating system • Migration of model layer (MySQL -> Mongo)
  29. 29. #2: Income
  30. 30. 6 Months later…
  31. 31. #2: Ta-da!!!
  32. 32. “Exactly what did you do here?” – Your client. (most likely)
  33. 33. #3: BDD PIPELINE AKA Business-Oriented rewrite
  34. 34. Why do some legacy projects suck?
  35. 35. Cost Because of the cost of change Time
  36. 36. … Of change where?
  37. 37. Why do applications change?
  38. 38. Welcome to the wonderland of Agile Business Analysis
  39. 39. Questionnaire 1. What is the goal and minimal valuable product? 2. What is the minimal set of features to support it? 3. Which features are more likely to change? 4. How fully those features should be implemented? 5. How to avoid gold plating?
  40. 40. “BDD Pipeline” 1. Impact Mapping 2. Feature Mapping 3. Prioritisation 4. Example Workshop 5. Full-stack BDD
  41. 41. 1. What is the Goal & minimal valuable product? Impact Mapping
  42. 42. “Impact mapping is a strategic planning technique that prevents organisations from getting lost while building products and delivering projects, by clearly communicating assumptions, helping teams align their activities with overall business objectives and make better roadmap decisions.” – Gojko Adzic
  43. 43. Four levels of Impact Map 1. Why? are we doing all this (rewrite)? What is the goal we’re trying to achieve? 2. Who? will be impacted by it? 3. How? can they help us to achieve the goal? 4. What? can we do to support them?
  44. 44. MVP
  45. 45. impactmapping.org
  46. 46. 2. What is the minimal set of features to support it? Feature Mapping
  47. 47. “Feature mapping is a backlog grooming technique. It is a graphical process which helps teams in finding features that are necessary to support discovered MVP” – Marcello Duarte
  48. 48. Three levels of feature map 1. What? is the minimal marketable feature? 2. Who? will be impacted by this feature? 3. What? particular parts of this feature do they need to create this impact?
  49. 49. Product Backlog
  50. 50. In order to buy more products As a customer I need to have a product autocompletion in the search field
  51. 51. 3. Which features are more likely to change? Prioritisation workshop
  52. 52. In order to maintain my shopping history As a site visitor I need to be able to register on this site
  53. 53. In order to maintain my shopping history As a site visitor I need to be able to register on this site benefit ! actor
  54. 54. SELECT s.* FROM backlog as s ORDER BY s.role, s.benefit LIMIT 25
  55. 55. 4. How fully those features should be implemented? Example workshops
  56. 56. Three layers of a User-Story • Business rule(s) • Communication • Acceptance criteria
  57. 57. Three layers of a User-Story • Business rule(s) == Acceptance criteria • Communication
  58. 58. Three layers of a User-Story • Business rule(s) == Acceptance criteria • Communication == Examples
  59. 59. Three layers of a User-Story • Business rule(s) • Communication == Examples == Acceptance criteria
  60. 60. Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site
  61. 61. Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site Scenario: Successful registration when visitor provides all the required info
  62. 62. Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site Scenario: Successful registration when visitor provides all the required info Scenario: Unable to register when visitor misses required info
  63. 63. Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site Scenario: Successful registration when visitor provides all the required info Scenario: Unable to register when visitor misses required info Scenario: ...
  64. 64. Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site Scenario: Successful registration when visitor provides all the required info Scenario: Unable to register when visitor misses required info Scenario: ... Scenario: ... Scenario: ... Scenario: ... Scenario: ...
  65. 65. Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site Scenario: Successful registration when visitor provides all the required info Given I am on the homepage When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  66. 66. 5. How to avoid gold plating? Delivery
  67. 67. Architecture
  68. 68. Architecture HTTP layering
  69. 69. [GET] /products Infrastructure Legacy system
  70. 70. [GET] /products [GET] /products/123 Infrastructure Legacy system
  71. 71. [GET] /products [GET] /products/123 New system Infrastructure Legacy system
  72. 72. [GET] /products [GET] /products/123 New system Infrastructure Legacy system New system
  73. 73. Implementation
  74. 74. Implementation Full-stack BDD
  75. 75. Stories Examples Describe Design Implement
  76. 76. Stories Examples Describe Scenario-BDD Design Implement
  77. 77. Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site Scenario: Successful registration when visitor provides all the required info Given I am on the homepage When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  78. 78. Feature: in order as a i need Scenario: Successful registration when visitor provides Given I am on the When And I fill in And I submit it Then And I should be on the homepage What could be automated should be automated
  79. 79. assertEquals(Your Feature, Your App)
  80. 80. Setup 1. Dump your sprint features into text files 2. Put those text files into the `features/` folder inside project 3. Install behat (via composer or behat.phar) 4. Initialize behat test suite by running `bin/behat —init`
  81. 81. Feature Context <?php ! use BehatBehatContextClosuredContextInterface, BehatBehatContextTranslatedContextInterface, BehatBehatContextBehatContext, BehatBehatExceptionPendingException; use BehatGherkinNodePyStringNode, BehatGherkinNodeTableNode; ! class FeatureContext extends BehatContext { }
  82. 82. First run $> bin/behat ... You can implement step definitions for undefined steps with these snippets: ! /** * @Then I should see :arg1 */ public function iShouldSee($arg1) { throw new PendingException(); } ...
  83. 83. Append snippets $> bin/behat --append-snippets
  84. 84. Feedback loop $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage TODO: write pending definition When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  85. 85. Stories Examples Describe Scenario-BDD Design Implement
  86. 86. Colour it Red
  87. 87. Colour it red /** * @Given /^I am on the homepage$/ */ public function iAmOnTheHomepage() { $crawler = new SomeCrawlerLibCrawler(); $crawler->goto(“http://localhost:8080/”); if (200 !== $crawler->getCurrentStatusCode()) { throw new RuntimeException(‘Can not open homepage’); } }
  88. 88. Colour it red $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage Can not open homepage (RuntimeException) When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  89. 89. Stories Examples Describe Scenario-BDD Design Implement
  90. 90. Change the message As quickly as possible
  91. 91. Change the message $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage Can not open homepage (RuntimeException) When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  92. 92. Change the message $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage Route … not found (FrameworkException) When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  93. 93. Change the message $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage Template … not found (FrameworkException) When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  94. 94. Change the message $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage When I follow “sign up” TODO: write pending definition And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  95. 95. behat.org
  96. 96. Change the message $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered Object `User` and its method `isRegistered` does not exist (RuntimeException) And I should be on the homepage again
  97. 97. Stories Spec-BDD Examples Describe Design Implement
  98. 98. Setup 1. Install phpspec via composer 2. use it
  99. 99. Stories Spec-BDD Examples Describe Design Implement
  100. 100. Prepare your first spec $> bin/phpspec desc Acme/Userbase/User
  101. 101. Describe your first message <?php ! namespace specAcmeUserbase; ! class User extends ObjectBehavior
 { function it_is_registered_by_default() { $this->shouldBeRegistered(); } }
  102. 102. Colour it red $> bin/phpspec ! Class “AcmeUserbaseUser” does not exist. Create? [Y/n] y ! Method `AcmeUserbaseUser::isRegistered()` does not exist. Create? [Y/n] y
  103. 103. Stories Spec-BDD Examples Describe Design Implement
  104. 104. Stories Spec-BDD Examples Describe Design Implement
  105. 105. Stories Spec-BDD Examples Describe Design Implement
  106. 106. phpspec.net
  107. 107. Verify feature $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  108. 108. Verify feature $> bin/behat ! Feature: registration in order to maintain my shopping history as a site visitor i need to be able to register on this site ! Scenario: Successful registration when visitor provides all the required info Given I am on the homepage When I follow “sign up” And I fill in registration form And I submit it Then I should be successfully registered And I should be on the homepage again
  109. 109. Stories Examples Describe Design Implement
  110. 110. #3: Income
  111. 111. 6 months later…
  112. 112. #3: Same ashtrays, better car
  113. 113. We do that for clients
  114. 114. http:// .com We do that for clients And teach others in those rare moments when we don’t
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×