# Code Smell 216 - Fat Interface

> TL;DR: Split your interfaces 

# Problems

- Interface Segregation Principle Violation

- Coupling

# Solutions

1. Split the interface

# Context

The term "Fat Interface" emphasizes that the interface is overloaded with methods, including those that may not be necessary or used by all clients. 

The interface violates the principle of segregating interfaces into smaller, more focused contracts.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/dc549ce28d805020e657f227eef10c5f)
```java
interface Animal {
  void eat();
  void sleep();
  void makeSound();
  // This protocol should be common to all animals
}

class Dog implements Animal {
  public void eat() { }
  public void sleep() { }
  public void makeSound() { }
}

class Fish implements Animal
  public void eat() { }
  public void sleep() {
    throw new UnsupportedOperationException("I do not sleep");
  }
  public void makeSound() {
    throw new UnsupportedOperationException("I cannot make sounds");
  }
}

class Bullfrog implements Animal
  public void eat() { }
  public void sleep() { 
    throw new UnsupportedOperationException("I do not sleep");  
  }
  public void makeSound() { }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/811c638d22fb50bff24336695a6750ae)
```java
interface Animal {
  void move();
  void reproduce();  
}
// You can even break these two responsibilities

class Dog implements Animal {
  public void move() { }
  public void reproduce() { } 
}

class Fish implements Animal {
  public void move() { }
  public void reproduce() { } 
}

class Bullfrog implements Animal {
  public void move() { }
  public void reproduce() { } 
}
```

# Detection

[X] Manual

We can check the size of the interface protocol

# Tags

- Cohesion

# Conclusion

Favoring small, reusable code components promotes code and behavior reuse.

# Relations

%[https://maximilianocontieri.com/code-smell-61-coupling-to-classes]

%[https://maximilianocontieri.com/code-smell-135-interfaces-with-just-one-realization]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Towfiqu barbhuiya](https://unsplash.com/fr/@towfiqu999999) on [Unsplash](https://unsplash.com/s/photos/fa)
    
* * *

> The best smells are something that's easy to spot and most of time lead you to really interesting problems. Data classes (classes with all data and no behavior) are good examples of this. You look at them and ask yourself what behavior should be in this class.

_Martin Fowler_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
