SlideShare uma empresa Scribd logo
1 de 38
Baixar para ler offline
Refactoring
Introduction
(Extract Method)
February 2016
Peter Kofler, ‘Code Cop’
@codecopkofler
www.code-cop.org
Copyright Peter Kofler, licensed under CC-BY.
Peter Kofler
• Ph.D. (Appl. Math.)
• Professional Software
Developer for 15 years
• “fanatic about code quality”
• Freelance Code Mentor
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
I help development teams with
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
Professionalism
●
Quality and
Productivity
●
Continuous
Improvement
Mentoring
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
Pair Programming
●
Programming
Workshops
●
Deliberate
Practice, e.g.
Coding Dojos
Developing Quality
Software Developers
Workshop Structure
●
Simple Design
●
Code Smells
●
Refactoring
●
Refactor manually
●
Refactoring tools
(i.e. PHPStorm)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Coding Dojo Mindset
●
Safe place outside
work
●
We are here to learn
●
Need to slow down
●
Focus on doing it right
●
Collaborative Game
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Software Design
Why Software Design?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Software Design
Enables Change
Four Rules of Simple Design
(1) Passes the tests
(2) No duplication
(3) Reveals intention
(4) Fewer elements
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
"single letter variables who
the fuck do you think you are"
http://theprofoundprogrammer.com/post/26561881517/text-single-letter-variables-who-the-fuck-do
… that's a Code Smells
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Code Smell
●
“a surface indication that usually
indicates a deeper problem in the
system.“
●
quick to spot
●
e.g. bad names
●
e.g. long method
●
e.g. duplication
●
does not always indicate a problem
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
List of Code Smells (Fowler)
The Bloaters
- Long Method
- Large Class
- Primitive Obsession
- Long Parameter List
- DataClumps
The Object-Orientation Abusers
- Switch Statements
- Temporary Field
- Refused Bequest
- Alternative Classes with Different
Interfaces
The Change Preventers
- Divergent Change
- Shotgun Surgery
- Parallel Inheritance Hierarchies
The Dispensables
- Lazy class
- Data class
- Duplicate Code
- Dead Code
- Speculative Generality
The Couplers
- Feature Envy
- Inappropriate Intimacy
- Message Chains
- Middle Man
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Comment explaining
section of code
●
code grouped
into blocks
and there is
a comment
above each
few lines of
code
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
# delete previously converted BAS
File.delete PRG if File.exist? PRG
Dir['*.BAS'].each { |bas| File.delete bas }
# convert basic PRG to readable format
Dir['*.PRG'].each do |prg|
# handles only 8.3 names, rename
File.rename(prg, PRG)
puts "converting #{prg}" +
`#{CBM2ASC} #{PRG} #{BAS} b`
File.rename(BAS,
prg.sub(/.PRG$/i,'.BAS'))
end
# delete converted PRG
Dir['*.PRG'].each { |prg| File.delete prg }
Duplicate Code
●
same code found in 2 or more places
●
e.g. formatting, is user in session?
●
same expression found in 2 or more
places in the same function
●
constant value found in 2 or more places
●
e.g. items per page, default encoding
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Method does too much
●
has more than 5 lines of code
●
If in doubt – it's too big.
●
does different things
●
e.g. DB and UI
●
has strange name or
●
has 'and' in its name
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Code Smell Exercise
●
Group into pairs.
●
Get the printout of the Tennis code.
●
Read the code carefully.
●
Mark found code smells.
●
where? and which?
●
maybe more smells in one line
●
We found 25, how many can you find?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
How do we fix it?
Refactoring
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Definition
Refactoring is a technique
for restructuring
an existing body of code,
altering its internal structure
without changing
its external behaviour.
(Martin Fowler)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Small Transformations
●
behaviour preserving transformation
●
each transformation is small, less likely
to go wrong
●
system is fully working after each change
●
verified by working tests
●
sequence of transformations produce a
significant restructuring
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Extract Method
● Create a new method, name by intention
● Copy extracted code from source method to new method
● Scan for local variables.
● Temporary variables local to method?
● Local-scope variables modified?
Return changes back to parent method.
● Pass local-scope variables as parameters.
● (Compile.)
● Replace extracted code with call to new method.
● (Compile and) test.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Introduce (Explaining) Variable
●
Declare a (final) temporary variable.
●
Set it to the result of part of the expression.
●
Replace the result part of the expression
with the value of the temp.
●
If the result part of the expression is repeated,
you can replace the repeats one at a time.
●
(Compile and) test.
●
Repeat for other parts of the expression.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Refactoring Exercise
●
Group into new pairs (with computer).
●
Go to the Tennis project.
●
Run ./vendor/bin/phpunit
●
Open TennisGame2.php in text editor.
●
Extract >= 2 methods (by hand).
●
Extract >= 2 local expressions (by hand).
●
Extract >= 2 constant (by hand).
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Refactor Mercilessly
Seriously ;-)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
mer·ci·less is defined as
having or showing no mercy,
cold-blooded, hard-boiled,
heartless, insensitive, hard,
pitiless, remorseless, ruthless,
slash-and-burn, soulless,
take-no-prisoners,
unfeeling, unsympathetic
●
e.g. „extract till you drop“
https://sites.google.com/site/unclebobconsultingllc/one-thing-extract-till-you-drop
Find more in the Refactoring
Book
Refactoring Questions
●
How long is a long method?
●
How do we refactor?
●
During refactoring, how
long does code not compile?
●
When to run the tests?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Refactoring with Tools
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Automatic Refactorings
(PHPStorm)
● Rename
● Change Signature
● Move
● Extract Variable
● Extract Constant
● Extract Field
● Extract Method
● Extract Parameter
● Inline
● Safe Delete
● Copy/Clone
● Extract Interface
● Pull Members up
● Push Members down
JavaScript:
● Change Signature
● Extract Variable
● Extract Parameter
● (Format)
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Demo(Refactoring in PHPStorm)
PHPStorm Exercise
●
Group into new pairs.
●
Open Gilded Rose project in PHPStorm.
●
On folder test, select Run Tests
●
Open gilded_rose.php.
●
Clean up in small steps.
●
Try to use only automatic refactorings.
●
Run tests often.
●
Commit to Git whenever tests pass.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
What you could do
●
Make it more readable.
●
Remove duplication (extract duplicates).
●
Split into logically coherent blocks.
●
Simplify complex boolean conditions.
●
Bonus Round: Replace duplicated if-
statements with polymorphism (extract
Strategy pattern).
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Closing Circle
●
What did you learn today?
●
What surprised you today?
●
What will you do
differently in the
future?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
created by Peter Kofler
@codecopkofler
www.code-cop.org
with help from Aki Salmi
@rinkkasatiainen
https://about.me/rinkkasatiainen
both katas by Emily Bache
@emilybache
http://coding-is-like-cooking.info/2011/08/refactoring-kata-fun/
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
CC Images
●
puzzle https://www.flickr.com/photos/lizadaly/2944362379/
●
todos http://www.flickr.com/photos/kylesteeddesign/3724074594/
●
dojo http://www.flickr.com/photos/49715404@N00/3267627038/
●
wants you http://www.flickr.com/photos/shutter/105497713/
●
boys https://www.flickr.com/photos/andymorffew/16925347231/
●
smells http://www.flickr.com/photos/hhbw/4215183405/
●
exercise https://www.flickr.com/photos/sanchom/2963072255/
●
mercy http://www.flickr.com/photos/williac/99551756/
●
questions http://www.flickr.com/photos/seandreilinger/2326448445/
●
tools https://www.flickr.com/photos/tom-margie/5019211728/
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY

