Kill Static, Revive Objects
TL;DR: Replace static functions with object interactions.
Problems Addressedhttps://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-iv-7sc3w8n
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-iv-7sc3w8n
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-v-evj3zs9
Steps\
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);\
TypeYou can make step-by-step replacements.
\
SafetyThis 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 |
\
TagsCohesion
\
https://hackernoon.com/refactoring-018-how-to-replace-a-singleton?embedable=true
https://maximilianocontieri.com/refactoring-007-extract-class?embedable=true
\
See alsohttps://hackernoon.com/coupling-the-one-and-only-software-designing-problem-9z5a321h?embedable=true
\
CreditsImage by Menno van der Krift from Pixabay
This article is part of the Refactoring Series.
\
All Rights Reserved. Copyright , Central Coast Communications, Inc.