DEV Community

Cover image for Code Smell 61 - Coupling to Classes
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

2

Code Smell 61 - Coupling to Classes

Classes are handy. We can call them and invoke them any time. Is this good?

Problems

  • Coupling

  • Extensibility

  • Hard to mock

Solutions

  1. Use interfaces or traits (if available).

  2. Use Dependency Injection.

  3. Favor Loose Coupling.

Sample Code

Wrong

public class MyCollection {
public bool HasNext { get; set;}
// implementation details
public object Next();
// implementation details
}
public class MyDomainObject
sum(MyCollection anObjectThatCanBeIterated) {
// Tight coupling
}
// You cannot fake or mock this method
// since it always expects an instance of MyCollection
view raw coupled.java hosted with ❤ by GitHub

Right

public interface Iterator {
public bool HasNext { get; set;}
public object Next();
}
public Iterator Reverse(Iterator iterator) {
var list = new List<int>();
while (iterator.HasNext) {
list.Insert(0, iterator.Next());
}
return new ListIterator(list);
}
public class MyCollection implements Iterator {
public bool HasNext { get; set;} // Implementation details
public object Next(); // Implementation details
}
public class myDomainObject sum(Iterator anObjectThatCanBeIterated) {
// Loose coupling
}
// Can use any Iterator
// (even a mocked one as long as it adheres to the protocol)
view raw decoupled.java hosted with ❤ by GitHub

Detection

We can use almost any linter to find references to classes. We should not abuse since many uses might be false positives.

Tags

  • Coupling

Conclusion

Dependencies to Interfaces make a system less coupled and thus more extensible and testable.

Interfaces change less often than concrete implementations.

Some objects implement many interfaces, declaring which part depends on which interface makes the coupling more granular and the object more cohesive.

Relations

More info

Coupling

Wikipedia

Credits

Photo by Marco Bianchetti on Unsplash


When your code depends on an interface, that dependency is usually very minor and unobtrusive. Your code doesn’t have to change unless the interface changes, and interfaces typically change far less often than the code behind them. When you have an interface, you can edit classes that implement that interface or add new classes that implement the interface, all without impacting code that uses the interface.

For this reason, it is better to depend on interfaces or abstract classes than it is to depend on concrete classes. When you depend on less volatile things, you minimize the chance that particular changes will trigger massive recompilation.

Michael Feathers


Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (5)

Collapse
 
chrisza4 profile image
Chakrit Likitkhajorn

I am confused. The solution said that favor loose coupling and the conclusion favor dependency on to the interface. But in the wrong example, MyDomain class depends on interface and in the right example it depends on class.

Maybe it is swapped?

Collapse
 
mcsee profile image
Maxi Contieri

thank you very much for pointing it out

last minute change. :)
Of course coupled.java was the wrong one!

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Hi. I don't know this is an issue with your embedding or just a limitation of the platforms, but when I look at 3 different posts of yours for the first time, then initially the same file is used. On reloading immediately then it is fixed.

Perhaps its because of how multiple files exist in one gist. But doesn't explain buggy gist behavior.

First
Second

Collapse
 
mcsee profile image
Maxi Contieri

Hi.

There is a bug with dev.to app version that messes up gists.
This is not happening on the website

Can you check this out?

Collapse
 
michaelcurrin profile image
Michael Currin

It does sound like a bug. I don't think I've seen gists much before here.

I am using mobile web.

Strangely after the first 3 post problems, on the 4th post I clicked, it worked right first time around.

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more