CS 683 Emerging Technologies: Embracing Change Spring Semester, 2001 Code Smells |
||
---|---|---|
© 2001, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 27-Mar-01 |
Code Smells
If it stinks, change it
-- Grandma Beck on child-rearing
Some Smells
Duplicate
Code
|
Long
Method
|
Large
Class
|
Long
Parameter List
|
Divergent
Change
|
Shotgun
Surgery
|
Feature
Envy
|
Data
Clumps
|
Primitive
Obsession
|
Switch
Statements
|
Parallel
Inheritance Hierarchies
|
Lazy
Class
|
Speculative
Generality
|
Temporary
Field
|
Message
Chains
|
Middle
Man
|
Inappropriate
Intimacy
|
Alternative
Classes with Different Interfaces
|
Incomplete
Library Class
|
Data
Class
|
Refused
Bequest
|
Comments |
Duplicate Code[1]
Same code in two methods in same class
Form Template Method[2]
You have two methods in subclasses that perform similar steps in the same order, yet the steps are different
Get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up
Example
Note "_" is used to indicate an instance variable
Site subclass: #ResidentialSite
Site subclass: #LifelineSite
ResidentialSite>>billableAmount | base tax | base := _units * _rate. tax := base * TaxRate. ^base + tax
LifelineSite>>billableAmount | base tax | base := _units * _rate * 0.7. tax := base * TaxRate * 0.2. ^base + tax
With Template Method [3]
Site>>billableAmount ^self baseAmount + self taxAmount
Site>>taxAmount ^self subclassResponsibility
Site>>baseAmount ^self subclassResponsibility
ResidentialSite>>taxAmount ^self baseAmount * TaxRate
ResidentialSite>>baseAmount ^_units * _rate
LifelineSite>>taxAmount ^self baseAmount * TaxRate * 0.2
LifelineSite>>baseAmount ^_units * _rate * 0.7
Extract Class[4]
You have one class doing the work that should be done by two
Create a new class and move the relevant fields and methods from the old class into the new class
Substitute Algorithm[5]
You want to replace an algorithm with one that is clearer
Replace the body of the method with the new algorithm
Long Method[6]
Metrics from XP Java Project [7]
|
Production
code
|
Test
code
|
Classes |
288 |
273 |
Methods |
2158 |
2107 |
Statements |
6201 |
9080 |
Methods
/ Class
|
7.4 |
7.7 |
Statements
/ Method
|
2.9 |
4.3 |
Replace Temp with Query[8]
You are using a temporary variable to hold the result of an expression
Extract the expression into a method. Replace all references to the temp with the expression
Example
cost | basePrice | basePrice := _quantity * _itemPrice. basePrice > 1000 ifTrue:[^basePrice * 0.95] ifFalse:[^basePrice * 0.98]
Replace with
cost self basePrice > 1000 ifTrue:[^self basePrice * 0.95] ifFalse:[^self basePrice * 0.98]
basePrice ^_quantity * _itemPrice.
Introduce Parameter Object[9]
You have a group of parameters that naturally go together
Replace them with an object
Example
Customer>>amountInvoicedfrom: aDate to: anotherDate
Replace with
Customer>>amountInvoicedIn: aDateRange
Preserve Whole Object[10]
You are getting several values from an object and passing these values as parameters in a method call
Send the whole object instead
Example
| low high | low := range high. high := range low. plan validRangeFrom: low to: high.
Replace with
plan validRange: range.
Comments
Comments are a good smell, but:
Rename Method[12]
The name of a method does not reveal its purpose
Change the name of the method.
Example
cstrcdlmt
Replace with
customerCreditLimit
Long Parameter List [13]
Long parameter lists are:
Replace Parameter with Method [14]
An object invokes a method, then passes the result as a parameter for a method. The receiver can also invoke this method.
Remove the parameter and let the receiver invoke this method
Example
cost | basePrice discount | basePrice := _quantity * _itemPrice. discountLevel := self discountLevel. ^self discountedPriceOn: basePrice discount: discountLevel
Replace with:
cost | basePrice discount | basePrice := _quantity * _itemPrice. ^self discountedPriceOn: basePrice
discountedPriceOn: can call self discountLevel
[1] Fowler, pp. 76
[2] Fowler, pp. 345-351
[3] Can you find a better Template Method?
[4] Fowler, pp. 149-153
[5] Fowler, pp. 139-140
[6] Fowler, .pp. 76-77
[7] Jim Little
[8] Fowler, pp. 120-123
[9] Fowler, pp. 295-299
[10] Fowler, pp. 288-291
[11] Fowler pp. 87
[12] Fowler pp. 273-274
[13] Fowler pp. 78-79
[14] Fowler pp. 292-294
Copyright ©, All rights reserved.
2001 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 27-Mar-01    Next