SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante a The Art of Refactoring | Asmit Ghimire | Gurzu.pdf

OOP Is More Then Cars and Dogs - Midwest PHP 2017
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
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxAtikur Rahman
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8Alexei Gorobets
 
From framework coupled code to #microservices through #DDD /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 @codelytvCodelyTV
 
OOP Is More Than Cars and Dogs
OOP Is More Than Cars and DogsOOP Is More Than Cars and Dogs
OOP Is More Than Cars and DogsChris Tankersley
 
Drupalcon 2023 - How Drupal builds your pages.pdf
Drupalcon 2023 - How Drupal builds your pages.pdfDrupalcon 2023 - How Drupal builds your pages.pdf
Drupalcon 2023 - How Drupal builds your pages.pdfLuca Lusso
 
2023 - Drupalcon - How Drupal builds your pages
2023 - Drupalcon - How Drupal builds your pages2023 - Drupalcon - How Drupal builds your pages
2023 - Drupalcon - How Drupal builds your pagessparkfabrik
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormMichelangelo van Dam
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
 
Coming to Terms with OOP In Drupal - php[world] 2016
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] 2016Chris Tankersley
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applicationschartjes
 
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 - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоGeeksLab Odessa
 
So S.O.L.I.D Fu - Designing Better Code
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 CodeNeil Crookes
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practicesmanugoel2003
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java DevelopersYakov Fain
 

Semelhante a The Art of Refactoring | Asmit Ghimire | Gurzu.pdf (20)

OOP Is More Then Cars and Dogs - Midwest PHP 2017
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
 
Php
PhpPhp
Php
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptx
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
 
Patterns in PHP
Patterns in PHPPatterns in PHP
Patterns in PHP
 
From framework coupled code to #microservices through #DDD /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
 
OOP Is More Than Cars and Dogs
OOP Is More Than Cars and DogsOOP Is More Than Cars and Dogs
OOP Is More Than Cars and Dogs
 
Drupalcon 2023 - How Drupal builds your pages.pdf
Drupalcon 2023 - How Drupal builds your pages.pdfDrupalcon 2023 - How Drupal builds your pages.pdf
Drupalcon 2023 - How Drupal builds your pages.pdf
 
2023 - Drupalcon - How Drupal builds your pages
2023 - Drupalcon - How Drupal builds your pages2023 - Drupalcon - How Drupal builds your pages
2023 - Drupalcon - How Drupal builds your pages
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
Coming to Terms with OOP In Drupal - php[world] 2016
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
 
mod_rewrite
mod_rewritemod_rewrite
mod_rewrite
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
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 - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
 
So S.O.L.I.D Fu - Designing Better Code
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
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
 
Refactoring
RefactoringRefactoring
Refactoring
 

Mais de GurzuInc

Power of documentation | Aarati Shah | Gurzu.pdf
Power of documentation | Aarati Shah | Gurzu.pdfPower of documentation | Aarati Shah | Gurzu.pdf
Power of documentation | Aarati Shah | Gurzu.pdfGurzuInc
 
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdf
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdfI'm Programmer _ Ganesh Kunwar _ Gurzu.pdf
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdfGurzuInc
 
Obtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdf
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
 
Problem Solving Skill - Bishal Sapkota - Gurzu
Problem Solving Skill - Bishal Sapkota - GurzuProblem Solving Skill - Bishal Sapkota - Gurzu
Problem Solving Skill - Bishal Sapkota - GurzuGurzuInc
 
My experience with Mobile Testing - Asmita Poudel - Gurzu
My experience with Mobile Testing - Asmita Poudel - GurzuMy experience with Mobile Testing - Asmita Poudel - Gurzu
My experience with Mobile Testing - Asmita Poudel - GurzuGurzuInc
 
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptxUpgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptxGurzuInc
 
The real definition of done (1).pptx.pdf
The real definition of done (1).pptx.pdfThe real definition of done (1).pptx.pdf
The real definition of done (1).pptx.pdfGurzuInc
 
Fantastic Blogs and How to Write Them | Alaka Acharya.pptx
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.pptxGurzuInc
 
The power of saying no | Abinash Bhattarai | Gurzu.pdf
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.pdfGurzuInc
 
DDOS Attack - Gurzu Nepal
DDOS Attack - Gurzu NepalDDOS Attack - Gurzu Nepal
DDOS Attack - Gurzu NepalGurzuInc
 
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdf
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdfHotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdf
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdfGurzuInc
 
Automation Testing - G1 conference Ch13.pptx
Automation Testing - G1 conference Ch13.pptxAutomation Testing - G1 conference Ch13.pptx
Automation Testing - G1 conference Ch13.pptxGurzuInc
 
CSS 101 - G1 conference Gurzu.pptx
CSS 101 - G1 conference Gurzu.pptxCSS 101 - G1 conference Gurzu.pptx
CSS 101 - G1 conference Gurzu.pptxGurzuInc
 
Discussion Regarding benefits on taxes on income from employment.pptx
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.pptxGurzuInc
 
How not to Model Data - G1 conference.pptx
How not to Model Data - G1 conference.pptxHow not to Model Data - G1 conference.pptx
How not to Model Data - G1 conference.pptxGurzuInc
 
