Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Embracing the Interface Segregation Principle in your codebase

April 19, 2023

Embracing the Interface Segregation Principle in Your Codebase

In the world of software development, the Interface Segregation Principle (ISP) is one of the five SOLID principles of object-oriented programming. It focuses on the importance of designing software interfaces in such a way that clients are not forced to depend on methods they do not use.

The principle states: “Clients should not be forced to depend on interfaces that they do not use.” In simpler terms, this means that it’s better to have many specific interfaces than a single general-purpose interface. By adhering to this principle, we can create more flexible and maintainable code that is easier to understand and extend.

In this blog post, we will explore how to embrace the Interface Segregation Principle in your codebase using examples in C and Python. We will demonstrate how to design and refactor interfaces to adhere to the ISP and discuss the benefits of doing so.

Understanding the Interface Segregation Principle

To better understand the Interface Segregation Principle, let’s consider a scenario where we have a simple interface for a document editing application. The interface provides methods for editing, saving, and printing documents.

In a non-compliant design, the interface might look like this:

1
2
3
4
5
6
// Non-compliant C interface
typedef struct {
    void (*edit)(void);
    void (*save)(void);
    void (*print)(void);
} DocumentInterface;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Non-compliant Python interface
class DocumentInterface:
    def edit(self):
        pass
    
    def save(self):
        pass

    def print(self):
        pass

In this design, any class that implements the DocumentInterface is forced to provide implementations for all three methods, even if it doesn’t need them. This violates the Interface Segregation Principle because clients are forced to depend on methods they may not use.

Embracing the Interface Segregation Principle in C

To adhere to the Interface Segregation Principle in C, we can refactor the DocumentInterface into more specific interfaces that are tailored to different use cases. This allows clients to depend only on the interfaces they need.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Compliant C interfaces
typedef struct {
    void (*edit)(void);
} EditableDocumentInterface;

typedef struct {
    void (*save)(void);
} SavableDocumentInterface;

typedef struct {
    void (*print)(void);
} PrintableDocumentInterface;

In this compliant design, classes can implement one or more interfaces based on their specific needs, providing a more flexible and cohesive codebase.

Embracing the Interface Segregation Principle in Python

In Python, we can achieve the same result by defining more specific interfaces using abstract base classes (ABCs) provided by the abc module.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Compliant Python interfaces
from abc import ABC, abstractmethod

class EditableDocumentInterface(ABC):
    @abstractmethod
    def edit(self):
        pass

class SavableDocumentInterface(ABC):
    @abstractmethod
    def save(self):
        pass

class PrintableDocumentInterface(ABC):
    @abstractmethod
    def print(self):
        pass

By defining specific interfaces for editing, saving, and printing documents, we adhere to the Interface Segregation Principle and provide a clear and focused contract for classes to implement.

Benefits of Embracing the Interface Segregation Principle

By embracing the Interface Segregation Principle in your codebase, you can reap several benefits:

  1. Flexibility: Clients can depend on specific interfaces tailored to their needs, allowing for more flexible and modular designs.

  2. Maintainability: Refactoring interfaces to adhere to the ISP makes the codebase easier to maintain and extend, as changes to specific interfaces have localized impacts.

  3. Clarity: Clear and focused interfaces make it easier for developers to understand and reason about the behavior of classes that implement them.

In conclusion, the Interface Segregation Principle encourages us to design software interfaces that are tailored to specific use cases, promoting flexibility, maintainability, and clarity in our codebase. By refactoring interfaces to adhere to the ISP, we can create more cohesive and adaptable designs that stand the test of time.

So next time you are working on a new feature or refactoring existing code, consider the Interface Segregation Principle and strive to create interfaces that are specific, cohesive, and tailored to the needs of your clients.

Happy coding!


➡️ Set up github keys


⬅️ My git aliases


Go back to Posts.