Refactoring is the process of restructuring existing computer code -- changing its factors -- without changing its behavior. You probably knew that. But did you know it’s also a meditation technique? Come improve your practice of mindful coding.
After these slides, we’ll start with some actual production code, make sure its unit tests work ok, then collaboratively, consciously contemplate the many forking paths that twine and wind through the Garden of Abstract Delights.
refactoring (verb): to restructure software by applying a series of refactorings without changing its observable behavior
refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior
(definitions by Martin Fowler)
mostly an excuse for pretty pictures and hippie jokes
disclaimer: This presentation uses some spiritual imagery but is not intended to parody any religion or culture (except American New Age Hippies)
Refactoring is changing code without changing its behavior.
Refactoring is changing code without changing its behavior.
Refactoring is change without change.
Refactoring is changing code without changing its behavior.
Refactoring is change without change.
Refactoring is Zen.
rewriting
rebuilding
porting
upgradging
debugging
(Yoda ≈ Buddha)
clarity calm insight knowledge understanding
See the code for what it is, not what you think it is, or what you want it to be
DRY
sometimes clarity requires more duplication
Clear code is:
“Curiously enough, one cannot read a book; one can only reread it. A good reader, a major reader, an active and creative reader is a rereader.” - Nabokov
https://www.goodreads.com/quotes/34497-curiously-enough-one-cannot-read-a-book-one-can-only
Let your eyes blur and scroll through the code
See what pops out
run them before
change them during
tests are an extension of code
or at least made of many short, reversible steps
I choose my text editor based on how well it refactors my code
(Other programmers may have different criteria.)
The simplest and most powerful
Extract Variable, Extract Function, Extract Method
console.log('' + new Date() + ' - error: too many notes')
console.log('' + new Date() + ' - error: not enough cowbell')
=>
function logError(message) {
console.log('' + new Date() + ' - error: ' + message)
}
logError('too many notes')
logError('not enough cowbell')
"Inline" is the reverse of "Extract"
Introduces duplication to increase clarity
A medium-length comment can often be replaced with a variable / function / method / class whose name is the comment
"A comment is a lie waiting to happen." - Alex
// 11 days at 24 hours per day times 60 minutes per hour
let duration = 11 * 24 * 60
=>
let hoursPerDay = 24
let minutesPerHour = 60
let durationInMinutes = 11 * hoursPerDay * minutesPerHour
function logError(message, severity = 'error') {
console.log('' + new Date() + ' - ' + severity + ': ' + message)
}
logError('too many notes')
logError('volume too low', 'warning')
function distanceBetween(x1, y1, x2, y2) {
let deltaX = x2 - x1;
let deltaY = y2 - y1;
return Math.sqrt(deltaX * deltaX + deltaY * deltaY)
}
let distance = distanceBetween(41.12, -72.22, 42.18, -72.34)
=>
function distanceBetween(point1, point2) {
let deltaX = point2.x - point1.x;
let deltaY = point2.y - point1.y;
return Math.sqrt(deltaX * deltaX + deltaY * deltaY)
}
let distance = distanceBetween( { x: 41.12, y: -72.22 },
{ x: 42.18, y: -72.34 })
This type of object -- with fields but no methods -- is sometimes called a
struct
.
class Point {
distanceFrom(otherPoint) {
let deltaLat = otherPoint.x - this.x;
let deltaLon = otherPoint.y - this.y;
return Math.sqrt(deltaLat * deltaLat + deltaLon * deltaLon)
}
}
let point1 = new Point();
point1.x = 41.12
point1.y = -72.22
let point2 = new Point();
point2.x = 42.18
point2.y = -72.34
let distance = point1.distanceFrom(point2)
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
//...
}
let point1 = new Point(41.12, -72.22)
let point2 = new Point(42.18, -72.34)
let distance = point1.distanceFrom(point2)
a "code smell" means "something might be wrong here"
if
smore at Code Smells
don't confuse method chaining (often good) with feature envy (usually bad))
string.split(' ').map((s)=>s.toUpperCase()).join(' ')
course.students[0].phone.sendText('hi')
style | dots | collaborators |
---|---|---|
method chaining | 4 | 2 (String, Array) |
feature envy | 3 | 5 (Course, Array, Student, Phone, String) |
https://martinfowler.com/books/refactoring.html
...coming soon in JavaScript!
/