πŸ“Œ Law of Demeter: Minimizing Knowledge in Code

This guide explains the Law of Demeter, highlighting its importance in reducing system coupling and enhancing code maintainability.

May 29, 2025

DevelopersTechnologyBusiness

πŸ“Œ Law of Demeter: Minimizing Knowledge in Code

This guide explains the Law of Demeter, highlighting its importance in reducing system coupling and enhancing code maintainability.

1. What is the Law of Demeter? πŸ€”

The Law of Demeter, also known as the Principle of Least Knowledge, advocates that an object should interact only with its immediate collaborators. This minimizes unnecessary dependencies, making the code more flexible and maintainable.

2. Key Benefits of the Law of Demeter πŸš€

  • Reduction of Coupling: Fewer direct dependencies between components.
  • Improved Maintainability: Simplifies comprehension and modifications.
  • Minimal Impact from Changes: Changes in one module minimally affect others.
  • Clear and Understandable Code: Streamlines interactions and dependencies.

3. Strategies for Effective Application 🎯

  1. Direct Communication: Interact only with close collaborators.
  2. Avoid Long Call Chains: Do not access indirect objects (e.g., obj.a.b.c).
  3. Define Clear Interfaces: Use explicit interfaces and methods.
  4. Delegation: Clearly delegate responsibilities.

4. Practical Example: Law of Demeter in JavaScript πŸ› οΈ

⚠️ Violation of the Law of Demeter (High Coupling):

class Motor {
  arrancar() {
    console.log("Motor encendido");
  }
}

class Automovil {
  constructor() {
    this.motor = new Motor();
  }
}

class Persona {
  constructor() {
    this.auto = new Automovil();
  }

  iniciarAuto() {
    this.auto.motor.arrancar(); // Violation (indirect knowledge)
  }
}

βœ… Correct Application of the Law of Demeter:

class Motor {
  arrancar() {
    console.log("Motor encendido");
  }
}

class Automovil {
  constructor() {
    this.motor = new Motor();
  }

  arrancarMotor() {
    this.motor.arrancar();
  }
}

class Persona {
  constructor(auto) {
    this.auto = auto;
  }

  iniciarAuto() {
    this.auto.arrancarMotor(); // Correct (direct knowledge)
  }
}

const miAuto = new Automovil();
const conductor = new Persona(miAuto);
conductor.iniciarAuto();

This approach reduces coupling, allowing each class to interact only with its immediate collaborators.

5. Additional Tips for Applying the Law of Demeter πŸ“š

  • Regularly review code for indirect interactions.
  • Implement intermediary methods to manage access to secondary objects.
  • Ensure clear delegation of responsibilities among modules.

6. Conclusion: Decoupled and Clear Code πŸ”

The Law of Demeter is vital for achieving clean, decoupled, and maintainable code. By applying this principle, you minimize unnecessary dependencies and significantly simplify your software’s structure. Implement this law in your daily coding practices to optimize and enhance code quality.

Β© 2025 Synara LLC.

Leave your review

Rate with stars:

There are no reviews yet.