This document discusses refactoring and summarizes the key points from Martin Fowler's book Refactoring. It covers what refactoring is, when it should be used by recognizing code smells, and how it should be done through small, incremental changes backed by thorough testing. The benefits of refactoring include improving code quality by reducing bugs and technical debt, while making the code easier to understand and modify. Tools now make refactoring easier by providing code analysis, refactoring suggestions, and quick fixes.
5. Which Refactoring do you like most?
Extract Method
Delete Unused Code
Ok, you won :)
6.
7. Questions?!
1. Who has seen the Refactoring book in a shelf?
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?
8. “Any fool can write code that a computer can
understand. Good programmers write code that
humans can understand.”
M. Fowler (1999)
28. 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 Doench
Executive Editor
Pearson Technology Group
30. 28. November 2018
Latest Memo:
Most people will be disappointed
by the second edition
martinfowler.com/articles/refactoring-2nd-ed.html
31. 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
37. Book Structure (Basically the same)
1. Opening Narrative Example
Theatre Invoicing
2. Principles
3. Code Smells
4. Testing
5. Catalogue
a. most important ones first
b. other refactorings
6. Dropped tangential topics and big refactorings
39. 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
42. 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
53. 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.
54. Refactor (verb): to restructure software
by applying a series of refactorings
without changing the observable
behavior of the software.
57. Mysterious Name
Duplicated Code
Long Function
Long Parameter List
Global Data
Mutable Data
Divergent Change
Shotgun Surgery
Feature Envy
Data Clumps
Primitive Obsession
Repeated Switches
Code Smells
Lazy Element
Speculative Generality
Temporary Field
Message Chains
Middle Man
Insider Trading
Large Class
Alternative Classes with Different Interfaces
Data Class
Refused Bequest
Comments
Loops
89. ● Before adding a feature
● Addressing technical debt
● Improve testability (seams)
● Improve understandability
● For learning about a
new code-base
Planned Refactoring
107. 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
109. 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
best non-fiction book
loved the style
and the boxes
It sounds simple and obvious initially but then contains lots of wisdom.
And we continually need to teach it.
It sounds simple and obvious initially but then contains lots of wisdom.
And we continually need to teach it.
basically a refresher
update examples
chance to a more recent language
gather feedback from 20 years
accomodate for new refactorings, and not only object oriented
new motivating example
programming language
some refactorings were updated / changed / renamed
some refactoring were added
duplex book https://martinfowler.com/bliki/DuplexBook.html
Except in Dresden,Germany
Javascript in 2018 is not that bad anymore (EcmaScript 2016)
Simpler Function syntax
Classes
Testing
Widely used -> have to cater for all audiences not just backend-java
because JavaScript and Java 8
as usual it is well written and engaging to read
lots of common sense, but important to point it out
if refactoring is already an part of your workflow you don't need it
but hand it to your new colleagues
I love code smells
Today you get many of them highlighted by your IDE with intentions
Some are obvious some require observation and thinking
George Dinwiddie
If your tests run in milliseconds or seconds it's a nobrainer to run them
If your tests run in milliseconds or seconds it's a nobrainer to run them
Much easier to roll back a commit than
IntelliJ also has "local history" with safepoints
DRY in your code/module
Not across it
And not at all costs
Inline and re-extract is part of refactoring
Should be a continous activity
DRY in your code/module
Not across it
And not at all costs
Inline and re-extract is part of refactoring
Reason to keep it small
Be honest
Pull the plug (Lidl)
There is always a next day, Software is soft
depending on the size of the unit
if small enough you can throw away, keep the interfaces and redo with lessons learned
sometimes, seldomly small changes can have big performance impacts
- inlining
- polymorphic call sites
don't need to freak out just be aware and have performance regression tests (git bisect)
do you tell your management?
if they are not onboard? -> change jobs?
it's not about polishing
Find the refactoring (Cmd+P, Shift-Ctrl-A)
Remember the shortcut, close the menu
use the shortcut