Download presentation
Presentation is loading. Please wait.
Published byCameron Booker Modified over 9 years ago
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, Canada. 2004 ACM 04/0010
2
2 OOPSLA 2004 Premise oYou want to learn: ~test-driven development oyou are hip to learning by doing oYou have access to a Development environment ~for either Java, Visual Basic, or C#
3
3 OOPSLA 2004 Why TDD? oTest-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
4
4 OOPSLA 2004 And Refactoring? oInternally your code can be in terrible shape and yet look completely healthy externally oRefactoring ~Improves the health of code without altering its function ~Makes maintenance easier ~Tends to lead to simple, clean design
5
5 OOPSLA 2004 Plan for Today! oTest-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 oCooldown oRefactoring ~What it is ~Some Important techniques ~Clean up the Goal System ~Long Method ~Long Method (again!) ~Primitive Obsession ~Feature Envy oCooldown
6
6 OOPSLA 2004 Test-Driven! Choos e task N y Y Write 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
7
7 OOPSLA 2004 The Domain oA Goal management system ~lets you ~establish goals ~monitor progress against goals ? GoalActual
8
8 OOPSLA 2004 xUnit oFree(!) software from www.Nunit.org and www.junit.org ooriginally developed as JUnit by kent beck and erich gamma oa 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 oAnd now, A wee demo:
9
9 OOPSLA 2004 Go! ocompile testgoal. vb, TestGoal.cs, or TestGoal.java orun testgoal ~get a red bar ~w00t oImplement just enough (in Goal. vb/cs/java and Actual. vb/cs/java) to get a green bar ~Really: just enough!
10
10 OOPSLA 2004 Stoke it up! oGet 7 tests red-then-green to earn a prize! oyer 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
11
11 OOPSLA 2004 More Tests! oelaborate ~think like a unit tester ~cover any case you have missed omore 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
12
12 OOPSLA 2004 Cooldown oDebrief ~One surprising thing about tdd? oCould you use this at work? owhat about the internal health of the code? Does it matter? Can we improve it?
14
14 OOPSLA 2004 Refactoring oIn practice, TDD applies refactoring consistently oRefactoring ~a transformation that improves code but doesn ’ t change its effect oSmell ~a potential problem in code oExperience?
15
15 OOPSLA 2004 A Few refactorings oRefactorings that Address common smells: ~Long Method ~Primitive Obsession ~Feature Envy oCheck out Martin Fowler ’ s book "Refactoring" for lots of smells and refactorings
16
16 OOPSLA 2004 The Domain oA Goal vs. Actual tracking system oEnhanced to count actuals ~if they occur before the goal ~if they contain one of the goal's keywords
17
17 OOPSLA 2004 Example: Long Method Imports System.Text.RegularExpressions Public 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 Function End Class
18
18 OOPSLA 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 >= dQuantity End Function
19
19 OOPSLA 2004 You Gotta Long Method? oApply 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.
20
20 OOPSLA 2004 Feature Envy o"A method that seems more interested in a class other than the one it is actually in." --Refactoring, Fowler et al. oSimilar to Law of Demeter ~which we might trivialize as a.b.c
21
21 OOPSLA 2004 Feature Envy In Our Code oShould "fits" be a behavior for goal or actual? oMove this behavior to Actual ~and see if it feels (or smells) any better
22
22 OOPSLA 2004 Feature Envy? oApply 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.
23
23 OOPSLA 2004 Further Refactorings oAt this point, there are many possible improvements oYou 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
24
24 OOPSLA 2004 Primitive Obsession? oApply 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
25
25 OOPSLA 2004 Go! oExtract a tokenizeKeywords() method! oExtract a fits() method! oMove fits() to the Actual class! oLook for primitive obsession and other smells!
26
26 OOPSLA 2004 Extra Reps oapply true tdd: ~Red bar, green bar, and refactor oAnother 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)
27
27 OOPSLA 2004 Cooldown oDebrief ~What was one surprising or interesting thing about Refactoring? oWhere to? ~Finish your goal manager! ~Check out Bill's book, and Martin's
28
28 OOPSLA 2004 Resources oBeck, Kent. Test-Driven Development: By Example oNewkirk & Vorontsov: Test-Driven Development in Microsoft.Net oFowler, Martin, et al. Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999. oWake, William C. Refactoring Workbook, Addison-Wesley, 2003.
29
Thanks! william.wake@acm.org steve.metsker@acm.org
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.