SlideShare a Scribd company logo
Refactoring
What is/ Why / When?
Agile Aragon
Pedro Rivas (@rm_dro)
Angel Luis Blasco (@Anglu13)
What is refactoring?
• An individual Refactoring is a software change:
To make it easier to understand
 To make it cheaper to modify
Without changing its observable behavior
• It’s based on a self-checking suite of test (<1 minute)
• = ∑ small steps
• Start Run Test Run Test Run Test
Note: Changes are Committed after every green status
Step a
Few minutes
Step b
Few minutes
Step c
Few minutes
Step b again
Few minutes
Why to refactor?
When to refactor?
• Best time is just before I need to “touch” it (after investing some
time trying to understand it)
• Add a new feature
• Fix a bug
• In a code review
• Several aims:
• Comprehension refactoring
• Preparatory refactoring
A B
When NOT to refactor?
• It’s easier to rewrite it
• When I don´t need to modify it
• In case I found a large refactoring vs small new
feature. It depends, there is a tradeoff. I certainly
refactor:
• If it makes my new feature easier to implement
• If this is a code I visit frequently
What do I tell my manager?
• Don´t try to justify with moral reasons: “Clean code”, “Good
programming practice”, …
• Don´t schedule weeks of pure refactoring
 I don´t put time on my plans to do refactoring. Most refactoring