Mais conteúdo relacionado

Mais procurados

Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)Peter Kofler
 
JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Peter Kofler
 
Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Peter Kofler
 
Outside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDOutside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDPeter Kofler
 
Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)Peter Kofler
 
The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)Peter Kofler
 
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Peter Kofler
 
Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Peter Kofler
 
TDD as if You Meant It (2013)
TDD as if You Meant It (2013)TDD as if You Meant It (2013)
TDD as if You Meant It (2013)Peter Kofler
 
Mob Programming (2016)
Mob Programming (2016)Mob Programming (2016)
Mob Programming (2016)Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)Peter Kofler
 
Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Peter Kofler
 
Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Peter Kofler
 
Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Peter Kofler
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkPeter Kofler
 
Coding Dojo: Adding Tests to Legacy Code (2014)
Coding Dojo: Adding Tests to Legacy Code (2014)Coding Dojo: Adding Tests to Legacy Code (2014)
Coding Dojo: Adding Tests to Legacy Code (2014)Peter Kofler
 
Code Retreat Venice (2016)
Code Retreat Venice (2016)Code Retreat Venice (2016)
Code Retreat Venice (2016)Peter Kofler
 
Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013Peter Kofler
 

Mais procurados (20)

Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)
 
JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)
 
Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)
 
Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)
 
Outside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDOutside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDD
 
Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)
 
