The Art of Refactoring | Asmit Ghimire | Gurzu.pdf

GurzuInc
THE ART OF
REFACTORING
ENHANCING CODE QUALITY AND MAINTAINABILITY Prepared By: Asmit Ghimire
Introduction
➔ Refactoring is the process of
restructuring code while not changing
its original functionality.
➔ The main purpose of refactoring is to
enhance the code's readability,
maintainability, and overall quality,
making it easier to understand and
modify while reducing technical debt.
Why Refactoring Matters
➔ Improved code quality / readability
➔ Maintainability
➔ Reduced technical debt
➔ Quicker issue diagnosis
➔ Enhanced team collaboration
➔ Improved extensibility
➔ Performance gain in some cases
Signs It's Time to
Refactor
➔ Code Duplication
➔ Long Methods / Functions
➔ Complex Conditional
Statements
➔ Tight Coupling
➔ Performance Issues
➔ Changing Requirements
➔ Excessive Comments
Refactoring Techniques
➔ Descriptive names to variables,
methods, and classes
➔ Replace Magic Numbers with Named
Constants
➔ Consolidate Conditional Expressions
➔ Introduce Explaining Variable
➔ Introduce Parameter Object
➔ Remove Dead Code
➔ Separate Concerns with Design Patterns
➔ Pull Up / Push Down Methods
➔ Replace Conditional with Polymorphism
Pull Up Method
➔ Problem
◆ Your subclasses have methods that perform
similar work.
➔ Solution
◆ Make the methods identical and then move
them to the relevant superclass.
Push Down Method
➔ Problem
◆ Is behavior implemented in a superclass used by only one (or a few)
subclasses?
➔ Solution
◆ Move this behavior to the subclasses.
Replace Conditionals
with Polymorphism
➔ Problem
◆ You have a conditional that
performs various actions depending
on object type or properties.
class Bird {
// ...
public function getSpeed() {
switch ($this->type) {
case EUROPEAN:
return $this->getBaseSpeed();
case AFRICAN:
return $this->getBaseSpeed() -
$this->getLoadFactor() * $this->numberOfCoconuts;
case NORWEGIAN_BLUE:
return ($this->isNailed) ? 0 :
$this->getBaseSpeed($this->voltage);
}
throw new Exception("Should be unreachable" );
}
// ...
}
Replace Conditionals
with Polymorphism
➔ Solution
◆ Create subclasses matching the
branches of the conditional.
◆ In them, create a shared method and
move code from the corresponding
branch of the conditional to it.
◆ Then replace the conditional with the
relevant method call.
abstract class Bird {
// ...
abstract function getSpeed();
// ...
}
class European extends Bird {
public function getSpeed() {
return $this->getBaseSpeed();
}
}
class African extends Bird {
public function getSpeed() {
return $this->getBaseSpeed() -
$this->getLoadFactor() *
$this->numberOfCoconuts;
}
}
class NorwegianBlue extends Bird {
public function getSpeed() {
return ($this->isNailed) ? 0 :
$this->getBaseSpeed($this->voltage);
}
}
// Somewhere in Client code.
$speed = $bird->getSpeed();
Steps to Successful Refactoring
➔ Identify the target area for refactoring.
➔ Understand the existing behavior and write tests.
➔ Apply the chosen refactoring technique.
➔ Run tests to verify correctness.
➔ Commit changes to version control.
Tools and Resources
➔ SonarQube: Provides extensive static
code analysis, detecting bugs,
vulnerabilities, and code smells across
multiple languages.
➔ CodeClimate: Analyzes code quality,
security, and maintainability, offering
insights and metrics.
Challenges
➔ Time and Resources
➔ Risk of Introducing Bugs
➔ Lack of Automated Tests
➔ Legacy Code and Dependencies
➔ Team Buy-In
➔ Scope Creep
➔ Balancing Refactoring with New Features
➔ Resistance to Change
➔
“Code refactoring is like doing the dishes: it's not fun, but
leaving them all piled up will make things worse.”
The Art of Refactoring | Asmit Ghimire | Gurzu.pdf
1 of 14

Recommended

PHPSpec BDD Framework by
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD FrameworkMarcello Duarte
3K views47 slides
PHP: 4 Design Patterns to Make Better Code by
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodeSWIFTotter Solutions
1.2K views104 slides
Vision academy classes bcs_bca_bba_sybba_php by
Vision academy  classes bcs_bca_bba_sybba_phpVision academy  classes bcs_bca_bba_sybba_php
Vision academy classes bcs_bca_bba_sybba_phpsachin892777
44 views48 slides
Performance measurement and tuning by
Performance measurement and tuningPerformance measurement and tuning
Performance measurement and tuningAOE
2.3K views99 slides
PHP 5.3 Overview by
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overviewjsmith92
492 views38 slides
War between Tools and Design 2016 by
War between Tools and Design 2016War between Tools and Design 2016
War between Tools and Design 2016Mark Windholtz
167 views53 slides