RTM and Test Closure Report Gurzu Inc.pptx
RTM and Test Closure Report Gurzu Inc.pptxRTM and Test Closure Report Gurzu Inc.pptx
RTM and Test Closure Report Gurzu Inc.pptxGurzuInc
 
API Testing.pptx
API Testing.pptxAPI Testing.pptx
API Testing.pptxGurzuInc
 
Building CI_CD for Mobile Development.pptx
Building CI_CD for Mobile Development.pptxBuilding CI_CD for Mobile Development.pptx
Building CI_CD for Mobile Development.pptxGurzuInc
 

Mais de GurzuInc (18)

Power of documentation | Aarati Shah | Gurzu.pdf
Power of documentation | Aarati Shah | Gurzu.pdfPower of documentation | Aarati Shah | Gurzu.pdf
Power of documentation | Aarati Shah | Gurzu.pdf
 
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdf
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdfI'm Programmer _ Ganesh Kunwar _ Gurzu.pdf
I'm Programmer _ Ganesh Kunwar _ Gurzu.pdf
 
Obtaining Your Tax Clearance Certificate_ A Quick Guide | Deepak Rai | Gurzu.pdf
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
 
Problem Solving Skill - Bishal Sapkota - Gurzu
Problem Solving Skill - Bishal Sapkota - GurzuProblem Solving Skill - Bishal Sapkota - Gurzu
Problem Solving Skill - Bishal Sapkota - Gurzu
 
My experience with Mobile Testing - Asmita Poudel - Gurzu
My experience with Mobile Testing - Asmita Poudel - GurzuMy experience with Mobile Testing - Asmita Poudel - Gurzu
My experience with Mobile Testing - Asmita Poudel - Gurzu
 
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptxUpgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx
Upgrading Services _ Ashraya Tuldhar _ Knowledge ketchup.pptx
 
The real definition of done (1).pptx.pdf
The real definition of done (1).pptx.pdfThe real definition of done (1).pptx.pdf
The real definition of done (1).pptx.pdf
 
Fantastic Blogs and How to Write Them | Alaka Acharya.pptx
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
 
The power of saying no | Abinash Bhattarai | Gurzu.pdf
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
 
DDOS Attack - Gurzu Nepal
DDOS Attack - Gurzu NepalDDOS Attack - Gurzu Nepal
DDOS Attack - Gurzu Nepal
 
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdf
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdfHotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdf
Hotwire and Turbo - Knowledge Ketchup - Prajit Bhandari.pdf
 
Automation Testing - G1 conference Ch13.pptx
Automation Testing - G1 conference Ch13.pptxAutomation Testing - G1 conference Ch13.pptx
Automation Testing - G1 conference Ch13.pptx
 
CSS 101 - G1 conference Gurzu.pptx
CSS 101 - G1 conference Gurzu.pptxCSS 101 - G1 conference Gurzu.pptx
CSS 101 - G1 conference Gurzu.pptx
 
Discussion Regarding benefits on taxes on income from employment.pptx
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
 
How not to Model Data - G1 conference.pptx
How not to Model Data - G1 conference.pptxHow not to Model Data - G1 conference.pptx
How not to Model Data - G1 conference.pptx
 
RTM and Test Closure Report Gurzu Inc.pptx
RTM and Test Closure Report Gurzu Inc.pptxRTM and Test Closure Report Gurzu Inc.pptx
RTM and Test Closure Report Gurzu Inc.pptx
 
API Testing.pptx
API Testing.pptxAPI Testing.pptx
API Testing.pptx
 
Building CI_CD for Mobile Development.pptx
Building CI_CD for Mobile Development.pptxBuilding CI_CD for Mobile Development.pptx
Building CI_CD for Mobile Development.pptx
 

Último

Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfAshrafRagab14
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxCHAIRMAN M
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2T.D. Shashikala
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...Roi Lipman
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..MaherOthman7
 
Electrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineElectrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineJulioCesarSalazarHer1
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdfKamal Acharya
 
Intelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent ActsIntelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent ActsSheetal Jain
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)NareenAsad
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfMadan Karki
 
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdfBURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdfKamal Acharya
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringKanchhaTamang
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdfKamal Acharya
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesRashidFaridChishti
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoninghotman30312
 
Multivibrator and its types defination and usges.pptx
Multivibrator and its types defination and usges.pptxMultivibrator and its types defination and usges.pptx
Multivibrator and its types defination and usges.pptxalijaker017
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Prakhyath Rai
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfJNTUA
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGKOUSTAV SARKAR
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfEr.Sonali Nasikkar
 

Último (20)

Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdf
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Electrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineElectrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission line
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
Intelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent ActsIntelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent Acts
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
 
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdfBURGER ORDERING SYSYTEM PROJECT REPORT..pdf
BURGER ORDERING SYSYTEM PROJECT REPORT..pdf
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineering
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoning
 
Multivibrator and its types defination and usges.pptx
Multivibrator and its types defination and usges.pptxMultivibrator and its types defination and usges.pptx
Multivibrator and its types defination and usges.pptx
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdf
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 

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.”