Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s)....

29
presented by Bill Wake & Steve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia, Canada. 2004 ACM 04/0010

Transcript of Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s)....

Page 1: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

presented by

Bill Wake & Steve Metsker

OOPSLA 2004Copyright is held by the author/owner(s).OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia, Canada.2004 ACM  04/0010

Page 2: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

2OOPSLA 2004

Premiseo You want to learn:

~test-driven development

o you are hip to learning by doing

o You have access to a Development environment

~for either Java, Visual Basic, or C#

Page 3: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

3OOPSLA 2004

Why TDD?o Test-Driven Development

~Gets you focused on developing exactly what you need

~Produces a very tight linkage between requirements, testing, and development

~Guarantees that when development is done, unit-testing is also done

Page 4: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

4OOPSLA 2004

And Refactoring?

o Internally your code can be in terrible shape and yet look completely healthy externally

o Refactoring

~Improves the health of code without altering its function

~Makes maintenance easier

~Tends to lead to simple, clean design

Page 5: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

5OOPSLA 2004

Plan for Today!o Test-First

Development

~ tdd flow chart

~ Introducing a problem domain

~ introducing x-unit, and Getting NUnit/JUnit running on your machine

~ Writing lots of tests -- and making them pass

o Cooldown

o Refactoring

~ What it is

~ Some Important techniques

~ Clean up the Goal System

~Long Method

~Long Method (again!)

~Primitive Obsession

~Feature Envy

o Cooldown

Page 6: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

6OOPSLA 2004

Test-Driven!

Choose task N

y

YWrite a new,

small test

Implement just enough so that the test will fail

n implement just enough so that

tests should pass

Refactor

Done?

green?code

smells?

n

y

run tests

Page 7: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

7OOPSLA 2004

The Domaino A Goal management system

~lets you

~establish goals

~monitor progress against goals

?Goal Actual

Page 8: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

8OOPSLA 2004

xUnito Free(!) software from www.Nunit.org

and www.junit.org o originally developed as JUnit by kent

beck and erich gammao a framework for writing and running

tests~an Assertion class to use, to pick up Assert() and other methods

~Attributes, that mark methods as test methods

~a gui

o And now, A wee demo:

Page 9: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

9OOPSLA 2004

Go!o compile testgoal.vb, TestGoal.cs, or

TestGoal.java

o run testgoal

~get a red bar

~w00t

o Implement just enough (in Goal.vb/cs/java and Actual.vb/cs/java) to get a green bar

~Really: just enough!

Page 10: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

10OOPSLA 2004

Stoke it up!o Get 7 tests red-then-green to earn a

prize! o yer the customer, but… here are some

good tests ~multiple actuals applied to a goal

~do not meet goal if sum of actuals < goal~do meet goal if sum of actuals >= goal~no negative times (or zero) on actuals or goals

~goals with temporal element~actuals after goal datetime do not apply to goal

~goal is met by actuals that do not exceed the goal

~sum of actuals <= goal~as in spending

Page 11: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

11OOPSLA 2004

More Tests!o elaborate

~ think like a unit tester~ cover any case you have missed

o more features ~ monitor goals other than minutes applied

~ like, run a mile in 5 minutes~or run so many miles

~ goals with dependencies~ milestone-y goals

~ like, meet 5 times before Christmas

~ add notes~ horseshoe goals (being close counts)~ checkbook goals

Page 12: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

12OOPSLA 2004

Cooldowno Debrief

~One surprising thing about tdd?

o Could you use this at work?

o what about the internal health of the code? Does it matter? Can we improve it?

Page 13: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,
Page 14: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

14OOPSLA 2004

Refactoringo In practice, TDD applies

refactoring consistently

o Refactoring~a transformation that improves

code but doesn’t change its effect

o Smell~a potential problem in code

o Experience?

Page 15: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

15OOPSLA 2004

A Few refactorings

o Refactorings that Address common smells:

~Long Method

~Primitive Obsession

~Feature Envy

o Check out Martin Fowler’s book "Refactoring" for lots of smells and refactorings

Page 16: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

16OOPSLA 2004

The Domaino A Goal vs. Actual tracking

system

