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

Post on 25-Dec-2015

212 views 0 download

Tags:

transcript

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

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#

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

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

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

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

7OOPSLA 2004

The Domaino A Goal management system

~lets you

~establish goals

~monitor progress against goals

?Goal Actual

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:

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!

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

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

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?

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?

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

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

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

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

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.

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

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

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.

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

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

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!

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)

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

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.

Thanks!

william.wake@acm.org

steve.metsker@acm.org