ISP - The Interface Segregation Principle

The Interface Segregation Principle is part of SOLID, a mnemonic acronym which bundles a total of 5 design principles.

It is often associated with clean code.

But what exactly is it, is it important to you, should you even care?

🧵👇
1⃣ What does it state?

The ISP sets a pretty simple statement:

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

ISP applied to the extreme will result in single method interfaces, also called role interfaces.
You can take a look at the following example to get an idea of how the ISP can be violated in one way (this time it's in TypeScript): interface ReportService {  ...
Can you spot the issue?

The class implementing the interface actually doesn't want to implement two of the three methods defined within the interface.

But it is forced to do so because the interface is too bulky!

The client in this case is you, satisfying the interface.

👇
2⃣ What does it try to prevent?

It tries to prevent:

- Confusion among client developers
- Sprawling maintenance costs

most importantly.

But we'll take a look to find out what this actually means!
ℹ️ Confusion among client developers:

Imagine you had to implement such a bulky interface, like above, although you only wanted to use a base class to implement employee reports.

Wouldn't you be confused that you also had to implement so much more?

I would be!
It's also a sign that the base class maybe tries to do too much.

Nevertheless, you are left with the need to actually do something about the methods you didn't intend to use at all.

And even the thoughts you have to put into thinking about what to do are unnecessary work!
ℹ️ Sprawling maintenance costs

Every time the interface changes, several classes implementing it need to change.

And in this case it doesn't even matter if they actually implemented all of the methods. They are forced to, if the client wants their code to compile or at least...
...run error-free at runtime.

This is a huge addition to maintenance cost!

And this is a driver of much unnecessary work for every client developer.

👇
3⃣ Fixing the issue

One way to fix the issue can now be to actually pull the interface apart, like shown below.

This is the extreme version of the ISP applied, role interfaces which all do exactly one thing. interface EmployeeReportSer...
With a little more thought, however, you can fix the issue even better, by generalizing the interface!

Take a look at the version below.

Any notion of specifics was removed due to the fact that we now have a pretty generic interface, which can be implemented in many ways.

👇 interface ReportService {  ...
4⃣ Should you care?

I'm pretty sure you already know the answer => Yes, you should!

Bulky interfaces can occur in any language, even in dynamically typed ones like JavaScript.

Although you don't have interfaces at hand, you still define, by the code you write, ...
... what you expect a client to pass to you.

Just imagine a function which takes a god object and uses several methods on that object to do its work.

Everyone using your function would have to put at least something into those object's methods to make your function work.
The ISP saves you and others from putting in a lot of unnecessary work into the implementation of unwanted dependencies.

And as with nearly any other principle associated with clean code, code becomes more readable, tests become better and more maintainable!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Oliver Jumpertz

Oliver Jumpertz Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @oliverjumpertz

22 Nov
Although I am not a data scientist by any means, I was recently asked what knowledge of math someone would need to be able to efficiently get into the field.

Well, I researched a little and came up with the following course track, starting with the basics.

🧵👇
Calculus 1 will cover a lot of the basics, relevant especially to optimization problems which are quite common in data science.

khanacademy.org/math/calculus-1
Calculus 2 will go even deeper and cover topics such as integration techniques and differential equations.

Especially integrals are pretty important for probability distributions and hypothesis testing!

khanacademy.org/math/calculus-2
Read 7 tweets
21 Nov
DIP - The Dependency Inversion Principle

The Dependency Inversion Principle is a part of SOLID, a mnemonic acronym which bundles a total of 5 design principles.

It is often associated with clean code.

But what exactly is it, is it important to you, should you even care?

🧵👇
1⃣ What does it state?

It states:

Modules that encapsulate high-level policy should not depend upon modules that implement details. Rather, both kinds of modules should depend upon abstractions.

This may sound a little complicated, but you can break it up, as follows:
1. High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces).

2. Abstractions should not depend on details. Details (concrete implementations) should depend on abstractions.
Read 17 tweets
14 Nov
Clean code is important, clean code helps others understand your code, but clean code is also pretty subjective!

I want to give you my perspective on it, drawing from years of experience leading teams of developers as a tech lead and working in teams.

🧵
1️⃣ Clean code is important

Clean code helps people understand code.

The more your code is structured according to the opinion of the majority of developers, the more likely it is that other developers will understand your code!
A few principles of clean code are:

- Use meaningful and understandable identifiers
- Use functions and keep them as short as possible
- You don’t need comments most of the time when your code is human readable
- Don’t repeat yourself (DRY)
- Keep it Simple, Stupid! (KISS)
Read 18 tweets
27 Oct
Have you ever had to process events?

If you have, have you ever thought about if the order of events really matters?

There is a huge difference in complexity whether the order of events processed matters or not.
Imagine the following task/problem:

You want to analyze application logs, because you're curious how an API that starts a long running background process behaves performance-wise.

One log message signals the start, a few intermediate ones, and one message that signals the end.
1⃣
If you can somehow ensure that every log message you process comes in order, the problem is manageable.

When you get your end message, you know that you already processed the start message.

You could now, for example, just subtract the end time from the start time.
Read 8 tweets
14 Aug
Interviewing for a new position is never easy.

Algorithms & Data Structures are usually a pain for candidates, but so can System Design be.

Let me give you 7 steps that will help you to improve your experience in system design interviews.

🧵👇
1) Clarify The Requirements

Ask questions, and a lot of them!

Try to find out what exactly the interviewer expects from you.

Try to narrow it down as far as possible to the exact scope of the problem.

Even if the question is to (re)design an existing system, ask!
2) Define System Interfaces

Define all APIs that the system to design will need.

Explain what each API is for as detailed as possible, so the interviewer can jump in and tell you if you got a requirement wrong.

If you got something wrong, no problem, adjust accordingly.
Read 8 tweets
13 Aug
Improving developer experience is always a good investment.

It's even one of the investments with the highest return, in my opinion!

Some things that help to improve DX:

🧵👇
1) Providing All Relevant Information Up Front

Ensure that all information necessary is present in a README.md within the project's repository.

It should be sufficient enough to state what the project does, what problems it tries to solve, etc.
2) Making Sure Starting Out Is Easy

Does the project require some setup to be able to run/test/develop locally? -> Write an interactive script which sets everything up, maybe creating a config file / .env with meaningful defaults or settings from script input.
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!