SlideShare 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
Let’s 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 don’t 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...
Slides 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

PDF
Language portfolio
ODP
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
PDF
Pmd naming-rules
PDF
Common design patterns in php
PPTX
Behat - human-readable automated testing
PPTX
Coding standards for java
PPSX
Coding standard
ODP
BDD with Behat
Language portfolio
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Pmd naming-rules
Common design patterns in php
Behat - human-readable automated testing
Coding standards for java
Coding standard
BDD with Behat

Similar to Code refactoring workshop (in Javascript) (20)

PPTX
Refactoring, 2nd Edition
PDF
Writing Readable Code
PDF
Extract Method Refactoring Workshop (2016)
PDF
Working With Legacy Code
PDF
TDD and Simple Design Workshop - Session 1 - March 2019
PPTX
How I Learned to Stop Worrying and Love Legacy Code.....
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
PPTX
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
PPT
Code Quality
PDF
Good Coding Practices with JavaScript
PPS
Few minutes To better Code - Refactoring
PDF
Refactoring
PPTX
Ruby code smells
PDF
Evolutionary Design - Refactoring Heuristics
PDF
Half-automatic Compilable Source Code Recovery
PPTX
Writing code for others
PDF
YAGNI Principle and Clean Code
PPTX
Code reviews
PDF
Refactoring Legacy Code
Refactoring, 2nd Edition
Writing Readable Code
Extract Method Refactoring Workshop (2016)
Working With Legacy Code
TDD and Simple Design Workshop - Session 1 - March 2019
How I Learned to Stop Worrying and Love Legacy Code.....
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Code Quality
Good Coding Practices with JavaScript
Few minutes To better Code - Refactoring
Refactoring
Ruby code smells
Evolutionary Design - Refactoring Heuristics
Half-automatic Compilable Source Code Recovery
Writing code for others
YAGNI Principle and Clean Code
Code reviews
Refactoring Legacy Code
Ad

More from Ilias Bartolini (11)

PDF
Getting started with Ruby on Rails
PDF
Building and evolving microservices
PDF
Desing principles: tensions and synergies v3.0
PDF
Desing principles: tensions and synergies
PDF
Climate Change
PDF
Training and Coaching Agile Minds
PDF
Facilitation 101
PDF
The hidden beauty of the rainbow
PDF
Paul Collier - The Bottom Billion
PDF
A personal (self) review
PPTX
Lean startup And Occupy Movement
Getting started with Ruby on Rails
Building and evolving microservices
Desing principles: tensions and synergies v3.0
Desing principles: tensions and synergies
Climate Change
Training and Coaching Agile Minds
Facilitation 101
The hidden beauty of the rainbow
Paul Collier - The Bottom Billion
A personal (self) review
Lean startup And Occupy Movement
Ad

Recently uploaded (20)

PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
AI in Product Development-omnex systems
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Nekopoi APK 2025 free lastest update
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
top salesforce developer skills in 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
history of c programming in notes for students .pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Which alternative to Crystal Reports is best for small or large businesses.pdf
Design an Analysis of Algorithms II-SECS-1021-03
AI in Product Development-omnex systems
PTS Company Brochure 2025 (1).pdf.......
Nekopoi APK 2025 free lastest update
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
top salesforce developer skills in 2025.pdf
ai tools demonstartion for schools and inter college
2025 Textile ERP Trends: SAP, Odoo & Oracle
Upgrade and Innovation Strategies for SAP ERP Customers
Online Work Permit System for Fast Permit Processing
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
history of c programming in notes for students .pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
How Creative Agencies Leverage Project Management Software.pdf
CHAPTER 2 - PM Management and IT Context
Design an Analysis of Algorithms I-SECS-1021-03
How to Choose the Right IT Partner for Your Business in Malaysia
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool

Code refactoring workshop (in Javascript)