DEV Community

Cover image for Refactoring 020 - Transform Static Functions
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3 1 1

Refactoring 020 - Transform Static Functions

Kill Static, Revive Objects

TL;DR: Replace static functions with object interactions.

Problems Addressed

Related Code Smells

Steps

  1. Identify static methods used in your code.

  2. Replace static methods with instance methods.

  3. Pass dependencies explicitly through constructors or method parameters.

  4. Refactor clients to interact with objects instead of static functions.

Sample Code

Before

class CharacterUtils {
    static createOrpheus() {
        return { name: "Orpheus", role: "Musician" };
    }

    static createEurydice() {
        return { name: "Eurydice", role: "Wanderer" };
    }

    static lookBack(character) {
      if (character.name === "Orpheus") {
        return "Orpheus looks back and loses Eurydice.";
    } else if (character.name === "Eurydice") {
        return "Eurydice follows Orpheus in silence.";
    }
       return "Unknown character.";
  }
}

const orpheus = CharacterUtils.createOrpheus();
const eurydice = CharacterUtils.createEurydice();
Enter fullscreen mode Exit fullscreen mode

After

// 1. Identify static methods used in your code.
// 2. Replace static methods with instance methods.
// 3. Pass dependencies explicitly through
// constructors or method parameters.

class Character {
    constructor(name, role, lookBackBehavior) {
        this.name = name;
        this.role = role;
        this.lookBackBehavior = lookBackBehavior;
    }

    lookBack() {
        return this.lookBackBehavior(this);
    }
}

// 4. Refactor clients to interact with objects 
// instead of static functions.
const orpheusLookBack = (character) =>
    "Orpheus looks back and loses Eurydice.";
const eurydiceLookBack = (character) =>
    "Eurydice follows Orpheus in silence.";

const orpheus = new Character("Orpheus", "Musician", orpheusLookBack);
const eurydice = new Character("Eurydice", "Wanderer", eurydiceLookBack);
Enter fullscreen mode Exit fullscreen mode

Type

[X] Semi-Automatic

You can make step-by-step replacements.

Safety

This refactoring is generally safe, but you should test your changes thoroughly.

Ensure no other parts of your code depend on the static methods you replace.

Why is the Code Better?

Your code is easier to test because you can replace dependencies during testing.

Objects encapsulate behavior, improving cohesion and reducing protocol overloading.

You remove hidden global dependencies, making the code clearer and easier to understand.

Refactor with AI

Without Proper Instructions With Specific Instructions
ChatGPT ChatGPT
Claude Claude
Perplexity Perplexity
Copilot Copilot
Gemini Gemini

Tags

  • Cohesion

Related Refactorings

See also

Credits

Image by Menno van der Krift from Pixabay


This article is part of the Refactoring Series.

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay