> “The advantage of loose coupling is the same in software design as it is in the physical socket and plug model:
> Once the infrastructure is in place,
> it can be used by anyone and adapted to changing needs and unforeseen requirements
> without requiring large changes to the application code base and infrastructure.”
[Dependency Injection by Steven van Deursen](https://www.amazon.com/Dependency-Injection-Principles-Practices-Patterns/dp/161729473X)
**Loose coupling** describes components that collaborate through small, stable contracts without depending on each other's internal implementation. The goal is not to eliminate dependencies, but to make them explicit and replaceable.
## Why is loose coupling important?
For one thing, it makes your code easier to maintain. Because the different parts of your code aren't tightly intertwined, you can make changes to one part without breaking everything else. It also makes your code more flexible. You can swap out one part for another without having to rewrite all of your code.
(Can you feel the sensation of déjà vu? I might have written about this topic in my blog before. Check it out and let me know!)
## how do you achieve loose coupling?
One approach is `dependency injection`: provide a collaborator to a function or object instead of constructing it inside the consumer.
Another approach is a small interface that describes the behavior the consumer needs without exposing implementation details.
Loose coupling is useful when a dependency is likely to vary or when isolating it materially improves testing. Applying interfaces everywhere can add indirection without value, so start from a concrete change or testability need.
“The advantage of loose coupling is the same in software design as it is in the physical socket and plug model:
Once the infrastructure is in place,
it can be used by anyone and adapted to changing needs and unforeseen requirements
without requiring large changes to the application code base and infrastructure.”
Loose coupling describes components that collaborate through small, stable contracts without depending on each other’s internal implementation. The goal is not to eliminate dependencies, but to make them explicit and replaceable.
Why is loose coupling important?
For one thing, it makes your code easier to maintain. Because the different parts of your code aren’t tightly intertwined, you can make changes to one part without breaking everything else. It also makes your code more flexible. You can swap out one part for another without having to rewrite all of your code.
(Can you feel the sensation of déjà vu? I might have written about this topic in my blog before. Check it out and let me know!)
how do you achieve loose coupling?
One approach is dependency injection: provide a collaborator to a function or object instead of constructing it inside the consumer.
Another approach is a small interface that describes the behavior the consumer needs without exposing implementation details.
Loose coupling is useful when a dependency is likely to vary or when isolating it materially improves testing. Applying interfaces everywhere can add indirection without value, so start from a concrete change or testability need.