Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Balancing Coupling in Software Design (KanDDDin...

Balancing Coupling in Software Design (KanDDDinsky 2022)

We are used to treating coupling as the necessary evil. Hence, we aim to break systems apart into the smallest services possible, in the ever-lasting quest of decoupling everything. But what results are we getting by following this reasoning? — Instead of the promised land of evolvable systems, many such “decouple everything” endeavors lead straight into the chains of distributed monoliths. Can we do better? What if there is a better way to handle coupling in distributed systems? Or even, what if instead of fighting coupling, we can use it as a heuristic for designing systems that are actually evolvable and maintainable? That’s exactly what you will learn in this session: what coupling is, and how you can use it as a design tool. We will start by taking a journey through time, and explore the different models of evaluating coupling. Next, we will combine the existing knowledge into a coherent model — a simple function for evaluating a system’s decomposition into components. This function will allow you to assess the expected effort for evolving a system and use it as a heuristic for optimizing architectural design decisions.

Vladik Khononov

November 19, 2022
Tweet

More Decks by Vladik Khononov

Other Decks in Programming

Transcript

  1. vladikk doit-intl.com #KanDDDinsky “System design is inherently about boundaries (what’s

    in, what’s out, what spans, what moves between), and about tradeo ff s. It reshapes what is outside, just as it shapes what is inside.” 
 - Ruth Malan (@ruthmalan)
  2. vladikk doit-intl.com #KanDDDinsky “System design is inherently about boundaries (what’s

    in, what’s out, what spans, what moves between), and about tradeo ff s. It reshapes what is outside, just as it shapes what is inside.” 
 - Ruth Malan (@ruthmalan)
  3. vladikk doit-intl.com #KanDDDinsky 💩 💩 💩 💩 💩 💩 💩

    💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩 💩
  4. vladikk doit-intl.com #KanDDDinsky “"As small as possible”… is almost universally

    bad advice in so ft ware design. Some critical logic is going to cross those boundaries and result in poorly implemented, preventable workarounds” 
 - Mathias Verraes (@mathiasverraes)
  5. vladikk doit-intl.com #KanDDDinsky “There are many useful and revealing heuristics

    for defining the boundaries of a service. Size is one of the least useful.” 
 - Nick Tune (@ntcoding)
  6. vladikk doit-intl.com #KanDDDinsky AGENDA 1. WHAT IS COUPLING? 2. EXPLORE

    THE DIMENSIONS OF COUPLING 3. USE COUPLING AS A DESIGN TOOL / HEURISTIC
  7. vladikk doit-intl.com #KanDDDinsky co- 
 together apere 
 fasten copula

    
 copulare cople 
 copler couple LATIN LATIN OLD FRENCH MIDDLE ENGLISH
  8. vladikk doit-intl.com #KanDDDinsky co- 
 apere 
 copula 
 cople

    
 couple LATIN LATIN OLD FRENCH MIDDLE ENGLISH COUPLED = CONNECTED
  9. vladikk doit-intl.com #KanDDDinsky COUPLING Should only allow changes that we

    actually need Design decision Defines the relationship between components Limits components’ degrees of freedom
  10. vladikk doit-intl.com #KanDDDinsky 1. CONTENT COUPLING 2. COMMON COUPLING 3.

    EXTERNAL COUPLING 4. CONTROL COUPLING 5. STAMP COUPLING 6. DATA COUPLING
  11. vladikk doit-intl.com #KanDDDinsky 1. CONTENT COUPLING 2. COMMON COUPLING 3.

    EXTERNAL COUPLING 4. CONTROL COUPLING 5. STAMP COUPLING 6. DATA COUPLING • Directly referencing another module’s “contents” - implementation details • Private interfaces • “GO TO”
  12. vladikk doit-intl.com #KanDDDinsky 1. CONTENT COUPLING 2. COMMON COUPLING 3.

    EXTERNAL COUPLING 4. CONTROL COUPLING 5. STAMP COUPLING 6. DATA COUPLING • Communicate through globally accessible memory space • Unstructured global data item • Global byte array
  13. vladikk doit-intl.com #KanDDDinsky 1. CONTENT COUPLING 2. COMMON COUPLING 3.

    EXTERNAL COUPLING 4. CONTROL COUPLING 5. STAMP COUPLING 6. DATA COUPLING • Communicate through globally accessible primitive value or an array of primitive values • Global variable
  14. vladikk doit-intl.com #KanDDDinsky 1. CONTENT COUPLING 2. COMMON COUPLING 3.

    EXTERNAL COUPLING 4. CONTROL COUPLING 5. STAMP COUPLING 6. DATA COUPLING • A module controls execution logic of another module • Close familiarity with its behavior and execution context • The controlled module doesn’t have enough information (input) to control its own behavior
  15. vladikk doit-intl.com #KanDDDinsky 1. CONTENT COUPLING 2. COMMON COUPLING 3.

    EXTERNAL COUPLING 4. CONTROL COUPLING 5. STAMP COUPLING 6. DATA COUPLING • Modules communicate through exchanging data structures • Data structures are the modules’ implementation details • Sensitive to changes in the data structures
  16. vladikk doit-intl.com #KanDDDinsky 1. CONTENT COUPLING 2. COMMON COUPLING 3.

    EXTERNAL COUPLING 4. CONTROL COUPLING 5. STAMP COUPLING 6. DATA COUPLING • Modules communicate through exchanging primitive types • Communicating by sharing minimum information • Integration specific data
  17. vladikk doit-intl.com #KanDDDinsky Referencing implementation details Globally accessible memory Global

    variable Controlling behavior Exchanging data records Minimizing integration interface CONTENT COUPLING: COMMON COUPLING: EXTERNAL COUPLING: CONTROL COUPLING: STAMP COUPLING: DATA COUPLING:
  18. vladikk doit-intl.com #KanDDDinsky CONNASCENCE “Having been born together” (Latin) Components

    are connnascent (born together) if: • A change in one component, requires a change in another component • You can postulate a change that would require both components to change
  19. vladikk doit-intl.com #KanDDDinsky STATIC CONNASCENCE: 1. Name 2. Type 3.

    Meaning 4. Algorithm 5. Position DYNAMIC CONNASCENCE: 1. Execution 2. Timing 3. Value 4. Identity
  20. vladikk doit-intl.com #KanDDDinsky STATIC CONNASCENCE: 1. NAME 2. Type 3.

    Meaning 4. Algorithm 5. Position • Two variables reference the same thing • Need to have the same name
  21. vladikk doit-intl.com #KanDDDinsky STATIC CONNASCENCE: 1. Name 2. TYPE 3.

    Meaning 4. Algorithm 5. Position • Types of two variables have to match
  22. vladikk doit-intl.com #KanDDDinsky STATIC CONNASCENCE: 1. Name 2. Type 3.

    MEANING 4. Algorithm 5. Position • Conventions used to assign meaning to values • “Magic numbers” • int status = 7;
  23. vladikk doit-intl.com #KanDDDinsky STATIC CONNASCENCE: 1. Name 2. Type 3.

    Meaning 4. ALGORITHM 5. Position • Same algorithm is applied in multiple modules • Assume expected behaviour • E.g. encoding, encryption
  24. vladikk doit-intl.com #KanDDDinsky STATIC CONNASCENCE: 1. Name 2. Type 3.

    Meaning 4. Algorithm 5. POSITION • Order of passed values
  25. vladikk doit-intl.com #KanDDDinsky STATIC CONNASCENCE: Name Type Meaning Algorithm Position

    Two variables need to have the same name Types have to match Conventions for assinging meaning to values A common algorithm is used or assumed Values must be passed in a specific order
  26. vladikk doit-intl.com #KanDDDinsky DYNAMIC CONNASCENCE: 1. EXECUTION 2. Timing 3.

    Value 4. Identity • Execution must be carried out in a given order • No intervening execution • Dynamic equivalent of connascence of position
  27. vladikk doit-intl.com #KanDDDinsky DYNAMIC CONNASCENCE: 1. Execution 2. TIMING 3.

    Value 4. Identity • Execution in a set amount of time • E.g.: Timeouting a request • E.g.: Turning off an X-Ray machine
  28. vladikk doit-intl.com #KanDDDinsky DYNAMIC CONNASCENCE: 1. Execution 2. Timing 3.

    VALUE 4. Identity • Values have to change together • Logical transaction • E.g.: Arithmetic constraint • E.g.: Replicated data • E.g.: Business invariant
  29. vladikk doit-intl.com #KanDDDinsky DYNAMIC CONNASCENCE: 1. Execution 2. Timing 3.

    Value 4. IDENTITY • Two objects must reference the same instance of another object • E.g.: Unit of work
  30. vladikk doit-intl.com #KanDDDinsky DYNAMIC CONNASCENCE: Execution Timing Value Identity Execution

    in a given order Execution after a set amount of time Values have to change together Reference the same instance of an object
  31. vladikk doit-intl.com #KanDDDinsky Connascence of Identity Connascence of Value Connascence

    of Timing Connascence of Execution Connascence of Position Connascence of Algorithm Connascence of Meaning Connascence of Type Connascence of Name Content Coupling Common Coupling External Coupling Control Coupling Stamp Coupling Data Coupling Strength Refactor
  32. vladikk doit-intl.com #KanDDDinsky • 1960s - Coupling in Structured Design

    • 1990s - Connascence • 2020s - Integration Strength COUPLING MODELS
  33. vladikk doit-intl.com #KanDDDinsky • Remix of structured design and connascence

    models • Focuses on the knowledge shared between the connected components • Practical applicability INTEGRATION STRENGTH
  34. vladikk doit-intl.com #KanDDDinsky INTEGRATION STRENGTH 1. IMPLEMENTATION COUPLING 2. Functional

    Coupling 3. Model Coupling 4. Contract Coupling • Dependence on implementation details • Integration through private interfaces • Fragile • Implicit
  35. vladikk doit-intl.com #KanDDDinsky INTEGRATION STRENGTH 1. Implementation Coupling 2. FUNCTIONAL

    COUPLING 3. Model Coupling 4. Contract Coupling • Functionally related • Implementing closely-related business functionality • Change in business requirements affect both components • Don’t have to be physically integrated!
  36. vladikk doit-intl.com #KanDDDinsky INTEGRATION STRENGTH 1. Implementation Coupling 2. Functional

    Coupling 3. MODEL COUPLING 4. Contract Coupling • Sharing a model of the business domain • Structures • Hinders evolution of the shared model
  37. vladikk doit-intl.com #KanDDDinsky INTEGRATION STRENGTH 1. Implementation Coupling 2. Functional

    Coupling 3. Model coupling 4. CONTRACT COUPLING • Explicit integration contract • Integration-specific model • Decoupled from the implementation model
  38. vladikk doit-intl.com #KanDDDinsky Strength Shared Knowledge INTEGRATION STRENGTH Implementation Coupling

    Functional Coupling Model Coupling Contract Coupling Implicit 
 Interfaces Explicit 
 Interfaces
  39. vladikk doit-intl.com #KanDDDinsky DISTANCE Statem ents M ethods Classes Nam

    espaces Com ponents (M icro)Services System s Coordination
  40. vladikk doit-intl.com #KanDDDinsky DISTANCE Statem ents M ethods Classes Nam

    espaces Com ponents (M icro)Services System s Coordination
  41. vladikk doit-intl.com #KanDDDinsky DISTANCE Statem ents M ethods Classes Nam

    espaces Com ponents (M icro)Services System s Coordination Lifecycle 
 Coupling
  42. vladikk doit-intl.com #KanDDDinsky • Defines the components’ lifecycle coupling •

    Sets the probability of “collateral” cascading changes • A ff ected by the type of encapsulation boundaries • Can also be a ff ected by the organizational structure DISTANCE
  43. vladikk doit-intl.com #KanDDDinsky DISTANCE Statem ents M ethods Classes Nam

    espaces Com ponents (M icro)Services Coordination Lifecycle 
 Coupling
  44. vladikk doit-intl.com #KanDDDinsky DISTANCE Statem ents M ethods Classes Nam

    espaces Com ponents (M icro)Services (M icro)Services Coordination Lifecycle 
 Coupling One 
 Team Multiple 
 Teams
  45. vladikk doit-intl.com #KanDDDinsky • Subdomain is an area of activity

    required for the company to achieve its goals • Business building blocks • Coherent set of interrelated use cases SUBDOMAINS
  46. vladikk doit-intl.com #KanDDDinsky SUBDOMAINS Supporting CORE Generic • The company’s

    competitive advantage • What it does differently from its competitors • New invention or optimizing existing processes • How it makes money (or hopes to)
  47. vladikk doit-intl.com #KanDDDinsky SUBDOMAINS Supporting Core GENERIC • “Solved problems”

    • Things all companies are doing in the same way • Cheaper to buy / adopt than to build in-house
  48. vladikk doit-intl.com #KanDDDinsky SUBDOMAINS SUPPORTING Core Generic • Implemented in-house

    • Provide no competitive advantage • Needed by Core Subdomains • No innovation, optimization, or intellectual property
  49. vladikk doit-intl.com #KanDDDinsky Strength increases cascading changes High Strength +

    High Volatility = High Maintenance E ff ort STRENGTH & VOLATILITY
  50. vladikk doit-intl.com #KanDDDinsky Strength increases cascading changes Distance increases coordination

    overhead High Strength over High Distance = High Cost of Evolving the System STRENGTH & DISTANCE
  51. vladikk doit-intl.com #KanDDDinsky Distance increases coordination overhead High Volatility over

    High Distance = High Coordination E ff ort DISTANCE & VOLATILITY
  52. vladikk doit-intl.com #KanDDDinsky Distance Strength Volatility Pain = 𝑓 (S,

    V, D) Maintenance Pain STRENGTH & DISTANCE & VOLATILITY
  53. vladikk doit-intl.com #KanDDDinsky Pain = 𝑓 (S, V, D) STRENGTH

    & DISTANCE & VOLATILITY Pain = Strength * Volatility * Distance
  54. vladikk doit-intl.com #KanDDDinsky PAIN = 𝑓 (S, V, D) Pain

    = Strength * Volatility * Distance 
 0 = 0 * ∞ * ∞
  55. vladikk doit-intl.com #KanDDDinsky PAIN = 𝑓 (S, V, D) Pain

    = Strength * Volatility * Distance 
 0 = ∞ * ∞ * 0
  56. vladikk doit-intl.com #KanDDDinsky PAIN = 𝑓 (S, V, D) Pain

    = Strength * Volatility * Distance 
 0 = ∞ * 0 * ∞
  57. vladikk doit-intl.com #KanDDDinsky 1. Eliminate accidental coupling 2. Reduce integration

    strength as much as possible 3. If strength and volatility is high: reduce distance PAIN = S * V * D
  58. vladikk doit-intl.com #KanDDDinsky EXAMPLE: INTEGRATION STRENGTH IMPLEMENTATION COUPLING Functional Coupling

    Model Coupling Contract Coupling Component A 
 Component B Not intended 
 for integration
  59. vladikk doit-intl.com #KanDDDinsky EXAMPLE: INTEGRATION STRENGTH Implementation Coupling FUNCTIONAL COUPLING

    Model Coupling Contract Coupling Component A 
 Component B Have to operate on the same data
  60. vladikk doit-intl.com #KanDDDinsky EXAMPLE: INTEGRATION STRENGTH Implementation Coupling Model Coupling

    MODEL COUPLING Contract Coupling Component A 
 Component B BI imports data for analysis
  61. vladikk doit-intl.com #KanDDDinsky EXAMPLE: INTEGRATION STRENGTH Implementation Coupling Functional Coupling

    Model Coupling CONTRACT COUPLING Component A 
 Component B Dedicated integration DB
  62. vladikk doit-intl.com #KanDDDinsky • Defines the relationship between a system’s

    components • Is an inherent part of system design • Happens in 3 dimensions: strength, volatility, distance WRAP UP: COUPLING
  63. vladikk doit-intl.com #KanDDDinsky • The coordination e ff ort and

    cost of a change is proportional to the distance between the a ff ected components • Couples components’ lifecycles WRAP UP: DISTANCE
  64. vladikk doit-intl.com #KanDDDinsky WRAP UP: VOLATILITY • How often do

    the coupled components have to change? • Affects the actual effort • Can be estimated using domain-driven design’s subdomains: Core (high), Supporting (low), Generic (low)
  65. vladikk doit-intl.com #KanDDDinsky Distance Strength Volatility Pain = 𝑓 (S,

    V, D) Maintenance Pain WRAP UP: BALANCING COUPLING