More Related Content

Similar to The Art of Refactoring | Asmit Ghimire | Gurzu.pdf

OOP Is More Then Cars and Dogs - Midwest PHP 2017 by
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017Chris Tankersley
608 views63 slides
Php by
PhpPhp
PhpRajkiran Mummadi
181 views67 slides
PHP OOP Lecture - 02.pptx by
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxAtikur Rahman
42 views17 slides
Dependency injection in Drupal 8 by
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8Alexei Gorobets
3.8K views57 slides
Patterns in PHP by
Patterns in PHPPatterns in PHP
Patterns in PHPDiego Lewin
686 views27 slides
From framework coupled code to #microservices through #DDD /by @codelytv by
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvCodelyTV
1.5K views50 slides

Similar to The Art of Refactoring | Asmit Ghimire | Gurzu.pdf(20)

OOP Is More Then Cars and Dogs - Midwest PHP 2017 by Chris Tankersley
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017
Chris Tankersley608 views
Dependency injection in Drupal 8 by Alexei Gorobets
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
Alexei Gorobets3.8K views
From framework coupled code to #microservices through #DDD /by @codelytv by CodelyTV
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytv
CodelyTV1.5K views
Drupalcon 2023 - How Drupal builds your pages.pdf by Luca Lusso
Drupalcon 2023 - How Drupal builds your pages.pdfDrupalcon 2023 - How Drupal builds your pages.pdf
Drupalcon 2023 - How Drupal builds your pages.pdf
Luca Lusso90 views
2023 - Drupalcon - How Drupal builds your pages by sparkfabrik
2023 - Drupalcon - How Drupal builds your pages2023 - Drupalcon - How Drupal builds your pages
2023 - Drupalcon - How Drupal builds your pages
sparkfabrik4 views
Quality assurance for php projects with PHPStorm by Michelangelo van Dam
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
Michelangelo van Dam16.2K views
Oops concepts in php by CPD INDIA
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA3.6K views
Coming to Terms with OOP In Drupal - php[world] 2016 by Chris Tankersley
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016
Chris Tankersley387 views
Building Testable PHP Applications by chartjes
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
chartjes7.4K views
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко by GeeksLab Odessa
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
GeeksLab Odessa1.5K views
So S.O.L.I.D Fu - Designing Better Code by Neil Crookes
So S.O.L.I.D Fu - Designing Better CodeSo S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better Code
Neil Crookes223 views
Drupal Best Practices by manugoel2003
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
manugoel20035.4K views
Dart for Java Developers by Yakov Fain
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
Yakov Fain2.1K views

More from GurzuInc

Power of documentation | Aarati Shah | Gurzu.pdf by
Power of documentation | Aarati Shah | Gurzu.pdfPower of documentation | Aarati Shah | Gurzu.pdf
Power of documentation | Aarati Shah | Gurzu.pdfGurzuInc
2 views8 slides
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdf by
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdfI'm Programmer _ Ganesh Kunwar _ Gurzu.pdf
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdfGurzuInc
3 views7 slides
Obtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdf by
Obtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdfObtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdf
Obtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdfGurzuInc
15 views13 slides
Problem Solving Skill - Bishal Sapkota - Gurzu by
Problem Solving Skill - Bishal Sapkota - GurzuProblem Solving Skill - Bishal Sapkota - Gurzu
Problem Solving Skill - Bishal Sapkota - GurzuGurzuInc
16 views27 slides
My experience with Mobile Testing - Asmita Poudel - Gurzu by
My experience with Mobile Testing - Asmita Poudel - GurzuMy experience with Mobile Testing - Asmita Poudel - Gurzu
My experience with Mobile Testing - Asmita Poudel - GurzuGurzuInc
5 views14 slides
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx by
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptxUpgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptxGurzuInc
2 views10 slides

More from GurzuInc(17)

