DEV Community

Dzung Nguyen
Dzung Nguyen

Posted on

2

Interface Segregation Principle (ISP) Explained in 100 Seconds

πŸ’‘ What is Interface Segregation Principle (ISP)?

The ISP, part of the SOLID principles, states:

"A client should not be forced to depend on methods it does not use."

  • When an interface has too many unrelated methods, classes implementing it may end up with unnecessary or unused code, leading to fragile, hard-to-maintain systems.

  • ISP encourages creating smaller, more specific interfaces to keep things clean and manageable.

🎯 Simple Example

Imagine you’re ordering from a restaurant with a menu full of dishes you don’t like. You have to flip through 30 pages of irrelevant options to find it. Frustrating, right?

What if there were focused, specialized menus β€” one for sandwiches, one for sushi, one for desserts? Suddenly, ordering becomes becomes easier. This is exactly what ISP suggests:

🚫 No massive, one-size-fits-all interfaces full of unused methods.

βœ… Small, specific interfaces tailored to fit the needs of their users.

Menu

Code Example

Code Example 1

Code Example 2

🌟 Benefits

βœ… No more unnecessary methods: Each class gets exactly what it needs, nothing more.

βœ… Easier to understand, maintain and extend.

βœ… Cleaner design: No more empty or "not supported" methods.

⚠️ Signs of Violations

❌ Large Interfaces
❌ Unimplemented Methods

πŸ’‘ How to Avoid ISP Violations

βœ… Refactor Large Interfaces into smaller, more specialized ones.
βœ… Implement Only What’s Needed β€” classes should only implement methods relevant to their behavior.
βœ… Focus on Composition, not inheritance, so your code stays flexible and easier to extend without violating ISP.

πŸ“° Others

Interested? πŸ˜ƒ Check out other posts from my programming principles series!


Follow me to stay updated with my future posts:

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

πŸ‘‹ Kindness is contagious

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

Okay