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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

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

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay