refactoring, 2nd ed. a love story...i'm martin fowler's editor at addison-wesley. we are...
TRANSCRIPT
Questions?!
1. Who has seen the Refactoring book?
2. Who has read it?
3. Who thought that it's just common sense?
4. Who refactors code on a daily basis?
5. Who mentors new developers?
“Any fool can write code that a computer can understand. Good programmers write code that
humans can understand.”
M. Fowler (1999)
Martin Fowler
Foreword by Erich GammaContributions by Kent Beck,William Opdyke, Don Roberts
28. Juni 1999
Michael,
I'm Martin Fowler's editor at Addison-Wesley. We are working on a revision of his Refactoring book. Martin suggested that I reach out to you about reviewing the manuscript. He was very impressed with feedback you've provided on other projects.
Thanks,
Greg
-- Gregory DoenchExecutive EditorPearson Technology Group
28. November 2018
Latest Memo: Most people will be disappointed by the second edition
martinfowler.com/articles/refactoring-2nd-ed.html
Like the original, this edition explains what refactoring is; why you should refactor; how to recognize code that needs refactoring; and how to actually do it successfully, no matter what language you use.
● Understand the process and general principles of refactoring● Quickly apply useful refactorings to make a program easier to
comprehend and change● Recognize “bad smells” in code that signal opportunities to refactor● Explore the refactorings, each with explanations, motivation, mechanics,
and simple examples● Build solid tests for your refactorings● Recognize tradeoffs and obstacles to refactoring
Book Structure (Basically the same)
1. Opening Narrative ExampleTheatre Invoicing
2. Principles3. Code Smells4. Testing5. Catalogue
a. most important ones firstb. other refactorings
6. Dropped tangential topics and big refactorings
Refactoring Changes
"Split Temporary Variable" => "Split Variable"
"Extract Method" => "Extract Function"
Add Parameter, Remove Parameter, Rename Method
=> Change Function Declaration
Extract Function
Inline Function
Extract Variable
Inline Variable
Rename Variable
Encapsulate Variable
Highlighted: Most important Refactorings
Migrate Function Declaration
Change Function Declaration
Introduce Parameter Object
Combine Functions into Class
Combine Functions into Transform
Combine Functions into Class
Combine Functions into Transform
Move Statements into Function
Move Statements to Callers
Remove Dead Code
Rename Field
Rename Variable
Replace Command with Function
New Refactorings
Replace Derived Variable with Query
Replace Inline Code with Function Call
Replace Loop with Pipeline
Replace Query with Parameter
Replace Subclass with Delegate
Return Modified Value
Split Phase
Refactoring (noun): a change made to the internal structure of software to make it
easier to understand and cheaper to modify
without changing the observable behavior of the software.
Refactor (verb): to restructure software by applying a series of refactorings
without changing the observable behavior of the software.
Mysterious NameDuplicated CodeLong FunctionLong Parameter ListGlobal DataMutable DataDivergent ChangeShotgun SurgeryFeature EnvyData ClumpsPrimitive ObsessionRepeated Switches
Code SmellsLazy ElementSpeculative GeneralityTemporary FieldMessage ChainsMiddle ManInsider TradingLarge ClassAlternative Classes with Different InterfacesData ClassRefused BequestCommentsLoops
Before you start refactoring, check that you have a solid suite of tests. These
tests must be self-checking.
Martin Fowler
Run your tests frequently. Localize tests whenever you compile - every test at
least every day.
Martin Fowler
A suite of tests is a powerful bug detector
that decapitates the time it takes to find bugs.
Martin Fowler
Think of the boundary conditions under which things might go wrong and
concentrate your tests there.
Martin Fowler
Continous Clean-Up● Rename● Replace comment with function● Make intent clear● Intention in your IDE● Not about polishing
● Before adding a feature● Addressing technical debt● Improve testability (seams)● Improve understandability● For learning about a
new code-base
Planned Refactoring
Don’t let the fear that testing can’t catch all bugs stop you from writing the tests
that will catch most bugs.
Martin Fowler
Refactoring Catalogue (my favorites)Move Function
Split Loop
Replace Loop with Pipeline (Stream)
Replace Magic Literal
Replace Derived Variable with Query
Replace Typecode with Subclass
Replace Superclass with Delegate
Replace Control Flag with Break (Return)
Introduce Special Case (Null Object)
Replace Nested Conditionals with Guad
Clauses
Extract Function
Extract Variable
Push Statements into Function
Push members up
Extract Interface / Superclass
Migrate Function Declaration
Combine Functions into Transform
Encapsulate Collection
Replace Temp with Query
Separate Query from Modifier
Parameterize Function
Refactoring & Patterns● patterns encapsulate good solutions in a context
● represent roles of components
● avoid code-smells
● they can be the goal of a design improvment
● refactorings are steps to those designs
Resources
dzone.com/refcardz/refactoring-patterns refactoring.guru
martinfowler.com/tags/refactoring.html refactoring.com/catalog