Refactoring 020 - Transform Static Functions
Kill Static, Revive Objects

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Search for a command to run...
Kill Static, Revive Objects

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
No comments yet. Be the first to comment.
Approve the logic once, then let it run the same way forever.

Different stages need different brains.

One Second Brain doesn't scale past one skull.

Style errors double when nobody enforces them.

Know who speaks before the skill runs TL;DR: Always define a clear role at the top of every skill file so you know whose perspective drives the execution. Common Mistake ❌ You write a skill full of

TL;DR: Replace static functions with object interactions.
High coupling due to global access
Poor testability
Overloaded protocols in classes
Decreased cohesion
Identify static methods used in your code.
Replace static methods with instance methods.
Pass dependencies explicitly through constructors or method parameters.
Refactor clients to interact with objects instead of static functions.
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();
// 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);
[X] Semi-Automatic
You can make several step-by-step replacements.
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.
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.
| Without Proper Instructions | With Specific Instructions |
| ChatGPT | ChatGPT |
| Claude | Claude |
| Perplexity | Perplexity |
| Copilot | Copilot |
| Gemini | Gemini |
Image by Menno van der Krift from Pixabay
This article is part of the Refactoring Series.