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):
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.
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.
👇
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
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.
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.
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)
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.