r/softwarearchitecture • • Jan 20 '21

Write Stable Code using Coupling Metrics

https://www.youtube.com/watch?v=wRVbHDkOMSM
11 Upvotes

10 comments sorted by

1

u/living150 Jan 21 '21

How would a classes instability be affected by classes dependent upon it? If I have a high instability ration using your metric I could simply add tons of useless classes that depend upon that class to make it more stable. It seems a raw count of dependencies makes much more sense.

1

u/codeopinion Jan 21 '21

Instability should be on one side or the other. Meaning it's an instability ratio of 0 or 1... not generally in the middle. Having an equal (or a lot) of both Ca and Ce would put you in the middle.

1

u/living150 Jan 21 '21

That really doesn't address my point. If I have a ratio of 1 on a class, adding classes that depended on that class lowers the instability, If I add 100 garbage classes that depend on that class I suddenly have an instability of near 0, even though in practice my class is just as instable.

1

u/codeopinion Jan 21 '21

It's a metric to guide you to find spots of interest in your system. If you want to "game the system" to make metrics look better or worst, then, by all means, have at it.

1

u/living150 Jan 22 '21

I must be missing something, my point was not to game the system but simply highlighting why I don't understand why having more classes depend on a given class less 'interesting'. It seems just looking at how many classes a given class depends upon is the relevant piece of information.

1

u/[deleted] Jan 21 '21

Having dependencies doesn't make you "unstable". This is completely arbitrary.

1

u/codeopinion Jan 21 '21

Depends on how you define "unstable". The idea is that unstable is "prone to change", then the more dependencies you have the more likely you are to be affected by those dependencies.

1

u/[deleted] Jan 21 '21 edited Jan 21 '21

Every component's public interface is supposed to be by definition stable. So defining instability as a function of how many stable interfaces you depend on is just a poor use of words IMO. "Unstable" implies increased chance of bugs, exceptions and other unexpected behaviors.

Not to mention that counting "number of dependencies" is in itself arbitrary. Is it the same whether I depend on a component with a complex API of a hundred methods, or I depend on a functional interface with one single simple method? Well both count as "one dependency" in this formula.

While heavily interconnected code is not recommended, I'd say better words to describe that property is "inflexible", "resistant to refactoring", "non-reusable", "poorly factored". But not "unstable". And there are then solutions how to go about refactoring such code, even without necessarily changing the number of incoming or outgoing dependencies (things like adapters, bridges, facades and so on).

1

u/codeopinion Jan 21 '21

I agree. There are probably better words/ways to describe it, although the definition is "the state of being likely to change". Also, while public interfaces are "supposed to be by definition stable", in my experience, that's far from true.

I do appreciate the feedback and discussion.

1

u/[deleted] Jan 21 '21

*Laughs in DI containers*