o Enhanced to count actuals

~if they occur before the goal

~if they contain one of the goal's keywords

Page 17: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

17OOPSLA 2004

Example: Long Method

Imports System.Text.RegularExpressionsPublic Class Goal ... Public Function met() As Boolean

Dim keys As New ArrayList Dim keyword As String For Each keyword In Regex.Split(sKeywords, ",") keys.Add(keyword.Trim()) Next keyword ... End FunctionEnd Class

Page 18: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

18OOPSLA 2004

Public Function met() As Boolean ... Dim a As Actual For Each a In actuals If dtDeadline.CompareTo(a.WhenOccurred) >= 0 Then Dim k As String For Each k In keys Dim desc As String desc = a.Description desc = desc.ToLower

Dim lowK As String lowK = k.ToLower()

Dim i As Integer i = desc.IndexOf(lowK)

If i >= 0 Then fits = True End If Next End If If fits Then sum = sum + a.Quantity End If Next Return sum >= dQuantityEnd Function

Page 19: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

19OOPSLA 2004

You Gotta Long Method?

o Apply Extract Method!~ Create a stub for the new method.

~ Copy code to the new method.

~ Adjust the new method: some variables are temporaries, others are parameters, and others may become return values.

~ Compile.

~ Replace the old code with a call to the new method.

~ Compile and test.

Page 20: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

20OOPSLA 2004

Feature Envyo "A method that seems more

interested in a class other than the one it is actually in." --Refactoring, Fowler et al.

o Similar to Law of Demeter

~which we might trivialize as a.b.c

Page 21: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

21OOPSLA 2004

Feature Envy In Our Code

o Should "fits" be a behavior for goal or actual?

o Move this behavior to Actual

~and see if it feels (or smells) any better

Page 22: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

22OOPSLA 2004

Feature Envy?o Apply Move Method!

~ Decide which method(s) should move.~ Declare the method in the target.~ Copy code from the source method to the target

method.~ Adjust the target method. (You may have to

pass in a reference to the source if the new method uses some features from it.)

~ Compile.~ Ensure the source can find the proper target (via

some variable.)~ Make the source method delegate to the target

method.~ Compile and test.~ You may want to inline references to the target

so you can remove the delegating method.~ Compile and test.

Page 23: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

23OOPSLA 2004

Further Refactorings

o At this point, there are many possible improvements

o You may see string manipulation methods used to excess - this may be primitive obsession

~If you get this far, you might want to use Martin's formula for removing Primitive Obsession

Page 24: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

24OOPSLA 2004

Primitive Obsession?o Apply Replace Data Value with Object!

~ Create the new class:~ Make the data value a final field. (This makes the

new class start as a value type rather than a reference type.)

~ Give the class a constructor that stores the data value in the field.

~ Give the class a getter for the field.

~ Compile.

~ In the old class, make the type of the data value field be the new class.

~ In the old class, make any assignment call the new constructor. If the field is assigned, create a new instance of the new class.

~ Compile and test

Page 25: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

25OOPSLA 2004

Go!o Extract a tokenizeKeywords()

method!

o Extract a fits() method!

o Move fits() to the Actual class!

o Look for primitive obsession and other smells!

Page 26: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

26OOPSLA 2004

Extra Reps o apply true tdd:

~Red bar, green bar, and refactor

o Another feature - Composite Goals

~a goal that would support passing the xyz certification exam

~Allow for a goal to be composed of subgoals (and so on recursively)

Page 27: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

27OOPSLA 2004

Cooldowno Debrief

~What was one surprising or interesting thing about Refactoring?

o Where to?

~Finish your goal manager!

~Check out Bill's book, and Martin's

Page 28: Presented by Bill Wake & S teve Metsker OOPSLA 2004 Copyright is held by the author/owner(s). OOPSLA'04, October 24-28, 2004, Vancouver, British Columbia,

28OOPSLA 2004

Resourceso Beck, Kent. Test-Driven

Development: By Example

o Newkirk & Vorontsov: Test-Driven Development in Microsoft .Net

o Fowler, Martin, et al. Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999.

o Wake, William C. Refactoring Workbook, Addison-Wesley, 2003.