Understanding SOLID Principles: Interface Segregation Principle
Tags: solid, bestpractices, development, learning
Published: 2017-11-11 23:05:38

---
title: "Understanding SOLID Principles: Interface Segregation Principle"
published: true
description: Understand what it takes to write SOLID code.
tags: SOLID, bestpractices, development, learning
---
![pexels](https://thepracticaldev.s3.amazonaws.com/i/ebbp96owlrswxsz37ish.jpeg)

[**Read the original article on Medium**](https://codeburst.io/understanding-solid-principles-interface-segregation-principle-b2d57026cf6c)

> This is the 4th part of the series of understanding SOLID Principles where we explore what is Interface Segregation Principle and why it helps with creating thin abstraction interfaces that make it easy for clients to have fewer dependant factors between them.

As a small reminder, in **SOLID** there are five basic principles which help to create good (or solid) software architecture. SOLID is an acronym where:-

**S** stands for SRP (Single responsibility principle)
**O** stands for OCP (Open closed principle)
**L** stands for LSP (Liskov substitution principle)
**I** stand for ISP ( Interface segregation principle)
**D** stands for DIP ( Dependency inversion principle)

We’ve discussed [Dependency Inversion](https://codeburst.io/understanding-solid-principles-dependency-injection-d570c15560ab0), [Single Responsibility](https://codeburst.io/understanding-solid-principles-single-responsibility-b7c7ec0bf80) and [Open-Closed Principle](https://medium.com/r/?url=https%3A%2F%2Fcodeburst.io%2Funderstanding-solid-principles-open-closed-principle-e2b588b6491f) before.

Now we are going to address Interface Segregation.

## Interface Segregation Principle

> Clients should not be forced to depend on methods that they do notÂ use.

This principle is very much related to the Single Responsibility Principle. What it really means is that you should always design your abstractions in a way that the clients that are using the exposed methods do not get the whole pie instead. **That also include imposing the clients with the burden of implementing methods that they don’t actually need**.

Keep your interfaces thin or fine-grained and don’t attach to them unused methods. You will have to be more resourceful with the naming as you will have to name a few more types than you do already. That’s fine because it gives you the flexibility to compose thinner interfaces into bigger ones if you need to.

Let’s see an example in Go. Suppose we have a library of utility functions that deal with byte operations that we want to expose. We create the interface:

```go
type ByteUtils interface {
    Read(b []byte) (n int, err error) // Read into buffer
    Write(b []byte)(n int, err error) // Write into buffer
    Trim(b []byte, exclusions string)[]byte // Trim buffer by removing bytes from the exclusion chars
}
```

It works for a while. However notice that the name ByteUtils is too generic, it doesn’t give you any specific informationâ€Š–â€Šit could be called anything. Also, it’s very awkward to implement the required methods because if you have a different optimisation to do on the trimming side when you read into a buffer as opposed to when you write to a buffer you will need to provide 2 different implementations of ByteUtils that both read and write that are the same. Not Good!

Instead, we could provide a simple interface list:

```go
type Reader interface {
    Read(b []byte) (n int, err error) 
}
type Writer interface {
    Write(b []byte)(n int, err error) 
}
type Trimmer interface {
    Trim(b []byte, exclusions string)[]byte 
}
```
Such thin interfaces are also called **role interfaces** as its easier to refactor, change, and redeploy something that it has a very defined role or purpose.
In that case, we could combine a more relevant interface list like that:

```go
type ReadWriter interface {
    Reader
    Writer 
}
type TrimReader interface {
    Trimmer
    Reader
}
```

This gives the flexibility for the clients to combine the abstractions as they may see fit and to provide implementations without unnecessary cargo. In the above example a client may provide an implementation for an Optimised TrimReader.

## Conclusion
As you can see fat interfaces lead to inadvertent coupling between classes and you should avoid them. **When designing interfaces you should always ask your self the question** “Do I really need all the methods on this interface I’m using? If not how can I break them into smaller interfaces?”.

As Gandhi said nonce:
>“Your actions become your habits,Â 
Your habits become your values,Â 
Your values become your destiny.”

If you do the right thing every and allow only the good patterns to emerge from your architecture, it will become a habit. And that it will become your values or principles. If you keep doing that it will become your destiny, which is to always produce sound solutions or whatever your idea of a software engineer is.

**My point is to always strive to be better by challenging yourself**. At some point, you will ask, but you will know already the answer…

Happy coding!

## References
* [Wiki page](https://medium.com/r/?url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FInterface_segregation_principle)
* [C2 Page](https://medium.com/r/?url=http%3A%2F%2Fwiki.c2.com%2F%3FRolePattern)

## Coming up next is Understanding SOLID Principles: Liskov Substitution Principle

**If this post was helpful please share it and stay tuned on my other [articles](https://medium.com/@fanisdespoudis/). You can follow me on [GitHub](https://github.com/theodesp)  and [LinkedIn](https://www.linkedin.com/in/theofanis-despoudis-7bb30913/). If you have any ideas and improvements feel free to share them with me.**

Happy coding.

**If you would like to schedule a mentoring session visit my [Codementor Profile](https://www.codementor.io/theofanisdespoudis)**.