happens while I’m adding features or fixing bugs
• Depends on your manager. Is he a techy manager?
• Yes  Isn’t hard to justify.
• No  Just don´t tell.
How do I get a solid test suite?
• It’s risk driven activity:
• Don´t test every public method.
• 100% coverage ≠ test suite quality
• Write tests to try to find bugs (now or in the future).
• Write test in areas that I´m worried about going wrong.
• Write tests for all the things should do, and for any condition that might cause the class to
fail
• First happy path, and then test at boundaries.
• Write test (on your contracts / APIs), so they don´t depend on your
implementation details.
• Otherwise I can spend more time changing the tests than the code under test
FAQ:
• Software Limits (API)
• Databases
• Feature branches
• Legacy code. See Book “Working Effectively with Legacy Code” from
Michael Feathers
• Architecture: Simple design / Incremental design / YAGNI
/Evolutionary architecture
The most useful to learn first
Refactorings Before After
Extract/Inline Function Lines of code [intention is not clear] f()
Extract/Inline Variable Expression: a * b + c const product = a*b
Change Function Declaration f(a,b) newName(a,b) or f(a) or f(a,b,c)
Encapsulate Variable alpha=23 setAlpha(a)
alpha()
Introduce Parameter Object f(min,max) range = new Range(min,max)
f(range)
Combine Functions into Class f1(), f2(), … Class X{ f1() f2() }
Combine Functions into
Transform
calculateBase(aReading)
calculateCharge(aReading)
enrichReading(original) {
const result= _.cloneDeep(original)
result.base=calculateBase(result)
result.charge=calculateCharge(result)
Split Phase Long Function f() f(){
priceOrder()
calculateShipping() }
Refactoring List I:
Note: Red ones are the opposite
Encapsulation Moving Features Organising Data
Encapsulate Record Move Function Split Variable
Encapsulate Collection Move Field Rename Field
Replace primitive with
Object
Move Statements into Functions
Move Statements to Callers
Replace derived variable with Query
Replace temp with Query Replace Inline code with existing
function call
Change Reference to Value
Change Value to Reference
Extract / Inline Class Slide statements
Hide Delegate
Remove Middle Man
Split loop
Substitute algorithm Replace loop with pipeline
Remove dead code
Hide secrets (data structures). Mainly for mutable data
Modularity: Only understand a small part to make a change
Data Structure are the key to understand what’s going on
Refactoring List II:
Simplifying conditional logic Refactoring APIs Dealing with Inheritance
Decompose conditional Separate Query from modifier Pull up / Push down method
Consolidate conditional
expression
Parameterize function Pull up / Push down field
Replace nested conditional with
guard clauses
Remove flag argument Pull up constructor body
Replace contional with
Polymorphism
Preserve Whole Object Replace type code with subclasses
Remove subclass
Introduce special case Replace Parameter with query
Replace Query with Parameter
Extract Superclass
Introduce Assertion Remove Setting method Collapse Hierarchy
Replace constructor with Factory function Replace subclass with delegate
Replace Function with Command Object
Replace command object with function
Replace superclass with delegate
Much of the complexity of programs lies in conditional logic
APIs are the module joints. Make it easy to understand / use
Inheritance can only be played once
Refactoring List III:
• And now let’s go on with the kata:
martinsson/BugsZero-Kata

More Related Content

What's hot

TDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and JasmineTDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and Jasmine
Luis Sánchez Castellanos
 
Code Review Tool Evaluation
Code Review Tool EvaluationCode Review Tool Evaluation
Code Review Tool Evaluation
Kate Semizhon
 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven DevelopmentABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
Hendrik Neumann
 
Being Lean Agile
Being Lean AgileBeing Lean Agile
Being Lean Agile
prakashmpujar
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
Diaa Al-Salehi
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy Code
Terry Yin
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
Pablo Villar
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sergey Aganezov
 
Come si applica l'OCP
Come si applica l'OCPCome si applica l'OCP
Come si applica l'OCP
Andrea Francia
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
Erick M'bwana
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Cefalo
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Anuj Arora
 
Improve your development skills with Test Driven Development
Improve your development skills with Test Driven DevelopmentImprove your development skills with Test Driven Development
Improve your development skills with Test Driven Development
John Stevenson
 
Behavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlowBehavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlow
Rachid Kherrazi
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
priya_trivedi
 
Practical unit testing in c & c++
Practical unit testing in c & c++Practical unit testing in c & c++
Practical unit testing in c & c++
Matt Hargett
 
.Net Debugging Techniques
.Net Debugging Techniques.Net Debugging Techniques
.Net Debugging Techniques
Bala Subra
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Joe Tremblay
 
Angular Unit Test
Angular Unit TestAngular Unit Test
Angular Unit Test
Michael Haberman
 
ABAPCodeRetreat - TDD Intro by Damir Majer
ABAPCodeRetreat - TDD Intro by Damir MajerABAPCodeRetreat - TDD Intro by Damir Majer
ABAPCodeRetreat - TDD Intro by Damir Majer
ABAPCodeRetreat
 

What's hot (20)

TDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and JasmineTDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and Jasmine
 
Code Review Tool Evaluation
Code Review Tool EvaluationCode Review Tool Evaluation
Code Review Tool Evaluation
 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven DevelopmentABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
 
Being Lean Agile
Being Lean AgileBeing Lean Agile
Being Lean Agile
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy Code
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Come si applica l'OCP
Come si applica l'OCPCome si applica l'OCP
Come si applica l'OCP
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Improve your development skills with Test Driven Development
Improve your development skills with Test Driven DevelopmentImprove your development skills with Test Driven Development
Improve your development skills with Test Driven Development
 
Behavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlowBehavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlow
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Practical unit testing in c & c++
Practical unit testing in c & c++Practical unit testing in c & c++
Practical unit testing in c & c++
 
.Net Debugging Techniques
.Net Debugging Techniques.Net Debugging Techniques
.Net Debugging Techniques
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Angular Unit Test
Angular Unit TestAngular Unit Test
Angular Unit Test
 
ABAPCodeRetreat - TDD Intro by Damir Majer
ABAPCodeRetreat - TDD Intro by Damir MajerABAPCodeRetreat - TDD Intro by Damir Majer
ABAPCodeRetreat - TDD Intro by Damir Majer
 

Similar to Refactoring

Feature toggles
Feature togglesFeature toggles
Feature toggles
Anand Agrawal
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
Steven Smith
 
Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023
Scott Keck-Warren
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith
 
Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023
Scott Keck-Warren
 
Feature toggling
Feature togglingFeature toggling
Feature toggling
T. Alexander Lystad
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in Golang
Sofian Hadiwijaya
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sachithra Gayan
 
Improving the Design of Existing Software
Improving the Design of Existing SoftwareImproving the Design of Existing Software
Improving the Design of Existing Software
Steven Smith
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approach
Enrico Da Ros
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCode Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Cefalo
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
Brett Child
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2Hammad Rajjoub
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
Hammad Rajjoub
 
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
 Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Daniel Gallego Vico
 
Refactoring workshop
Refactoring workshop Refactoring workshop
Refactoring workshop
Itzik Saban
 
PHPConf.asia 2016 - BDD with Behat for Beginners
PHPConf.asia 2016 - BDD with Behat for BeginnersPHPConf.asia 2016 - BDD with Behat for Beginners
PHPConf.asia 2016 - BDD with Behat for Beginners
Adam Englander
 
Continuous delivery @wcap 5-09-2013
Continuous delivery   @wcap 5-09-2013Continuous delivery   @wcap 5-09-2013
Continuous delivery @wcap 5-09-2013
David Funaro
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Edition
jexp
 

Similar to Refactoring (20)

Feature toggles
Feature togglesFeature toggles
Feature toggles
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
 
Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023
 
Feature toggling
Feature togglingFeature toggling
Feature toggling
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in Golang
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Improving the Design of Existing Software
Improving the Design of Existing SoftwareImproving the Design of Existing Software
Improving the Design of Existing Software
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approach
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCode Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
 Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
 
Refactoring workshop
Refactoring workshop Refactoring workshop
Refactoring workshop
 
PHPConf.asia 2016 - BDD with Behat for Beginners
PHPConf.asia 2016 - BDD with Behat for BeginnersPHPConf.asia 2016 - BDD with Behat for Beginners
PHPConf.asia 2016 - BDD with Behat for Beginners
 
Continuous delivery @wcap 5-09-2013
Continuous delivery   @wcap 5-09-2013Continuous delivery   @wcap 5-09-2013
Continuous delivery @wcap 5-09-2013
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Edition
 

Recently uploaded

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 

Recently uploaded (20)

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 

Refactoring

  • 1. Refactoring What is/ Why / When? Agile Aragon Pedro Rivas (@rm_dro) Angel Luis Blasco (@Anglu13)
  • 2. What is refactoring? • An individual Refactoring is a software change: To make it easier to understand  To make it cheaper to modify Without changing its observable behavior • It’s based on a self-checking suite of test (<1 minute) • = ∑ small steps • Start Run Test Run Test Run Test Note: Changes are Committed after every green status Step a Few minutes Step b Few minutes Step c Few minutes Step b again Few minutes
  • 4. When to refactor? • Best time is just before I need to “touch” it (after investing some time trying to understand it) • Add a new feature • Fix a bug • In a code review • Several aims: • Comprehension refactoring • Preparatory refactoring A B
  • 5. When NOT to refactor? • It’s easier to rewrite it • When I don´t need to modify it • In case I found a large refactoring vs small new feature. It depends, there is a tradeoff. I certainly refactor: • If it makes my new feature easier to implement • If this is a code I visit frequently
  • 6. What do I tell my manager? • Don´t try to justify with moral reasons: “Clean code”, “Good programming practice”, … • Don´t schedule weeks of pure refactoring  I don´t put time on my plans to do refactoring. Most refactoring happens while I’m adding features or fixing bugs • Depends on your manager. Is he a techy manager? • Yes  Isn’t hard to justify. • No  Just don´t tell.
  • 7. How do I get a solid test suite? • It’s risk driven activity: • Don´t test every public method. • 100% coverage ≠ test suite quality • Write tests to try to find bugs (now or in the future). • Write test in areas that I´m worried about going wrong. • Write tests for all the things should do, and for any condition that might cause the class to fail • First happy path, and then test at boundaries. • Write test (on your contracts / APIs), so they don´t depend on your implementation details. • Otherwise I can spend more time changing the tests than the code under test
  • 8. FAQ: • Software Limits (API) • Databases • Feature branches • Legacy code. See Book “Working Effectively with Legacy Code” from Michael Feathers • Architecture: Simple design / Incremental design / YAGNI /Evolutionary architecture
  • 9. The most useful to learn first Refactorings Before After Extract/Inline Function Lines of code [intention is not clear] f() Extract/Inline Variable Expression: a * b + c const product = a*b Change Function Declaration f(a,b) newName(a,b) or f(a) or f(a,b,c) Encapsulate Variable alpha=23 setAlpha(a) alpha() Introduce Parameter Object f(min,max) range = new Range(min,max) f(range) Combine Functions into Class f1(), f2(), … Class X{ f1() f2() } Combine Functions into Transform calculateBase(aReading) calculateCharge(aReading) enrichReading(original) { const result= _.cloneDeep(original) result.base=calculateBase(result) result.charge=calculateCharge(result) Split Phase Long Function f() f(){ priceOrder() calculateShipping() } Refactoring List I: Note: Red ones are the opposite
  • 10. Encapsulation Moving Features Organising Data Encapsulate Record Move Function Split Variable Encapsulate Collection Move Field Rename Field Replace primitive with Object Move Statements into Functions Move Statements to Callers Replace derived variable with Query Replace temp with Query Replace Inline code with existing function call Change Reference to Value Change Value to Reference Extract / Inline Class Slide statements Hide Delegate Remove Middle Man Split loop Substitute algorithm Replace loop with pipeline Remove dead code Hide secrets (data structures). Mainly for mutable data Modularity: Only understand a small part to make a change Data Structure are the key to understand what’s going on Refactoring List II:
  • 11. Simplifying conditional logic Refactoring APIs Dealing with Inheritance Decompose conditional Separate Query from modifier Pull up / Push down method Consolidate conditional expression Parameterize function Pull up / Push down field Replace nested conditional with guard clauses Remove flag argument Pull up constructor body Replace contional with Polymorphism Preserve Whole Object Replace type code with subclasses Remove subclass Introduce special case Replace Parameter with query Replace Query with Parameter Extract Superclass Introduce Assertion Remove Setting method Collapse Hierarchy Replace constructor with Factory function Replace subclass with delegate Replace Function with Command Object Replace command object with function Replace superclass with delegate Much of the complexity of programs lies in conditional logic APIs are the module joints. Make it easy to understand / use Inheritance can only be played once Refactoring List III:
  • 12.
  • 13. • And now let’s go on with the kata: martinsson/BugsZero-Kata

Editor's Notes

  1. Any fool can write code that a computer can understand. Good programmers write code that humans can understand Modify = Add new features / fix bugs No new functionality, no new tests Ej: When Refactoring don´t think about performance optimization
  2. In a code review pair programming is recommended
  3. API Rename Mark old function as deprecated, pass-through to the new one. Database: Use data migration scripts & versioning Refactoring are separated over several consecutive production releases Branches Feature branches last < 1 day (CI) Break large features into smaller chunks Use Feature flags Merges: The larger the feature branch, the more complicated Merges: The larger the feature branch, the less refactoring (as merges are more complicated) Legacy code: Refactoring can be a fantastic tool to understand it. But there is a common lack of tests. Refactor step by step, not all at once Refactor more in areas you visit frequently Architecture: Architecture before coding. And put flexibility mechanism into SW  Slow down my ability to react to change Build SW that solves ONLY the currently understood needs. As my understanding changes I use Refactoring to adapt to those new demands.