Power of documentation | Aarati Shah | Gurzu.pdf by GurzuInc
Power of documentation | Aarati Shah | Gurzu.pdfPower of documentation | Aarati Shah | Gurzu.pdf
Power of documentation | Aarati Shah | Gurzu.pdf
GurzuInc2 views
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdf by GurzuInc
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdfI'm Programmer _ Ganesh Kunwar _ Gurzu.pdf
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdf
GurzuInc3 views
Obtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdf by GurzuInc
Obtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdfObtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdf
Obtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdf
GurzuInc15 views
Problem Solving Skill - Bishal Sapkota - Gurzu by GurzuInc
Problem Solving Skill - Bishal Sapkota - GurzuProblem Solving Skill - Bishal Sapkota - Gurzu
Problem Solving Skill - Bishal Sapkota - Gurzu
GurzuInc16 views
My experience with Mobile Testing - Asmita Poudel - Gurzu by GurzuInc
My experience with Mobile Testing - Asmita Poudel - GurzuMy experience with Mobile Testing - Asmita Poudel - Gurzu
My experience with Mobile Testing - Asmita Poudel - Gurzu
GurzuInc5 views
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx by GurzuInc
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptxUpgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx
GurzuInc2 views
The real definition of done (1).pptx.pdf by GurzuInc
The real definition of done (1).pptx.pdfThe real definition of done (1).pptx.pdf
The real definition of done (1).pptx.pdf
GurzuInc6 views
Fantastic Blogs and How to Write Them | Alaka Acharya.pptx by GurzuInc
Fantastic Blogs and How to Write Them | Alaka Acharya.pptxFantastic Blogs and How to Write Them | Alaka Acharya.pptx
Fantastic Blogs and How to Write Them | Alaka Acharya.pptx
GurzuInc11 views
The power of saying no | Abinash Bhattarai | Gurzu.pdf by GurzuInc
The power of saying no | Abinash Bhattarai | Gurzu.pdfThe power of saying no | Abinash Bhattarai | Gurzu.pdf
The power of saying no | Abinash Bhattarai | Gurzu.pdf
GurzuInc41 views
DDOS Attack - Gurzu Nepal by GurzuInc
DDOS Attack - Gurzu NepalDDOS Attack - Gurzu Nepal
DDOS Attack - Gurzu Nepal
GurzuInc17 views
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdf by GurzuInc
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdfHotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdf
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdf
GurzuInc47 views
Automation Testing - G1 conference Ch13.pptx by GurzuInc
Automation Testing - G1 conference Ch13.pptxAutomation Testing - G1 conference Ch13.pptx
Automation Testing - G1 conference Ch13.pptx
GurzuInc17 views
CSS 101 - G1 conference Gurzu.pptx by GurzuInc
CSS 101 - G1 conference Gurzu.pptxCSS 101 - G1 conference Gurzu.pptx
CSS 101 - G1 conference Gurzu.pptx
GurzuInc10 views
Discussion Regarding benefits on taxes on income from employment.pptx by GurzuInc
Discussion Regarding benefits on taxes on income from employment.pptxDiscussion Regarding benefits on taxes on income from employment.pptx
Discussion Regarding benefits on taxes on income from employment.pptx
GurzuInc7 views
How not to Model Data - G1 conference.pptx by GurzuInc
How not to Model Data - G1 conference.pptxHow not to Model Data - G1 conference.pptx
How not to Model Data - G1 conference.pptx
GurzuInc9 views
API Testing.pptx by GurzuInc
API Testing.pptxAPI Testing.pptx
API Testing.pptx
GurzuInc26 views
Building CI_CD for Mobile Development.pptx by GurzuInc
Building CI_CD for Mobile Development.pptxBuilding CI_CD for Mobile Development.pptx
Building CI_CD for Mobile Development.pptx
GurzuInc9 views

Recently uploaded

Control Systems Feedback.pdf by
Control Systems Feedback.pdfControl Systems Feedback.pdf
Control Systems Feedback.pdfLGGaming5
6 views39 slides
Pull down shoulder press final report docx (1).pdf by
Pull down shoulder press final report docx (1).pdfPull down shoulder press final report docx (1).pdf
Pull down shoulder press final report docx (1).pdfComsat Universal Islamabad Wah Campus
17 views25 slides
Design_Discover_Develop_Campaign.pptx by
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptxShivanshSeth6
32 views20 slides
MK__Cert.pdf by
MK__Cert.pdfMK__Cert.pdf
MK__Cert.pdfHassan Khan
11 views1 slide
What is Unit Testing by
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
24 views25 slides

Recently uploaded(20)

Control Systems Feedback.pdf by LGGaming5
Control Systems Feedback.pdfControl Systems Feedback.pdf
Control Systems Feedback.pdf
LGGaming56 views
Design_Discover_Develop_Campaign.pptx by ShivanshSeth6
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptx
ShivanshSeth632 views
Generative AI Models & Their Applications by SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN8 views
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx by lwang78
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
lwang7883 views
Introduction to CAD-CAM.pptx by suyogpatil49
Introduction to CAD-CAM.pptxIntroduction to CAD-CAM.pptx
Introduction to CAD-CAM.pptx
suyogpatil495 views
DevOps-ITverse-2023-IIT-DU.pptx by Anowar Hossain
DevOps-ITverse-2023-IIT-DU.pptxDevOps-ITverse-2023-IIT-DU.pptx
DevOps-ITverse-2023-IIT-DU.pptx
Anowar Hossain12 views
Effect of deep chemical mixing columns on properties of surrounding soft clay... by AltinKaradagli
Effect of deep chemical mixing columns on properties of surrounding soft clay...Effect of deep chemical mixing columns on properties of surrounding soft clay...
Effect of deep chemical mixing columns on properties of surrounding soft clay...
AltinKaradagli9 views
Instrumentation & Control Lab Manual.pdf by NTU Faisalabad
Instrumentation & Control Lab Manual.pdfInstrumentation & Control Lab Manual.pdf
Instrumentation & Control Lab Manual.pdf
NTU Faisalabad 6 views
SUMIT SQL PROJECT SUPERSTORE 1.pptx by Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 15 views
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) by Tsuyoshi Horigome
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Design of machine elements-UNIT 3.pptx by gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy32 views

