Code Smell 135 -  Interfaces With just One Realization

Photo by Brian Kostiuk on Unsplash

Code Smell 135 - Interfaces With just One Realization

Being generic and foreseeing the future is good.

Maxi Contieri
·May 26, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Problems
  • Solutions
  • Context
  • Sample Code
  • Detection
  • Exceptions
  • Tags
  • Relations
  • Conclusion
  • Credits

TL;DR: Don't over-generalize

Problems

  • Speculative Design

  • Complexity

  • Over-Engineering

Solutions

  1. Remove the interface until you get more examples

Context

In the past, programmers told us to design for change.

Nowadays, We follow the scientific method.

Whenever we find a duplication we remove it.

Not before.

Sample Code

Wrong

public interface Vehicle {
    public void start();
    public void stop();
}

public class Car implements Vehicle {
    public void start() {
        System.out.println("Running...");
    }
    public void stop() {
        System.out.println("Stopping...");
    }
}

// No more vehicles??

Right

public class Car {
    public void start() {
        System.out.println("Running...");
    }
    public void stop() {
        System.out.println("Stopping...");
    }
}

// Wait until more vehicles are discovered

Detection

[X] Automatic

This is very easy for our linters since they can trace this error at compile time.

Exceptions

This rule applies to inter system definition and business logic.

Some frameworks define an Interface as protocol to be fulfilled.

On our bijections we need to model existing real world protocols.

Interfaces are the MAPPER correspondence to protocol.

Dependency injection protocols declare interfaces that are fulfilled with their realizations. Until then, they can be empty.

Tags

  • Over Design

Relations

Conclusion

We need to wait for abstractions and not be creative and speculative

Credits

Photo by Brian Kostiuk on Unsplash


I love software, because if you can imagine something, you can build it.

Ray Ozzie


This article is part of the CodeSmell Series.

 
Share this

Impressum

Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)maximilianocontieri.com