TEST DRIVEN
  DESIGN
 AGILE NCR - APRIL 25, 2011
        Jonas Auken




                              1
Introduction
• Name: Jonas Auken
• Role: Agile Evangelist
• Primary goal: Excellent quality software
• Secondary goal: Happy developers
• Means: Agile practices everywhere

                                             2
Important questions
• How do you know if you're code is
  working?
  When you have tested it!
• When do you want feedback from your
  code?
  Now!
• When would you like to refactor?
  Anytime!


                                        3
Agenda
• Test driven development
• The “Find my Ride” example
• Immediate feedback
• Comfortable refactoring
• Towards a better design
• Discussion: How do we get started?
                                       4
@Test public void canInspirePeopleToDoTDD() {
  agileNCR.arrangeConference(2011);
  People people = agileNCR.invitePeople();
  Presenter presenter = new Presenter(“Jonas Auken”);
  presenter.prepareSlides();
  presenter.createExampleCode();
  while (!done) {
     try {
        done = presenter.performPresentation(people);
     } catch (QuestionRaisedException e) {
        presenter.answerQuestion();
     }
  }
  assertEquals(0.75, people.getRateInspiredToDoTDD);
}



                                                        5
Test driven development
•   Why?
     •     Feedback loop
     •     Change alarm
     •     Help in bug finding
     •     A better design
     •     Adding functionality by the slice
•   How and when?
     •     Write the test first
     •     Mock all external objects
     •     Only test worker objects, not value objects




                                                         6
Good design
• “Pretty much what you’d have expected”
• No superfluous code
• Easy to understand - easy to maintain
• Small classes, small methods
• Separation of concern - do one thing only
• Single layer of abstraction
                                              7
Test driven design
• The natural next step
• Avoid big design up front (BDUF)
• Design by refactoring
   • Design what you need
   • Mock the rest
   • Small steps or large steps
                                     8
“Find my Ride”

• A larger example
• Web application
• A few complex issues
• Java6, JSF 2, Tomcat server, mock database

                                               9
This is not real
• No users telling us what they want
• No component and backend design
• No planning
• So, this is not really agile - it’s just an
   example of TDD



                                                10
Immediate feedback

• The red-green cycle
• Test, code and refactor
• Feedback in every cycle
• Short cycle or long cycle

                              11
Comfortable refactoring

• Refactoring in every cycle
• Avoid the “big refactoring in the sky”
• Refactor when the test is green


                                           12
Towards a better design
• Walking skeleton
• One slice of functionality
• Hardest thing first
• Knowledge is gathered
• Choices can be deferred
• Better estimates and better design
                                       13
How do we get started?
• Inspiration?
• Discussion?
• Questions?
• Remarks?
• Personal experiences?

                          14
Books & Links
•   Growing Object-Oriented Software Guided by Tests - Steve Freeman & Nat Pryce
•   Test Driven Development: By Example - Kent Beck
•   Extreme Programming Explained: Embrace Change - Kent Beck
•   Clean Code - Robert C Martin
•   Refactoring: Improving the Design of Existing Code - Martin Fowler
•   10 Ways to Improve Your Code - http://www.slideshare.net/guestebde/10-ways-to-improve-
    your-code-neal-ford - Neal Ford
•   SLAP - http://www.markhneedham.com/blog/2009/06/12/coding-single-level-of-
    abstraction-principle - Mark Needham
•   Do One Thing - http://blog.objectmentor.com/articles/2009/09/11/one-thing-extract-till-
    you-drop - Robert C Martin
•   Test Driven Development - http://en.wikipedia.org/wiki/Test-driven_development -
    Wikipedia
•   Tech & Talk - www.auken.net - Jonas Auken




                                                                                              15

Test Driven Design by Jonas Auken

  • 1.
    TEST DRIVEN DESIGN AGILE NCR - APRIL 25, 2011 Jonas Auken 1
  • 2.
    Introduction • Name: JonasAuken • Role: Agile Evangelist • Primary goal: Excellent quality software • Secondary goal: Happy developers • Means: Agile practices everywhere 2
  • 3.
    Important questions • Howdo you know if you're code is working? When you have tested it! • When do you want feedback from your code? Now! • When would you like to refactor? Anytime! 3
  • 4.
    Agenda • Test drivendevelopment • The “Find my Ride” example • Immediate feedback • Comfortable refactoring • Towards a better design • Discussion: How do we get started? 4
  • 5.
    @Test public voidcanInspirePeopleToDoTDD() { agileNCR.arrangeConference(2011); People people = agileNCR.invitePeople(); Presenter presenter = new Presenter(“Jonas Auken”); presenter.prepareSlides(); presenter.createExampleCode(); while (!done) { try { done = presenter.performPresentation(people); } catch (QuestionRaisedException e) { presenter.answerQuestion(); } } assertEquals(0.75, people.getRateInspiredToDoTDD); } 5
  • 6.
    Test driven development • Why? • Feedback loop • Change alarm • Help in bug finding • A better design • Adding functionality by the slice • How and when? • Write the test first • Mock all external objects • Only test worker objects, not value objects 6
  • 7.
    Good design • “Prettymuch what you’d have expected” • No superfluous code • Easy to understand - easy to maintain • Small classes, small methods • Separation of concern - do one thing only • Single layer of abstraction 7
  • 8.
    Test driven design •The natural next step • Avoid big design up front (BDUF) • Design by refactoring • Design what you need • Mock the rest • Small steps or large steps 8
  • 9.
    “Find my Ride” •A larger example • Web application • A few complex issues • Java6, JSF 2, Tomcat server, mock database 9
  • 10.
    This is notreal • No users telling us what they want • No component and backend design • No planning • So, this is not really agile - it’s just an example of TDD 10
  • 11.
    Immediate feedback • Thered-green cycle • Test, code and refactor • Feedback in every cycle • Short cycle or long cycle 11
  • 12.
    Comfortable refactoring • Refactoringin every cycle • Avoid the “big refactoring in the sky” • Refactor when the test is green 12
  • 13.
    Towards a betterdesign • Walking skeleton • One slice of functionality • Hardest thing first • Knowledge is gathered • Choices can be deferred • Better estimates and better design 13
  • 14.
    How do weget started? • Inspiration? • Discussion? • Questions? • Remarks? • Personal experiences? 14
  • 15.
    Books & Links • Growing Object-Oriented Software Guided by Tests - Steve Freeman & Nat Pryce • Test Driven Development: By Example - Kent Beck • Extreme Programming Explained: Embrace Change - Kent Beck • Clean Code - Robert C Martin • Refactoring: Improving the Design of Existing Code - Martin Fowler • 10 Ways to Improve Your Code - http://www.slideshare.net/guestebde/10-ways-to-improve- your-code-neal-ford - Neal Ford • SLAP - http://www.markhneedham.com/blog/2009/06/12/coding-single-level-of- abstraction-principle - Mark Needham • Do One Thing - http://blog.objectmentor.com/articles/2009/09/11/one-thing-extract-till- you-drop - Robert C Martin • Test Driven Development - http://en.wikipedia.org/wiki/Test-driven_development - Wikipedia • Tech & Talk - www.auken.net - Jonas Auken 15