Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Art of Refactoring | Asmit Ghimire | Gurzu

Gurzu
August 17, 2023

The Art of Refactoring | Asmit Ghimire | Gurzu

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, while reducing technical debt.

In this episode of knowledge ketchup, Asmit talks about the art of refactoring.

Gurzu

August 17, 2023
Tweet

More Decks by Gurzu

Other Decks in Programming

Transcript

  1. 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.
  2. Why Refactoring Matters ➔ Improved code quality / readability ➔

    Maintainability ➔ Reduced technical debt ➔ Quicker issue diagnosis ➔ Enhanced team collaboration ➔ Improved extensibility ➔ Performance gain in some cases
  3. Signs It's Time to Refactor ➔ Code Duplication ➔ Long

    Methods / Functions ➔ Complex Conditional Statements ➔ Tight Coupling ➔ Performance Issues ➔ Changing Requirements ➔ Excessive Comments
  4. 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
  5. 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.
  6. 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.
  7. 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" ); } // ... }
  8. 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();
  9. 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.
  10. 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.
  11. 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 ➔
  12. “Code refactoring is like doing the dishes: it's not fun,

    but leaving them all piled up will make things worse.”