test driven development and refactoring (plus an eclectic ... · test-driven development: key ideas...
TRANSCRIPT
![Page 1: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/1.jpg)
Test Driven Development
and Refactoring
(plus an eclectic flyover) CSSE 574: Session 5, Part 1
Steve Chenoweth
Phone: Office (812) 877-8974
Cell (937) 657-3885 Email:
![Page 2: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/2.jpg)
Agenda
Test-Driven Development
Refactoring
Transition to Iteration 2
Analysis Refresh
Discuss Milestones 5
![Page 3: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/3.jpg)
3
Moving from Design to Code Example
ProductCatalog
...
getProductDesc(...)
Sale
isComplete : Boolean
time : DateTime
becomeComplete()
makeLineItem(...)
makePayment(...)
getTotal()
Register
...
endSale()
enterItem(id: ItemID, qty : Integer)
makeNewSale()
makePayment(cashTendered : Money)
public class Register
{
private ProductCatalog catalog;
private Sale currentSale;
public Register(ProductCatalog pc) {...}
public void endSale() {...}
public void enterItem(ItemID id, int qty) {...}
public void makeNewSale() {...}
public void makePayment(Money cashTendered) {...}
}
1
1
catalog
currentSale
![Page 4: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/4.jpg)
What Order? Typically, least coupled
to most coupled. Why?
![Page 5: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/5.jpg)
Test-Driven Development: Key Ideas
Tests get written first (not postponed)
Stub in method, then write tests for
method before writing the actual method
Quickly alternate between testing and
implementation (i.e., one method at a time)
Build up a library of test cases
![Page 6: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/6.jpg)
Advantages of TDD
Unit tests actually get written
Programmer satisfaction is increased
Tests serve to clarify the interface and
document behavior
As test suite grows, it serves as an
automated verification
Gives developers confidence to make
changes
![Page 7: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/7.jpg)
Refactoring – a short intro
Structured, disciplined method to
rewrite/restructure existing code
without changing its external behavior
Recognized importance of refactoring
Kent Beck and Ward Cunningham
Ralph Johnson's work with refactoring and frameworks has also been an important contribution
Martin Fowler’s book on Refactoring is a must read… Likely one of the texts for CSSE 575
![Page 8: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/8.jpg)
8
Some Example Refactorings
Add Parameter
Change Association
Reference to value
Value to reference
Collapse hierarchy
Consolidate
conditionals
Procedures to objects
Decompose conditional
Encapsulate collection
Encapsulate downcast
Encapsulate field
Extract class
Extract Interface
Extract method
Extract subclass
Extract superclass
Form template method
Hide delegate
Hide method
Inline class
Inline temp
Introduce assertion
Introduce explain variable
Introduce foreign method
See http://www.refactoring.com/.
![Page 9: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/9.jpg)
9
Example: Hide Delegate
When a client is calling a delegate class of an object
![Page 10: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/10.jpg)
Bad Code Smells
Duplicated code
Long methods
Class with many instance variables
Class with many methods
Little or no use of interfaces
…
Not every bad smell
indicates a problem
See http://sourcemaking.com/refactoring/bad-smells-in-code.
![Page 11: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/11.jpg)
Refactorings, …Code Deodorant?
Refactoring Description
Extract Method
Transform a long method into a shorter one
by factoring out a portion into a private helper
method
Extract
Constant
Replace a literal constant with a constant
variable
Introduce
Explaining
Variable
Put the result of the expression, or parts of
the expression, in a temporary variable with a
name that explains its purpose
… …
![Page 12: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/12.jpg)
12
Refactoring Indicators:
Bad Smells in Code
Duplicated Code
Long Method
Large Class
Long Parameter List
Divergent Change
Shotgun Surgery
Feature Envy
Data Clumps
Primitive Obsession
Switch Statements
Parallel Interface Hierarchies
Lazy Class
Speculative Generality
Temporary Field
Message Chains
Middle Man
Inappropriate Intimacy
Incomplete Library Class
Data Class
Refused Bequest
![Page 13: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/13.jpg)
Cartoon of the Day
Used by permission. http://notinventedhe.re/on/2010-1-18
![Page 14: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/14.jpg)
From Iteration 1 to Iteration 2
Iteration 2 corresponds to Milestones 5-6
in the class
Take a few minutes to review Milestone 5
![Page 15: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/15.jpg)
Some Typical
Iteration 2 Activities
Though not necessarily for our projects,
since we took smaller bites in Iteration 1
![Page 16: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/16.jpg)
Second Iterations
Would typically add a few lower risk use
cases
First iteration focuses on greatest risks
Would typically do analysis for a significant
portion of the system’s features—maybe 80%
Wouldn’t implement all of them yet
Might implement some alternative scenarios
for use cases where we only did the main
scenario in Iteration 1
![Page 17: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/17.jpg)
SSDs in Second Iterations
Often augmented to show some intersystem
collaboration
Update other analysis artifacts as needed…
Domain model: might introduce subclasses to deal
with clarifying variability
Operation contracts: if new system operations
warrant detailed post-conditions
![Page 18: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/18.jpg)
Example SSD with Intersystem Collaboration
Multiple ―actors‖
and one ―system‖
Still focused on
system interface
![Page 19: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/19.jpg)
Create Conceptual Subclasses in DM when:
Subclass has additional attributes
Subclass has additional associations
Subclass concept ―behaves‖ differently
than superclass or other subclasses
![Page 20: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/20.jpg)
Example of Conceptual Subclasses
Which reason(s) for creating
subclasses apply here? Guideline: Append
superclass name
to subclass
Guideline: Make
superclasses
abstract
![Page 21: Test Driven Development and Refactoring (plus an eclectic ... · Test-Driven Development: Key Ideas Tests get written first (not postponed) Stub in method, then write tests for method](https://reader035.vdocuments.mx/reader035/viewer/2022081400/5f0dffbf7e708231d43d1eb0/html5/thumbnails/21.jpg)
Collections
public class Sale {
…
private List<SalesLineItem> lineItems = new
ArrayList<SalesLineItem>(); …
}
Guideline: If an object implements an interface,
use the interface type for the variable.