educative.io

Transitive dependency violates OCP

can anyone explain this please ?


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles/my8BD0352OR

Hi @Pankaj_Shukla !!
True. Transitive dependency can violate the Open-Closed Principle (OCP). The Open-Closed Principle states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. In other words, you should be able to add new functionality without changing existing code.

Transitive dependency refers to a situation where a change in one module or class requires changes in other modules or classes that depend on it, leading to a ripple effect of modifications. This violates the Open-Closed Principle because you are forced to modify existing code (closed for modification) to accommodate the changes, rather than simply extending it (open for extension) with new code.

In an ideal OCP-compliant design, changes to one module should not affect other modules, and you should be able to introduce new functionality by adding new modules or classes without altering the existing ones. This promotes a more maintainable and flexible software architecture.
I hope it helps. Happy Learning :blush:

Thanks a lot for the explaination . Can you please confirm this example is doing the same ?
Dog:walk() – extends β†’ Animal: walk()
Now if we change the walk() β†’ walk(speed) in Animal class this will force us to change the dog:walk() method
By rippling effect you meant like the above one right ^^ ?

Yes, exactly. The scenario you described illustrates the concept of transitive dependency and how changes to the base class (Animal in this case) can have a ripple effect on the derived class (Dog).

In this example, if you initially have a Dog class that extends an Animal class and both have a walk() method, the Dog class inherits the walk() method from Animal. However, if you later decide to change the walk() method in the Animal class to accept a parameter speed, it will indeed require you to modify the walk() method in the Dog class as well. This is because the change in the base class now affects its derived class, creating a ripple effect of modifications.

This situation violates the Open-Closed Principle because you’re required to modify the existing Dog class to accommodate the change in the Animal class, rather than simply extending the Dog class with new functionality. An ideal adherence to the Open-Closed Principle would allow you to introduce the speed parameter in the Animal class without affecting the Dog class, by perhaps providing a default value for the speed parameter in the base class method.
Happy Learning :blush:

Thanks for further explanation


Course: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles
Lesson: Creational Design Patterns - Grokking the Low Level Design Interview Using OOD Principles

1 Like