Imagine every device manufacturer created their own plug that only they were allowed to manufacture. You'd need at least one cable for each device to charge. Gladly we have USB as a standard.

The Dependency Inversion Principle solves this issue for software.

🧵🔽
1️⃣ What does it state?

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.
Still not better?

Okay, view it this way:

Don't let anything you build depend on a concrete implementation of something. Better depend on an abstract description of a contract and let any implementor decide how to satisfy that contract.
2️⃣ An Example

Looking at examples can usually help to understand a concept better, so here's one. class PositionRepository {   constructor() {} }  class Trans
What you see here is one way of structuring a backend API. A resource layer that implements the specifics of the API, a service that contains the business logic, and repositories that handle the persistence layer (e.g. database interactions).
If you take a closer look, you can see that the high-level class (resource) depends on a concrete lower-level class (service) which itself depends on two (more) lower-level classes (repositories).
The control flow thus goes from left to right, from the highest to the lowest level. It is like a Singly Linked List or even better a Tree. Every higher-level module references at least one lower-level module.
3️⃣ What does it try to prevent?

Whenever you couple a module (a class, a function, a whole module) to another module, that first module depends on a lot of lower-level detail.
In object-oriented systems, this also means that you can only replace the logic you depend on by deriving from exactly this class and overwriting existing logic. This is pretty tight coupling and a lot of inflexibility.
It becomes pretty difficult to replace lower-level logic, and changes to the lower-level logic might have a larger impact on the higher-level one.
Just imagine you'd now restructure the TransactionService from the example. You couldn't tell, without taking an explicit look at the TransactionResource, that you perhaps now have to also make adjustments there.
Another issue is the direct dependency on the lower-level module. If you apply this example to a library, and class A depends on class B, and both originate from different modules, a user would always have a dependency on module A and module B (transitive).
A user couldn't even choose to not take module B, because the direct relationship and thus a dependency exists.
4️⃣ Fixing the issues

You can inverse the control flow by letting each concrete implementation either depend on an interface or implement said interface, as seen below. interface PositionRepository {}  interface TransactionReposi
Instead of the control flow going from the highest to the lowest level modules, there is now an abstraction in-between them.

Before you shout it out loud: yes, this adds more code overall, as each interface also needs to be described first.
But do you remember the issue with module dependencies?

Instead of module A now also requiring module B, module B now requires module A (given module A defines the interface), and this gives users the choice whether they want to pull in module B, as well, ...
... or want module C as an alternative to providing another implementation of the interface, which might be a better option for their use case.
5️⃣ Should you care?

Well, it depends.

In dynamically typed languages like JavaScript, you already depend on interfaces pretty much everywhere, as you can simply call any method or property on any object. As long as a user provides everything you require, you're good to go.
But as soon as static typing comes into play, you should indeed care. You gain all benefits described here, and it'll improve your code quality, usability, and maintainability a lot.

In the end, the benefits weigh pretty heavy, and should not be ignored.
6️⃣ Show Your Support

You can read this thread in the form of a blog post if that suits you better. You can find the link below.

blog.oliverjumpertz.dev/dip-the-depend…
And did you know that I have a newsletter where I share valuable insights, especially on how to upskill yourself?

You can read all past issues online, so no need to subscribe directly. Test it out first.

getrevue.co/profile/oliver…

• • •

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

14 Mar
Let's talk about how you can optimize your LinkedIn profile by using keywords.

This approach worked pretty well for me, and it might help you, too.

🧵🔽
1️⃣ Most Recruiters Search For Keywords

LinkedIn offers recruiters the chance to look for profiles of users by using keywords. If you ever had to search in Atlassian Jira, you know how that works approx.
If not, basically imagine it like this:

A recruiter can type in keywords and then use boolean logic, like ("engineer" OR "developer" OR "dev") AND "javascript" AND ("react" OR "vue").

LinkedIn will process this and then return a list of potential profiles for them.
Read 13 tweets
12 Mar
Just imagine that you need 20 different keys on your keyring to enter your home but only one of them is actually used to unlock the door.

The Interface Segregation Principle tries to solve this problem for software.

🧵🔽
1️⃣ What does it state?

The Interface Segregation Principle (ISP) sets a pretty simple statement:

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

The 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 (it's in TypeScript). interface ReportService {  ...
Read 18 tweets
5 Mar
Code Reviews are a great way to use the experience and knowledge of a team to create good software. Not all review processes are enjoyable, though.

Here are 6 tips to make your review process better and more enjoyable for everyone!

🧵🔽 Two developers sitting at a...
1️⃣ Make Expectations Clear

A good review process should both be standardized and individual. The terms of the process should be written down and transparent for everyone on the team. Each team member should know what is expected of them and all team members should agree ...
... on the terms. Changing the terms should always be a democratic process within the team. If the team disagrees on changes or terms, it should discuss the issues until there is consent.

On the other hand, the process should be flexible enough to allow for individuality.
Read 19 tweets
18 Feb
Proof-Of-Work is the name of a cryptographic algorithm that is used for some blockchains when new blocks are to be appended to the chain.

Let's take a higher-level look at how this one works, shall we?

🧵🔽
1️⃣ The Basics

This algorithm creates a system in which one party (the prover) has to prove to one or multiple other parties (the verifiers) that they put in a certain amount of work for some purpose.
The work the prover has to put in is moderately hard to very hard, while the verifiers can pretty easily check whether the proof is correct. This creates an asymmetric system.

The original idea was to create protection against DDoS attacks and spam.
Read 18 tweets
17 Feb
What are **some** possible applications of Blockchain technology?

Are there only cryptocurrencies? Are there more possible applications? Let's take a look at three possible applications outside of finance here to give you a general idea of what is possible.

🧵🔽
0️⃣ Foreword

A blockchain can be used to solve a lot of problems. Cryptocurrencies are only one possible way of applying the technology but there is way more to it.

We can take a look at some possible applications and what they each provide to users.
1️⃣ Social Networks

The current generation of social networks is centralized. Whether we talk about Facebook, Twitter, Instagram, TikTok, or whatever else. They all own your account, your content, your followers, and everything else.
Read 19 tweets
16 Feb
What actually is a Blockchain?

Bitcoin is breaking record after record, but there must be more to the technology than just crypto, or not? Well, we can take a look at the underlying technology first to understand what it actually provides to us.

🧵⬇️
1️⃣ Foreword

This will be a high-level overview to give you an intro to blockchain technology. With further threads, we will dive deeper and deeper, so don't be sad if something you really looked forward to isn't included as detailed as you would have wished for, yet!
2️⃣ Client-Server Architecture

Before we dive into blockchain technology, we should take a look at how most of the internet works. We need this to understand the fundamental differences between a traditional model and the change blockchain technology brings.
Read 29 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!