# Loose Coupling

## What is Loose Coupling

This is what happens when you have tight coupling: you change a function's internals, then you need to look up all the places where that function is used and that leads you to changing the clients of that function.

This happens because the client code is tightly coupled to the function you modified.

To avoid having to make all those changes you need to make the client and the function loosely coupled.

Many good coding techniques like design principles and patterns are made to avoid tight coupling of code. Design principles and patterns are used everywhere not just in programming. For example, once you learn how to drive a car you can drive any car. Or, you go to the same postal service branch for sending all kinds of letters or packages. Imagine how painful it would be if every couple of years you'd have re-learn driving a car because a manufacturer has "improved" the internals of the car.

With loose coupling the goal is to "code to interfaces not implementation". This means that ideally the client of a function should know only the signature of that function. Changes in how that function does what it does should affect its clients.

## Loose Coupling with Python Decorators

Take the implementation of decorators in Python. Python's ingenious design and implementation of functions in the language enables you to write code that doesn't even depend on the signature of a function. This is why decorators are so flexible in Python.

A decorator does not know the name of the function it decorates and the parameters it takes. The function being decorated knows nothing about it. All these are reasons why decorators are such a great tool for writing loosely coupled code in Python.

For some amazing use of decorators take a look at [Flask](http://flask.pocoo.org/) and [pytest](https://docs.pytest.org/en/latest/).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mikaeilorfanian.gitbook.io/design-patterns-with-examples/loose-coupling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