The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)
 
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
 
Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)
 
TDD as if You Meant It (2013)
TDD as if You Meant It (2013)TDD as if You Meant It (2013)
TDD as if You Meant It (2013)
 
Mob Programming (2016)
Mob Programming (2016)Mob Programming (2016)
Mob Programming (2016)
 
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)
 
Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)
 
Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)
 
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)
 
Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Coding Dojo: Adding Tests to Legacy Code (2014)
Coding Dojo: Adding Tests to Legacy Code (2014)Coding Dojo: Adding Tests to Legacy Code (2014)
Coding Dojo: Adding Tests to Legacy Code (2014)
 
Code Retreat Venice (2016)
Code Retreat Venice (2016)Code Retreat Venice (2016)
Code Retreat Venice (2016)
 
Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013
 

Destaque

Digital Optimisation Infographic - Forrester Report
Digital Optimisation Infographic - Forrester ReportDigital Optimisation Infographic - Forrester Report
Digital Optimisation Infographic - Forrester ReportOmer Celep
 
My favorite (mostly) free social media tools
My favorite (mostly) free social media toolsMy favorite (mostly) free social media tools
My favorite (mostly) free social media toolsJay Dolan
 
Lugar de encuentro y aprendizaje, una renovación de los parques infantiles
Lugar de encuentro  y aprendizaje, una renovación de los parques infantilesLugar de encuentro  y aprendizaje, una renovación de los parques infantiles
Lugar de encuentro y aprendizaje, una renovación de los parques infantilesBertaAparicio14
 
Build competitive edge through differentiated customer experience
Build competitive edge through differentiated customer experienceBuild competitive edge through differentiated customer experience
Build competitive edge through differentiated customer experienceIntense Technologies Limited
 
Oprah ( halima ahmed)
Oprah ( halima ahmed)Oprah ( halima ahmed)
Oprah ( halima ahmed)7aloom
 
OPRAH WINFREY
OPRAH WINFREYOPRAH WINFREY
OPRAH WINFREYshilu37
 
Practica de windows
Practica de windows Practica de windows
Practica de windows Norman Lucero
 
Mandarin Phonetic
Mandarin Phonetic Mandarin Phonetic
Mandarin Phonetic Devy Riani
 
Tax Problems with Big Box Stores
Tax Problems with Big Box StoresTax Problems with Big Box Stores
Tax Problems with Big Box Storesfsbrlaw
 
Trabajo Cooperativo sobre Animales invertebrados
Trabajo Cooperativo sobre Animales invertebradosTrabajo Cooperativo sobre Animales invertebrados
Trabajo Cooperativo sobre Animales invertebradosAlquimista Aula
 
Sap plant-maintenance-pm-business-blueprint-bbp2
Sap plant-maintenance-pm-business-blueprint-bbp2Sap plant-maintenance-pm-business-blueprint-bbp2
Sap plant-maintenance-pm-business-blueprint-bbp2gabrielsyst
 
La famille Simpson
La famille SimpsonLa famille Simpson
La famille Simpsonsmt786
 

Destaque (17)

Digital Optimisation Infographic - Forrester Report
Digital Optimisation Infographic - Forrester ReportDigital Optimisation Infographic - Forrester Report
Digital Optimisation Infographic - Forrester Report
 
My favorite (mostly) free social media tools
My favorite (mostly) free social media toolsMy favorite (mostly) free social media tools
My favorite (mostly) free social media tools
 
Lugar de encuentro y aprendizaje, una renovación de los parques infantiles
Lugar de encuentro  y aprendizaje, una renovación de los parques infantilesLugar de encuentro  y aprendizaje, una renovación de los parques infantiles
Lugar de encuentro y aprendizaje, una renovación de los parques infantiles
 