The Art of Refactoring | Asmit Ghimire | Gurzu.pdf

  • 1. THE ART OF REFACTORING ENHANCING CODE QUALITY AND MAINTAINABILITY Prepared By: Asmit Ghimire
  • 2. Introduction ➔ Refactoring is the process of restructuring code while not changing its original functionality. ➔ The main purpose of refactoring is to enhance the code's readability, maintainability, and overall quality, making it easier to understand and modify while reducing technical debt.
  • 3. Why Refactoring Matters ➔ Improved code quality / readability ➔ Maintainability ➔ Reduced technical debt ➔ Quicker issue diagnosis ➔ Enhanced team collaboration ➔ Improved extensibility ➔ Performance gain in some cases
  • 4. Signs It's Time to Refactor ➔ Code Duplication ➔ Long Methods / Functions ➔ Complex Conditional Statements ➔ Tight Coupling ➔ Performance Issues ➔ Changing Requirements ➔ Excessive Comments
  • 5. Refactoring Techniques ➔ Descriptive names to variables, methods, and classes ➔ Replace Magic Numbers with Named Constants ➔ Consolidate Conditional Expressions ➔ Introduce Explaining Variable ➔ Introduce Parameter Object ➔ Remove Dead Code ➔ Separate Concerns with Design Patterns ➔ Pull Up / Push Down Methods ➔ Replace Conditional with Polymorphism
  • 6. Pull Up Method ➔ Problem ◆ Your subclasses have methods that perform similar work. ➔ Solution ◆ Make the methods identical and then move them to the relevant superclass.
  • 7. Push Down Method ➔ Problem ◆ Is behavior implemented in a superclass used by only one (or a few) subclasses? ➔ Solution ◆ Move this behavior to the subclasses.
  • 8. Replace Conditionals with Polymorphism ➔ Problem ◆ You have a conditional that performs various actions depending on object type or properties. class Bird { // ... public function getSpeed() { switch ($this->type) { case EUROPEAN: return $this->getBaseSpeed(); case AFRICAN: return $this->getBaseSpeed() - $this->getLoadFactor() * $this->numberOfCoconuts; case NORWEGIAN_BLUE: return ($this->isNailed) ? 0 : $this->getBaseSpeed($this->voltage); } throw new Exception("Should be unreachable" ); } // ... }
  • 9. Replace Conditionals with Polymorphism ➔ Solution ◆ Create subclasses matching the branches of the conditional. ◆ In them, create a shared method and move code from the corresponding branch of the conditional to it. ◆ Then replace the conditional with the relevant method call. abstract class Bird { // ... abstract function getSpeed(); // ... } class European extends Bird { public function getSpeed() { return $this->getBaseSpeed(); } } class African extends Bird { public function getSpeed() { return $this->getBaseSpeed() - $this->getLoadFactor() * $this->numberOfCoconuts; } } class NorwegianBlue extends Bird { public function getSpeed() { return ($this->isNailed) ? 0 : $this->getBaseSpeed($this->voltage); } } // Somewhere in Client code. $speed = $bird->getSpeed();
  • 10. Steps to Successful Refactoring ➔ Identify the target area for refactoring. ➔ Understand the existing behavior and write tests. ➔ Apply the chosen refactoring technique. ➔ Run tests to verify correctness. ➔ Commit changes to version control.
  • 11. Tools and Resources ➔ SonarQube: Provides extensive static code analysis, detecting bugs, vulnerabilities, and code smells across multiple languages. ➔ CodeClimate: Analyzes code quality, security, and maintainability, offering insights and metrics.
  • 12. Challenges ➔ Time and Resources ➔ Risk of Introducing Bugs ➔ Lack of Automated Tests ➔ Legacy Code and Dependencies ➔ Team Buy-In ➔ Scope Creep ➔ Balancing Refactoring with New Features ➔ Resistance to Change ➔
  • 13. “Code refactoring is like doing the dishes: it's not fun, but leaving them all piled up will make things worse.”