Skip to content

Files

Latest commit

383c384 · May 25, 2025

History

History
164 lines (100 loc) · 5.99 KB

File metadata and controls

164 lines (100 loc) · 5.99 KB

Refactoring 002 - Extract Method

Refactoring 002 - Extract Method

Find some code snippets that can be grouped and called atomically.

TL;DR: Group your cohesive sentences together

Problems Addressed 😔

  • Readability

  • Complexity

  • Code Reuse

Related Code Smells 💨

Code Smell 03 - Functions Are Too Long

Code Smell 05 - Comment Abusers

Code Smell 18 - Static Functions

Code Smell 22 - Helpers

Code Smell 74 - Empty Lines

Code Smell 78 - Callback Hell

Code Smell 102 - Arrow Code

Steps 👣

  1. Move the code fragment to a separate new method

  2. Replace the old code with a call to the recently created method.

Sample Code 📖

Before 🚨

object Ingenuity {
    fun moveFollowingPerseverance() {
        // take Off
        raiseTo(10 feet)
      
        // move forward to perseverance
        while (distanceToPerseverance() < 5 feet) {
             moveForward()             
         }
        
        // land
        raiseTo(0 feet)
    }

After 👉

object Ingenuity {   
    //1. Move the code fragment to a separate new method 
    private fun takeOff() {
        raiseTo(10 feet)
    }
    
    //1. Move the code fragment to a separate new method 
    private fun moveForwardToPerseverance() {
       while (distanceToPerseverance() < 5 feet) {
             moveForward()             
         }
    }
    
    //1. Move the code fragment to a separate new method 
    private fun land() {
        raiseTo(0 feet)
    }
    
    fun moveFollowingPerseverance() {
        takeOff()
        // 2. Replace the old code with a call
        // to the recently created method
        moveForwardToPerseverance()
        // 2. Replace the old code with a call 
        // to the recently created method
        land()
        // 2. Replace the old code with a call
        // to the recently created method
    }
}

Type 📝

[X] Automatic

Many IDEs support this safe refactoring

Safety 🛡️

This is a safe refactoring.

Why is the Code Better? ✨

Code is more compact and easier to read.

Functions can be reused.

Algorithms and functions are more declarative hiding implementative details on extracted code.

Limitations ⚠️

Does not work well if you use meta-programming anti-pattern.

Tags 🏷️

  • Complexity

  • Readability

Level 🔋

[X] Beginner

Related Refactorings 🔄

Code Smell 107 - Variables Reuse

Refactoring 010 - Extract Method Object

Code Smell 154 - Too Many Variables

Code Smell 169 - Glued Methods

Refactoring 025 - Decompose Regular Expressions

Refactoring 007 - Extract Class

Refactoring 003 - Extract Constant

Refactoring 012 - Reify Associative Arrays

Refactoring 019 - Reify Email Addresses

Refactoring 013 - Remove Repeated Code

Refactoring 005 - Replace Comment with Function Name

  • Move method to a new class

Credits 🙏

Image by Hreisho on Pixabay


This article is part of the Refactoring Series

How to Improve Your Code With Easy Refactorings