際際滷

際際滷Share a Scribd company logo
Code Refactoring
in Javascript
Ilias Bartolini
Agenda

Introduction to Refactoring

Initial setup exercise
- break -

Refactoring examples

You own refactoring
journey
Where are my black socks?
Where are my black socks?
I need a 12mm spanner...
I need a 12mm spanner...
Coding activity
Writing code
Reading code
Any fool can write code that a computer
can understand. Good programmers write
code that humans can understand.
- Martin Fowler
Refactoring
Refactoring
Refactoring is the process of changing a
software system in such a way that it does
not alter the external behavior of the code
yet improves its internal structure.
- Martin Fowler
Hello demo exercise

Set up in pairs
...and exchange keyboard often!

Get the codebase
$ git clone
github.com/iliasbartolini/code_refactoring
$ cd code_refactoring
$ npm install
$ npm test

Check the /1_hello/ example
See: https://refactoring.com/catalog/
See: https://refactoring.guru
Some refactoring...
Rename method, variable, class, 
Replace magic numbers
Extract / Inline method or function
Extract / Inline variable
Move method, Extract class
Replace if/switch with polymorphism
Pull up / Push down method, Extract interface
Replace inheritance with delegation
And some smells...
Source: iStock Micha Ludwiczak
Code smells and refactoring
Treatment

Extract duplicate part

Extract & split

Introduce class

Move responsibility

Split responsibilities

Introduce polymorphism, ...

Delete, delete, delete

Rename / Extract method
Symptom

Duplicated code

Long function/class

Long list of parameters

Code envy

Divergent changes

Common if/switch lists

Speculative generic code

Code comments
//are like a deodorant
Practice!
Check the examples in order
/2_date_parser/
/3_direction/
Tools & Language support
Lets focus on baby steps!
1) Identify a smell
2) Make a refactoring
3) Run tests!
4) Repeat

Write down the list of
smells you see and
refactoring you apply

Remember to swap
keyboard ;)
Practice!
Check the examples in order
/4_order_receipt/
/5_rover/
Myths
Refactoring takes time
Good design makes you faster
https://www.martinfowler.com/bliki/DesignStaminaHypothesis.html
Myths
You dont need to refactor
your test code
TDD
Test Driven Development
Write a
test first
Implement
solution
Refactor
Commit
Add more tests
Test code is code
...and should be maintained
with the same quality criteria
Myths
We need to stop the project
for this refactoring
Boy scout rule
Leave the campground
cleaner than you found it.
Summary

Change code in baby steps

Make use of a test suite

Learn to identify smells

Try to make your code obvious

Continuously improve the code design

Practice, practice, practice makes perfect
:)
Feedback
bit.ly /
<add a feedback link>
Thank you! :)
This work is licensed under a Creative Commons
Attribution-ShareAlike 3.0 Unported License.
It makes use of the works of...
際際滷s template:
Mateus Machado Luna
Images credits:
https://www.flickr.com/photos/dietjay/3140774470/
https://www.flickr.com/photos/147489968@N06/
https://www.flickr.com/photos/silvertje/2197752384/
https://www.flickr.com/photos/mtneer_man/7819976694/
https://www.flickr.com/photos/lingoflora/2331681471/

More Related Content

Code refactoring workshop (in Javascript)