Estrategias de divulgación y posicionamiento de marca
Estrategias de divulgación y posicionamiento  de marcaEstrategias de divulgación y posicionamiento  de marca
Estrategias de divulgación y posicionamiento de marca
 
Portfolio_Dino
Portfolio_DinoPortfolio_Dino
Portfolio_Dino
 
Build competitive edge through differentiated customer experience
Build competitive edge through differentiated customer experienceBuild competitive edge through differentiated customer experience
Build competitive edge through differentiated customer experience
 
Tratao de Zimmermann
Tratao de ZimmermannTratao de Zimmermann
Tratao de Zimmermann
 
Oprah ( halima ahmed)
Oprah ( halima ahmed)Oprah ( halima ahmed)
Oprah ( halima ahmed)
 
OPRAH WINFREY
OPRAH WINFREYOPRAH WINFREY
OPRAH WINFREY
 
Practica de windows
Practica de windows Practica de windows
Practica de windows
 
Mandarin Phonetic
Mandarin Phonetic Mandarin Phonetic
Mandarin Phonetic
 
Tax Problems with Big Box Stores
Tax Problems with Big Box StoresTax Problems with Big Box Stores
Tax Problems with Big Box Stores
 
Alien museo 2.odp
Alien museo 2.odpAlien museo 2.odp
Alien museo 2.odp
 
Trabajo Cooperativo sobre Animales invertebrados
Trabajo Cooperativo sobre Animales invertebradosTrabajo Cooperativo sobre Animales invertebrados
Trabajo Cooperativo sobre Animales invertebrados
 
Sap plant-maintenance-pm-business-blueprint-bbp2
Sap plant-maintenance-pm-business-blueprint-bbp2Sap plant-maintenance-pm-business-blueprint-bbp2
Sap plant-maintenance-pm-business-blueprint-bbp2
 
La famille Simpson
La famille SimpsonLa famille Simpson
La famille Simpson
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 

Semelhante a Extract Method Refactoring Workshop (2016)

Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)Peter Kofler
 
Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Peter Kofler
 
Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Peter Kofler
 
Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Peter Kofler
 
Coding Dojo: Baby Steps (2014)
Coding Dojo: Baby Steps (2014)Coding Dojo: Baby Steps (2014)
Coding Dojo: Baby Steps (2014)Peter Kofler
 
Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)Peter Kofler
 
Code refactoring workshop (in Javascript)
Code refactoring workshop (in Javascript)Code refactoring workshop (in Javascript)
Code refactoring workshop (in Javascript)Ilias Bartolini
 
Coding Dojo: Roman Numerals (2014)
Coding Dojo: Roman Numerals (2014)Coding Dojo: Roman Numerals (2014)
Coding Dojo: Roman Numerals (2014)Peter Kofler
 
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Andrew Yatsenko
 
Dmytro Dziubenko "Developer's toolchain"
Dmytro Dziubenko "Developer's toolchain"Dmytro Dziubenko "Developer's toolchain"
Dmytro Dziubenko "Developer's toolchain"Fwdays
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)Peter Kofler
 
Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)Peter Kofler
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
Webpack & React Performance in 16+ Steps
Webpack & React Performance in 16+ StepsWebpack & React Performance in 16+ Steps
Webpack & React Performance in 16+ StepsGrgur Grisogono
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Peter Kofler
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your codePascal Larocque
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Editionjexp
 
Containerized build
Containerized buildContainerized build
Containerized buildDaniel Foo
 
Dev + DevOps для PHP розробника
Dev + DevOps для PHP розробникаDev + DevOps для PHP розробника
Dev + DevOps для PHP розробникаphpfriendsclub
 
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.Chris Laning
 

Semelhante a Extract Method Refactoring Workshop (2016) (20)

Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
 
Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)
 
Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)
 
Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)
 
Coding Dojo: Baby Steps (2014)
Coding Dojo: Baby Steps (2014)Coding Dojo: Baby Steps (2014)
Coding Dojo: Baby Steps (2014)
 
Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)
 
Code refactoring workshop (in Javascript)
Code refactoring workshop (in Javascript)Code refactoring workshop (in Javascript)
Code refactoring workshop (in Javascript)
 
