liveBook · Manning

Coupling

As explained in Practices of the Python Pro

Loose coupling is a design property in which software components have limited interdependence.

Loose coupling is a design property in which software components interact without relying heavily on one another’s internal details. It lets different areas change with less risk of breaking surrounding code. section 1.4.1 section 10.1.3

Think of coupling as a flexible mesh between pieces of code. A tightly woven section forces neighboring pieces to move together; a looser section permits change. Coupling is a continuum, not an all-or-nothing condition. section 10.1.1

Reduce coupling by making responsibilities and interactions easier to change:

  • Separate concerns so presentation, business logic, and persistence can evolve independently. section 10.3
  • Encapsulate data and behavior, then expose a focused shared abstraction instead of requiring callers to know implementation details. section 10.1.3 section 10.5
  • Prefer configuration data for simple mappings when conditional logic would entangle choices with outcomes. section 7.1.2
  • Recognize direct dependencies that would force shotgun surgery when adding behavior or replacing an implementation. section 7.1.3

Expose one message instead of several implementation steps

A caller that cleans a query by invoking remove_spaces, remove_quotes, and normalize must know the cleaning sequence. A focused clean_query function hides those details and gives callers one stable interaction: clean_query(query). The same principle appears in Bark, where menu options trigger commands through a shared execute abstraction rather than depending on each command’s implementation. section 10.1.3 section 10.3

Listing 10.6. Simplifying a shared interface
import re


def _remove_spaces(query):                             #1
    query = query.strip()
    query = re.sub(r'\s+', ' ', query)
    return query


def _normalize(query):
    query = query.casefold()
    return query


def _remove_quotes(query):
    query = re.sub(r'"', '', query)
    return query


def clean_query(query):                                #2
    query = _remove_spaces(query)
    query = _remove_quotes(query)
    query = _normalize(query)
    return query


if __name__ == '__main__':
    search_query = input('Enter your search query: ')
    search_query = clean_query(search_query)           #3
    print(f'Running a search for "{search_query}"')

Inspect the compact Python example that wraps several query transformations behind the clean_query interface. section 10.1.3

How Practices of the Python Pro covers it

How deep it goes
Explains coupling as a continuum, contrasts rigid and flexible designs, and applies interfaces, encapsulation, and separated concerns.
Examples
Uses conditional mappings, query cleaning, and Bark’s commands, database layer, menu, and multitier architecture.
What it assumes
Assumes basic familiarity with software components and teaches coupling through analogies, Python examples, interfaces, and commands.
What you can do after
Apply it by separating concerns, hiding implementation details, and introducing shared abstractions between collaborating components.
How it connects
Connects loose coupling to extensibility, interfaces, the command pattern, separation of concerns, encapsulation, and multitier architecture.

Also explained in

Compare how 17 books explain coupling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.