Direction of dependencies & The Stable Dependencies Principle (SDP)
Ali Rafique Muhammad
Software Architect | Problem solver | Crafting Clean, Scalable Solutions | Advocate for Continuous Learning
Keeping your codebase clean and easy to maintain is crucial in software development. One important principle that helps achieve this is the Stable Dependencies Principle (SDP). Let's dive into what this principle is and how it can benefit your projects.
What is the Stable Dependencies Principle (SDP)?
The Stable Dependencies Principle states that:
"Depend in the direction of stability."
In simple terms, more stable components (parts of your code that don't change often) should not depend on less stable components (parts of your code that change frequently).
Why is SDP Important?
How to Measure Stability
To understand stability, look at two things:
A component is more stable if it has fewer dependencies (lower Ce) and more dependents (higher Ca).
Relationship Between SDP and Dependency Inversion Principle (DIP)
The Dependency Inversion Principle (DIP) and SDP often work together. DIP suggests that high-level modules should not depend on low-level modules, but both should depend on abstractions (interfaces). This helps in creating stable dependencies.
Applying SDP with DIP
Let's look at an example to see how SDP and DIP can work together.
Problematic Example
Here, a stable component (CoreSystem) depends on a less stable component (ExperimentalFeature):
// Less stable component
public class ExperimentalFeature
{
public void Execute()
{
// Experimental code that changes often
}
}
// More stable component
public class CoreSystem
{
private ExperimentalFeature _feature = new ExperimentalFeature();
public void Run()
{
_feature.Execute();
}
}
This setup violates SDP because the stable CoreSystem depends on the unstable ExperimentalFeature.
Improved Example with DIP
We can fix this by using an interface to invert the dependency:
// Abstraction
public interface IFeature
{
void Execute();
}
// Less stable component implementing the abstraction
public class ExperimentalFeature : IFeature
{
public void Execute()
{
// Experimental code that changes often
}
}
// More stable component depending on the abstraction
public class CoreSystem
{
private readonly IFeature _feature;
public CoreSystem(IFeature feature)
{
_feature = feature;
}
public void Run()
{
_feature.Execute();
}
}
// Usage
public class Program
{
public static void Main()
{
IFeature feature = new ExperimentalFeature();
CoreSystem coreSystem = new CoreSystem(feature);
coreSystem.Run();
}
}
领英推荐
Benefits of This Approach
"Is SDP the same as DIP?"
While the Stable Dependencies Principle (SDP) and the Dependency Inversion Principle (DIP) are related and often complement each other, they are distinct principles with different focuses. SDP is a general and broader principle, and DIP is its specific form applicable where the level of modules is known; SDP can be more helpful when we don’t know which module is higher level, but we know which is more stable. This will mostly happen in code, which does not directly represent user behaviors. I will write more about this in the next article to keep this one sane and short, But to get a hint at how stability will help use defined dependency, look at three components In a configuration management system, we might have the following components:
Can you try to determine which one is at a higher level and which one is at a lower level? It’s difficult, but it will become clear if we look at stability.
Lets clarify how both of these work together:
Stable Dependencies Principle (SDP)
Definition:
Focus:
Dependency Inversion Principle (DIP)
Definition:
Focus:
How SDP and DIP Work Together
SDP and DIP often work together to create a robust and maintainable system:
By following both principles, you can achieve a system where:
Conclusion
The Stable Dependencies Principle (SDP) is a key guideline for making your codebase more reliable, maintainable, and scalable. By ensuring that stable components depend only on other stable components and by using interfaces to invert dependencies, you create a robust foundation for your software. Start applying SDP and DIP in your projects and see the benefits for yourself!
Software Architect | Problem solver | Crafting Clean, Scalable Solutions | Advocate for Continuous Learning
9 个月?? Read the substack article here: https://lnkd.in/dfCsSr9C