Coding Dojo: Roman Numerals (2014)
Coding Dojo: Roman Numerals (2014)Coding Dojo: Roman Numerals (2014)
Coding Dojo: Roman Numerals (2014)
 
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
Code Quality Control in a PHP project. GeekTalks, Cherkassy 2020
 
Dmytro Dziubenko "Developer's toolchain"
Dmytro Dziubenko "Developer's toolchain"Dmytro Dziubenko "Developer's toolchain"
Dmytro Dziubenko "Developer's toolchain"
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)
 
Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Webpack & React Performance in 16+ Steps
Webpack & React Performance in 16+ StepsWebpack & React Performance in 16+ Steps
Webpack & React Performance in 16+ Steps
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Edition
 
Containerized build
Containerized buildContainerized build
Containerized build
 
Dev + DevOps для PHP розробника
Dev + DevOps для PHP розробникаDev + DevOps для PHP розробника
Dev + DevOps для PHP розробника
 
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
 

Último

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 

Último (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 

Extract Method Refactoring Workshop (2016)

  • 1. Refactoring Introduction (Extract Method) February 2016 Peter Kofler, ‘Code Cop’ @codecopkofler www.code-cop.org Copyright Peter Kofler, licensed under CC-BY.
  • 2. Peter Kofler • Ph.D. (Appl. Math.) • Professional Software Developer for 15 years • “fanatic about code quality” • Freelance Code Mentor PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 3. I help development teams with PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● Professionalism ● Quality and Productivity ● Continuous Improvement
  • 4. Mentoring PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● Pair Programming ● Programming Workshops ● Deliberate Practice, e.g. Coding Dojos
  • 6. Workshop Structure ● Simple Design ● Code Smells ● Refactoring ● Refactor manually ● Refactoring tools (i.e. PHPStorm) PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 7. Coding Dojo Mindset ● Safe place outside work ● We are here to learn ● Need to slow down ● Focus on doing it right ● Collaborative Game PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 9. Why Software Design? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 11. Four Rules of Simple Design (1) Passes the tests (2) No duplication (3) Reveals intention (4) Fewer elements PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 12. "single letter variables who the fuck do you think you are" http://theprofoundprogrammer.com/post/26561881517/text-single-letter-variables-who-the-fuck-do
  • 13. … that's a Code Smells PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 14. Code Smell ● “a surface indication that usually indicates a deeper problem in the system.“ ● quick to spot ● e.g. bad names ● e.g. long method ● e.g. duplication ● does not always indicate a problem PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 15. List of Code Smells (Fowler) The Bloaters - Long Method - Large Class - Primitive Obsession - Long Parameter List - DataClumps The Object-Orientation Abusers - Switch Statements - Temporary Field - Refused Bequest - Alternative Classes with Different Interfaces The Change Preventers - Divergent Change - Shotgun Surgery - Parallel Inheritance Hierarchies The Dispensables - Lazy class - Data class - Duplicate Code - Dead Code - Speculative Generality The Couplers - Feature Envy - Inappropriate Intimacy - Message Chains - Middle Man PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 16. Comment explaining section of code ● code grouped into blocks and there is a comment above each few lines of code PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY # delete previously converted BAS File.delete PRG if File.exist? PRG Dir['*.BAS'].each { |bas| File.delete bas } # convert basic PRG to readable format Dir['*.PRG'].each do |prg| # handles only 8.3 names, rename File.rename(prg, PRG) puts "converting #{prg}" + `#{CBM2ASC} #{PRG} #{BAS} b` File.rename(BAS, prg.sub(/.PRG$/i,'.BAS')) end # delete converted PRG Dir['*.PRG'].each { |prg| File.delete prg }
  • 17. Duplicate Code ● same code found in 2 or more places ● e.g. formatting, is user in session? ● same expression found in 2 or more places in the same function ● constant value found in 2 or more places ● e.g. items per page, default encoding PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 18. Method does too much ● has more than 5 lines of code ● If in doubt – it's too big. ● does different things ● e.g. DB and UI ● has strange name or ● has 'and' in its name PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 19. Code Smell Exercise ● Group into pairs. ● Get the printout of the Tennis code. ● Read the code carefully. ● Mark found code smells. ● where? and which? ● maybe more smells in one line ● We found 25, how many can you find? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 20. How do we fix it?
  • 21. Refactoring PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 22. Definition Refactoring is a technique for restructuring an existing body of code, altering its internal structure without changing its external behaviour. (Martin Fowler) PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 23. Small Transformations ● behaviour preserving transformation ● each transformation is small, less likely to go wrong ● system is fully working after each change ● verified by working tests ● sequence of transformations produce a significant restructuring PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 24. Extract Method ● Create a new method, name by intention ● Copy extracted code from source method to new method ● Scan for local variables. ● Temporary variables local to method? ● Local-scope variables modified? Return changes back to parent method. ● Pass local-scope variables as parameters. ● (Compile.) ● Replace extracted code with call to new method. ● (Compile and) test. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 25. Introduce (Explaining) Variable ● Declare a (final) temporary variable. ● Set it to the result of part of the expression. ● Replace the result part of the expression with the value of the temp. ● If the result part of the expression is repeated, you can replace the repeats one at a time. ● (Compile and) test. ● Repeat for other parts of the expression. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 26. Refactoring Exercise ● Group into new pairs (with computer). ● Go to the Tennis project. ● Run ./vendor/bin/phpunit ● Open TennisGame2.php in text editor. ● Extract >= 2 methods (by hand). ● Extract >= 2 local expressions (by hand). ● Extract >= 2 constant (by hand). PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 28. Seriously ;-) PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● mer·ci·less is defined as having or showing no mercy, cold-blooded, hard-boiled, heartless, insensitive, hard, pitiless, remorseless, ruthless, slash-and-burn, soulless, take-no-prisoners, unfeeling, unsympathetic ● e.g. „extract till you drop“ https://sites.google.com/site/unclebobconsultingllc/one-thing-extract-till-you-drop
  • 29. Find more in the Refactoring Book
  • 30. Refactoring Questions ● How long is a long method? ● How do we refactor? ● During refactoring, how long does code not compile? ● When to run the tests? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 31. Refactoring with Tools PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 32. Automatic Refactorings (PHPStorm) ● Rename ● Change Signature ● Move ● Extract Variable ● Extract Constant ● Extract Field ● Extract Method ● Extract Parameter ● Inline ● Safe Delete ● Copy/Clone ● Extract Interface ● Pull Members up ● Push Members down JavaScript: ● Change Signature ● Extract Variable ● Extract Parameter ● (Format) PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 34. PHPStorm Exercise ● Group into new pairs. ● Open Gilded Rose project in PHPStorm. ● On folder test, select Run Tests ● Open gilded_rose.php. ● Clean up in small steps. ● Try to use only automatic refactorings. ● Run tests often. ● Commit to Git whenever tests pass. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 35. What you could do ● Make it more readable. ● Remove duplication (extract duplicates). ● Split into logically coherent blocks. ● Simplify complex boolean conditions. ● Bonus Round: Replace duplicated if- statements with polymorphism (extract Strategy pattern). PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 36. Closing Circle ● What did you learn today? ● What surprised you today? ● What will you do differently in the future? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 37. created by Peter Kofler @codecopkofler www.code-cop.org with help from Aki Salmi @rinkkasatiainen https://about.me/rinkkasatiainen both katas by Emily Bache @emilybache http://coding-is-like-cooking.info/2011/08/refactoring-kata-fun/ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 38. CC Images ● puzzle https://www.flickr.com/photos/lizadaly/2944362379/ ● todos http://www.flickr.com/photos/kylesteeddesign/3724074594/ ● dojo http://www.flickr.com/photos/49715404@N00/3267627038/ ● wants you http://www.flickr.com/photos/shutter/105497713/ ● boys https://www.flickr.com/photos/andymorffew/16925347231/ ● smells http://www.flickr.com/photos/hhbw/4215183405/ ● exercise https://www.flickr.com/photos/sanchom/2963072255/ ● mercy http://www.flickr.com/photos/williac/99551756/ ● questions http://www.flickr.com/photos/seandreilinger/2326448445/ ● tools https://www.flickr.com/photos/tom-margie/5019211728/ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY