Refactoring 030 – How to Avoid Accidental Redundancy

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


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Avoid accidental redundancy

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

Problems Addressed 😔

Related Code Smells 💨

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxviii

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxv

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ii-o96s3wl4

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-x-i7r34uj

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxix

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

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 🔄

https://maximilianocontieri.com/refactoring-010-extract-method-object?embedable=true

https://hackernoon.com/refactoring-016-building-with-the-essence?embedable=true

https://maximilianocontieri.com/refactoring-020-transform-static-functions?embedable=true

https://hackernoon.com/refactoring-024-replace-global-variables-with-dependency-injection?embedable=true

  • Remove Parameter
  • Introduce Parameter Object

Credits 🙏

Image by F. Muhammad on Pixabay


This article is part of the Refactoring Series.

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings?embedable=true

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2025-07-13T13:00:04+00:00) Refactoring 030 – How to Avoid Accidental Redundancy. Retrieved from https://www.scien.cx/2025/07/13/refactoring-030-how-to-avoid-accidental-redundancy/

MLA
" » Refactoring 030 – How to Avoid Accidental Redundancy." Maximiliano Contieri | Sciencx - Sunday July 13, 2025, https://www.scien.cx/2025/07/13/refactoring-030-how-to-avoid-accidental-redundancy/
HARVARD
Maximiliano Contieri | Sciencx Sunday July 13, 2025 » Refactoring 030 – How to Avoid Accidental Redundancy., viewed ,<https://www.scien.cx/2025/07/13/refactoring-030-how-to-avoid-accidental-redundancy/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Refactoring 030 – How to Avoid Accidental Redundancy. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/13/refactoring-030-how-to-avoid-accidental-redundancy/
CHICAGO
" » Refactoring 030 – How to Avoid Accidental Redundancy." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2025/07/13/refactoring-030-how-to-avoid-accidental-redundancy/
IEEE
" » Refactoring 030 – How to Avoid Accidental Redundancy." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2025/07/13/refactoring-030-how-to-avoid-accidental-redundancy/. [Accessed: ]
rf:citation
» Refactoring 030 – How to Avoid Accidental Redundancy | Maximiliano Contieri | Sciencx | https://www.scien.cx/2025/07/13/refactoring-030-how-to-avoid-accidental-redundancy/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.