Hello everyone, I am han Cao 😈, a grass code ape 🐒 who has been working for more than a year. If you like my article, you can follow ➕ to like it and grow up with me. Add my wechat: Hancao97, invite you to join the group, learn and communicate together, and become a better engineer

“This is the 16th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

background

This is the fourteenth article in my Architectural Cleanliness series on the Principle of stable dependency.

Clean Architecture Series:

  • Hancao’s column on the Way to Neat architecture

Principle of stable dependence

Dependencies must be directed in a more stable direction.

Any component that we expect to change frequently should not be relied upon by a component that is difficult to change, or else the mutable component will become very difficult to change.

The stability of

According to the Online dictionary Webster, stable means “difficult to move”.

Is a coin standing on a table stable?

Of course not. It’s a crash

A table, by contrast, is more stable, because it requires more force to move it.

So by analogy, the stability of a component is related to the difficulty of changing it. This section focuses on dependency, and one of the most straightforward ways to make a software component difficult to change is to have many other components depend on it. A component with many inbound dependencies is very stable because any changes to it need to be applied to all components that depend on it.

  • Independent component

In the figure above x is a stable component. Because there are three components that depend on X, there are three reasons why X should not be modified. It says here that X is responsible for three components. X, on the other hand, does not depend on any component, so there is no reason why it needs to be changed; we call it a “standalone” component.

  • Dependent component

The Y component in the figure below is a very unstable component. Since no other component depends on Y, Y is not responsible for any component. But because Y depends on three components at once, its changes can come from three different sources. Here we say that Y is a dependent component.

Stability index

How to quantify the stability of a component:

One way to do this is to calculate all the inbound and outbound dependencies. In this way, we can calculate the position stability of a component.

  • Fan-in: Inbound dependency, which refers to the number of classes outside the component that depend on classes inside the component.
  • Fan-out: Outbound dependencies. This metric refers to the number of classes inside a component that depend on classes outside the component.
  • I: instability, I = fan-out/(fan-in + fan-out). This metric ranges from [0,1], where I = O means that the component is the most stable, and I = 1 means that the component is the least stable.

Requirements of the Stable Dependency Principle (SDP) :

The I indicator of each component must be greater than the I indicator of the component on which it depends. In other words, the I index of each component in the component structure dependency graph must decrease in the direction of its dependency relationship.

Not all components should be stable:

If all the components in a system are in the most stable state, then the system must be unable to change, which is obviously not desirable. In fact, the purpose of our component architecture diagram is to determine which components should be stable and which should be unstable.

conclusion

What if the above principles cannot be met? We can refer to design principles (6) : THE DIP dependency inversion principle, and component building principles (3) : the dependency Free Loop Principle

✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨

Youth never knows heaven and earth

Conceit and talent are everywhere you look

Pretentious as it is

I was honest

I love such a boy

Humble and arrogant

Proud and calm ☀️

✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨

Your likes and attention are my constant motivation, you can add my wechat: HancAO97, invite you to join the group, learn and communicate together, become a better front-end engineer ~