Refactoring 030 - Inline Attributes

Table of contents

TL;DR: Donβt pass attributes your object already owns
Problems Addressed π
- Redundant Parameters
- Unclear responsibility
- Duplicated logic
- Parameter pollution
- Low cohesion
- Parameter redundancy
- Code duplication
- Incomplete extracted method object
Related Code Smells π¨
Steps π£
- Identify methods that receive owned attributes
- Remove those parameters from the method signature
- Replace usage with direct access to the attribute
- Rename the method if needed to match the new intention
Sample Code π»
Before π¨
class Auto {
constructor(motor) {
this.motor = motor
}
startEngine(motor) {
motor.ignite()
}
}
// Usage
const motor = new Motor()
const auto = new Auto(motor)
auto.startEngine(motor)
// Redundant and maybe inconsistent
After π
class Auto {
constructor(motor) {
this.motor = motor
}
// 1. Identify methods that receive owned attributes
startEngine() {
// 2. Remove those parameters from the method signature
// 4. Rename the method if needed to match the new intention
this.motor.ignite()
}
}
// Adjust usage to call without passing motor
const motor = new Motor()
const auto = new Auto(motor)
// 3. Replace usage with direct access to the attribute
auto.startEngine() // No parameter needed
Type π
[X] Automatic
Safety π‘οΈ
This refactoring is straightforward and safe if you have good test coverage.
Why is the Code Better? β¨
You remove accidental complexity.
You stop pretending your method needs information from the outside.
You reduce the cognitive load and improve encapsulation.
You clarify which attributes are essential.
You avoid passing the same data through different paths.
You reduce parameter redundancy and simplify method signatures.
You eliminate the possibility of passing inconsistent values since methods now directly access the object's state.
This makes the code more maintainable and reduces the cognitive load when reading method calls.
The methods become more cohesive by relying on their own object's data rather than external parameters.
How Does it Improve the Bijection? πΊοΈ
This refactoring enhances the Bijection by aligning object behavior more closely with real-world entities.
You match the real-world concept: an object uses what it owns.
You improve the anthropomorphism and avoid unrealistic indirection.
You also reduce internal and external coupling.
Limitations β οΈ
This works only if the method always uses the internal attribute.
If you need to inject different versions for testing or variations, consider using dependency injection or a strategy pattern.
Refactor with AI π€
Suggested Prompt: 1. Identify methods that receive owned attributes 2. Remove those parameters from the method signature 3. Replace usage with direct access to the attribute 4. Rename the method if needed to match the new intention
Without Proper Instructions | With Specific Instructions |
ChatGPT | ChatGPT |
Claude | Claude |
Perplexity | Perplexity |
Copilot | Copilot |
You | You |
Gemini | Gemini |
DeepSeek | DeepSeek |
Meta AI | Meta AI |
Grok | Grok |
Qwen | Qwen |
Tags π·οΈ
- Encapsulation
Level π
[X] Beginner
Related Refactorings π
Remove Parameter
Introduce Parameter Object
Credits π
Image by F. Muhammad on Pixabay
This article is part of the Refactoring Series.
Subscribe to my newsletter
Read articles from Maxi Contieri directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Maxi Contieri
Maxi Contieri
Iβm a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.