DEV Community

Cover image for Refactoring 030 - Inline Attributes
Maxi Contieri
Maxi Contieri

Posted on

1

Refactoring 030 - Inline Attributes

Avoid accidental redundancy

TL;DR: Don’t pass attributes your object already owns

Problems Addressed 😔

Related Code Smells 💨

Steps 👣

  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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

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.

DevCycle image

OpenFeature Multi-Provider: Enabling New Feature Flagging Use-Cases

DevCycle is the first feature management platform with OpenFeature built in. We pair the reliability, scalability, and security of a managed service with freedom from vendor lock-in, helping developers ship faster with true OpenFeature-native feature flagging.

Watch Full Video 🎥

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay