Refactoring Smalltalk Programs
Don Roberts and John Brant
Summary of Course
- Philosophy
- Software Development Lifecycle
- Techniques
- Standard Smalltalk Tools
- Smalllint and Refactoring Browser
Software Maintenance
- Two Views
- Software Development = Maintaining a Blank Sheet of Paper
- Practically all “maintenance” is just continuing development
- The Fact is:
- Software is never “finished”
The Lie(aka: The Waterfall Model)
- First, you gather requirements
- Then, you design the system
- Then, you are done (except for maintenance)
Reality“Build one to throw away.” - Fred Brooks
- You will never get it right the first time
- Can’t understand the problem domain
- Can’t understand user requirements
- Can’t understand how the system will change
- Result
- Original design is inadequate
- System becomes convoluted and brittle
- Changes become more and more difficult
Evolutionary Software Development“Grow, don’t build software.” - Fred Brooks
- Prototype
- solidifies user requirements
- sketch of system design
- Expansion
- add functionality
- determine “hot-spots”
- Consolidation
- correct design defects
- introduce new abstractions
Definition of RefactoringInterface design and functional factoring constitute the key intellectual content of software and are far more difficult to create or re-create than code. - Peter Deutch
- vt. - The process of redesigning the abstractions in a program
- n. - A behavior-preserving source-to-source program transformation
A Simple Refactoring
Refactorings
Where Refactorings Come From
Patterns & Refactoring
- Patterns provide solutions to problems in a context
- Refactorings are used to “apply” a Pattern to existing code
Renaming Items
- Intention Revealing Selector
- Role Suggesting Instance/Temporary Variable Name
- Type Suggesting Parameter Name
- Simple Enumeration Parameter
Extract Method
Move Code to Component
- Shortcut Constructor Method
System Development
Strategies
- Localize Changes
- Eliminate Duplication
- Break Apart Large Methods / Classes
- Employ Standard Idioms
- Reduce Subclassing for Reuse
Eliminate Duplication “[I refactor] any time/place I say something more than once” - Kent Beck
- Duplicate code implies a common abstraction
- Have to change the code in multiple places
- Often parameterization will make “almost” duplicate code the same
Large Methods
- Method is the unit of overriding
- Eliminate multiple message sends to a single object
Method Object
- Convert a large method into its own class
- temps become instance variables
- allows the method to be decomposed without passing many parameters
- Often a strategy object
Large Classes
- Often aggregation of many, smaller concepts
- Look for sets of instance variables used only by a subset of the methods
- Look for parameters passed between many methods
Standard Idioms
- e.g. Collections
- newColl := OrderedCollection new.
aColl do: [:each | newColl add: each value]
- newColl := aColl collect: [:each | each value]
- Composed methods generate idioms within the system
Reduce Subclassing
- Replace subclassing with parameterization
- blocks or selectors instead of methods
- dictionary instead of instance variables
- Allows variance along several axes
- Apply various design pattern (e.g., strategy)
Techniques
- Standard System Tools
- System Browser
- Reference Browsers
- Specialized Tools
- Smalllint
- Refactoring Browser
References
- Ralph Johnson and Brian Foote. Designing Reusable Classes. Journal of Object-Oriented Programming 1(2):22-35, 1988.
- Brian Foote and William Opdyke. Lifecycle and Refactoring Patterns That Support Evolution and Reuse. In Pattern Languages of Program Design (Coplien and Schmidt, eds.), Addison-Wesley, 1995.
- William Opdyke. Refactoring Object-Oriented Frameworks. PhD thesis, University of Illinois at Urbana-Champaign, 1992.
References (2)
- Don Roberts and Ralph Johnson. Evolving Frameworks. To appear in Pattern Languages of Progam Design-3. Addison-Wesley, to be published.
http://st-www.cs.uiuc.edu/~droberts/evolve.html
URLs
- Refactoring Browser & SmallLint
- http://st-www.cs.uiuc.edu/~brant/Refactory
- Papers
- ftp://st.cs.uiuc.edu/pub/papers/refactoring
- http://st-www.cs.uiuc.edu/~droberts/evolve.html