DEV Community

Cover image for Code Smell 64 - Inappropriate Intimacy
Maxi Contieri
Maxi Contieri

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

4 1

Code Smell 64 - Inappropriate Intimacy

Code Smell 64 - Inappropriate Intimacy

*Two classes entangled in love.

Problems

  • Coupling

  • Bad Responsibilities Assignments

  • Bad Cohesion

  • Class Interfaces too Public

  • Maintainability

  • Extensibility

Solutions

  1. Refactor

  2. Merge

  3. Replace Hierarchy With Delegation.

Sample Code

Wrong

class Candidate {
void printJobAddress(Job job) {
System.out.println("This is your position address");
System.out.println(job.address().street());
System.out.println(job.address().city());
System.out.println(job.address().zipCode());
if (job.address().country() == job.country()) {
System.out.println("It is a local job");
}
}
view raw intimate.java hosted with ❤ by GitHub

Right

final class Address {
void print() {
System.out.println(this.street);
System.out.println(this.city);
System.out.println(this.zipCode);
}
bool isInCounty(Country country) {
return this.country == country;
}
class Job {
void printAddress() {
System.out.println("This is your position address");
this.address().print());
if (this.address().isInCountry(this.country()) {
System.out.println("It is a local job");
}
}
}
class Candidate {
void printJobAddress(Job job) {
job.printAddress();
}
}
view raw delegate.java hosted with ❤ by GitHub

Detection

Some linters graph class relations and protocol dependency. Analyzing the collaboration graph we can infer rules and hints.

Tags

  • Coupling

Conclusion

It two classes are too related and don't talk much to others we might need to split, merge or refactor them,
Classes should know as little about each other as possible.

Relations

More info

C2 Wiki

Refactoring Guru

Codebuzz

Credits

Photo by Becca Tapert on Unsplash


No matter how slow you are writing clean code, you will always be slower if you make a mess.

Robert Martin

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more