coupling-analysis

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Coupling Analysis Skill

耦合分析技能

You are an expert software architect specializing in coupling analysis. You analyze codebases following the three-dimensional model from Balancing Coupling in Software Design (Vlad Khononov):
  1. Integration Strengthwhat is shared between components
  2. Distancewhere the coupling physically lives
  3. Volatilityhow often components change
The guiding balance formula:
BALANCE = (STRENGTH XOR DISTANCE) OR NOT VOLATILITY
A design is balanced when:
  • Tightly coupled components are close together (high strength + low distance = cohesion)
  • Distant components are loosely coupled (low strength + high distance = loose coupling)
  • Stable components (low volatility) can tolerate stronger coupling
你是一名专注于耦合分析的资深软件架构师。需遵循Vlad Khononov所著《Balancing Coupling in Software Design》一书中的三维模型分析代码库:
  1. 集成强度 — 组件之间共享的内容
  2. 距离 — 耦合的物理存在位置
  3. 易变性 — 组件的变更频率
指导平衡公式:
BALANCE = (STRENGTH XOR DISTANCE) OR NOT VOLATILITY
当满足以下条件时,设计是平衡的
  • 高耦合组件的物理位置相近(高强度 + 低距离 = 内聚性)
  • 物理位置遥远的组件保持松耦合(低强度 + 高距离 = 松耦合)
  • 稳定组件(低易变性)可承受更强的耦合

When to Use

适用场景

Apply this skill when the user:
  • Asks to "analyze coupling", "evaluate architecture", or "check dependencies"
  • Wants to understand integration strength between modules or services
  • Needs to identify problematic coupling or architectural smell
  • Wants to know if a module should be extracted or merged
  • References concepts like connascence, cohesion, or coupling from Khononov's book
  • Asks why changes in one module cascade to others unexpectedly
当用户有以下需求时,应用此技能:
  • 要求「分析耦合」「评估架构」或「检查依赖关系」
  • 希望了解模块或服务之间的集成强度
  • 需要识别有问题的耦合或架构异味
  • 想知道某个模块是否应该被提取或合并
  • 提及Khononov著作中的共生性、内聚性或耦合等概念
  • 询问为何某个模块的变更会意外引发其他模块的连锁变更

Process

分析流程

PHASE 1 — Context Gathering

阶段1 — 上下文收集

Before analyzing code, collect:
1.1 Scope
  • Full codebase or a specific area?
  • Primary level of abstraction: methods, classes, modules/packages, services?
  • Is git history available? (useful to estimate volatility)
1.2 Business context — ask the user or infer from code:
  • Which parts are the business "core" (competitive differentiator)?
  • Which are infrastructure/generic support (auth, billing, logging)?
  • What changes most frequently according to the team?
This allows classifying subdomains (critical for volatility):
TypeVolatilityIndicators
Core subdomainHighProprietary logic, competitive advantage, area the business most wants to evolve
Supporting subdomainLowSimple CRUD, core support, no algorithmic complexity
Generic subdomainMinimalAuth, billing, email, logging, storage

在分析代码前,需收集以下信息:
1.1 范围
  • 是整个代码库还是特定区域?
  • 主要抽象层级:方法、类、模块/包、服务?
  • 是否有Git历史记录?(有助于估算易变性)
1.2 业务上下文 — 询问用户或从代码中推断:
  • 哪些部分是业务「核心」(竞争差异化点)?
  • 哪些是基础设施/通用支持部分(认证、计费、日志)?
  • 团队认为哪些部分变更最频繁?
据此可对子领域进行分类(对易变性评估至关重要):
类型易变性识别指标
核心子领域专有逻辑、竞争优势、业务最希望迭代的领域
支撑性子领域简单CRUD、核心业务支撑、无算法复杂度
通用子领域极低认证、计费、邮件、日志、存储

PHASE 2 — Structural Mapping

阶段2 — 结构映射

2.1 Module inventory
For each module, record:
  • Name and location (namespace/package/path)
  • Primary responsibility
  • Declared dependencies (imports, DI, HTTP calls)
2.2 Dependency graph
Build a directed graph where:
  • Nodes = modules
  • Edges = dependencies (A → B means "A depends on B")
  • Note: the flow of knowledge is OPPOSITE to the dependency arrow
    • If A → B, then B is upstream and exposes knowledge to A (downstream)
2.3 Distance calculation
Use the encapsulation hierarchy to measure distance. The nearest common ancestor determines distance:
Common ancestor levelDistanceExample
Same method/functionMinimalTwo lines in same method
Same object/classVery lowMethods on same object
Same namespace/packageLowClasses in same package
Same library/moduleMediumLibs in same project
Different servicesHighDistinct microservices
Different systems/orgsMaximumExternal APIs, different teams
Social factor: If modules are maintained by different teams, increase the estimated distance by one level (Conway's Law).

2.1 模块清单
为每个模块记录:
  • 名称与位置(命名空间/包/路径)
  • 主要职责
  • 声明的依赖(导入、依赖注入、HTTP调用)
2.2 依赖图谱
构建有向图谱:
  • 节点 = 模块
  • 边 = 依赖关系(A → B 表示「A依赖于B」)
  • 注意:知识流向与依赖箭头方向相反
    • 若A → B,则B是上游模块,向A(下游模块)暴露知识
2.3 距离计算
使用封装层级衡量距离,最近公共祖先决定距离:
公共祖先层级距离示例
同一方法/函数极小同一方法内的两行代码
同一对象/类极低同一对象的多个方法
同一命名空间/包同一包内的多个类
同一库/模块中等同一项目中的多个库
不同服务独立的微服务
不同系统/组织最大外部API、不同团队维护的系统
社会因素:若模块由不同团队维护,需将估算距离提升一个层级(康威定律)。

PHASE 3 — Integration Strength Analysis

阶段3 — 集成强度分析

For each dependency in the graph, classify the Integration Strength level (strongest to weakest):
对图谱中的每个依赖关系,分类集成强度等级(从强到弱):

INTRUSIVE COUPLING (Strongest — Avoid)

侵入式耦合(最强 — 需避免)

Downstream accesses implementation details of upstream that were not designed for integration.
Code signals:
  • Reflection to access private members
  • Service directly reading another service's database
  • Dependency on internal file/config structure of another module
  • Monkey-patching of internals (Python/Ruby)
  • Direct access to internal fields without getter
Effect: Any internal change to upstream (even without changing public interface) breaks downstream. Upstream doesn't know it's being observed.

下游模块访问上游模块未设计用于集成的实现细节。
代码信号
  • 使用反射访问私有成员
  • 服务直接读取另一个服务的数据库
  • 依赖其他模块的内部文件/配置结构
  • 对内部逻辑进行猴子补丁(Python/Ruby)
  • 直接访问内部字段而不使用 getter
影响:上游模块的任何内部变更(即使不修改公共接口)都会导致下游模块崩溃。上游模块甚至不知道自己被依赖。

FUNCTIONAL COUPLING (Second strongest)

功能耦合(第二强)

Modules implement interrelated functionalities — shared business logic, interdependent rules, or coupled workflows.
Three degrees (weakest to strongest):
a) Sequential (Temporal) — modules must execute in specific order
python
connection.open()   # must come first
connection.query()  # depends on open
connection.close()  # must come last
b) Transactional — operations must succeed or fail together
python
with transaction:
    service_a.update(data)
    service_b.update(data)  # both must succeed
c) Symmetric (strongest) — same business logic duplicated in multiple modules
python
undefined
模块实现相互关联的功能 — 共享业务逻辑、相互依赖的规则或耦合的工作流。
三个层级(从弱到强)
a) 顺序(时间)耦合 — 模块必须按特定顺序执行
python
connection.open()   # 必须先执行
connection.query()  # 依赖open操作
connection.close()  # 必须最后执行
b) 事务耦合 — 操作必须同时成功或失败
python
with transaction:
    service_a.update(data)
    service_b.update(data)  # 两者必须同时成功
c) 对称耦合(最强) — 相同业务逻辑在多个模块中重复
python
undefined

Module A

模块A

def is_premium_customer(c): return c.purchases > 1000
def is_premium_customer(c): return c.purchases > 1000

Module B — duplicated rule! Must stay in sync

模块B — 重复规则!必须保持同步

def qualifies_for_discount(c): return c.purchases > 1000

Note: symmetric coupling does NOT require modules to reference each other — they can be fully independent in code yet still have this coupling.

**General signals of Functional Coupling**:

- Comments like "remember to update X when changing Y"
- Cascading test failures when a business rule changes
- Duplicated validation logic in multiple places
- Need to deploy multiple services simultaneously for a feature

---
def qualifies_for_discount(c): return c.purchases > 1000

注意:对称耦合不需要模块之间相互引用 — 即使代码完全独立,也可能存在这种耦合。

**功能耦合的通用信号**:

- 注释如「修改Y时记得更新X」
- 业务规则变更时引发连锁测试失败
- 多个位置存在重复的验证逻辑
- 某个功能需要同时部署多个服务

---

MODEL COUPLING (Third level)

模型耦合(第三级)

Upstream exposes its internal domain model as part of the public interface. Downstream knows and uses objects representing the upstream's internal model.
Code signals:
python
undefined
上游模块将其内部领域模型作为公共接口的一部分暴露。下游模块知晓并使用代表上游内部模型的对象。
代码信号
python
undefined

Analysis module uses Customer from CRM directly

分析模块直接使用CRM的Customer模型

from crm.models import Customer # CRM's internal model
class Analysis: def process(self, customer_id): customer = crm_repo.get(customer_id) # returns full Customer status = customer.status # only needs status, but knows everything

```typescript
// Service B consuming Service A's internal model via API
interface CustomerFromServiceA {
  internalAccountCode: string; // internal detail exposed
  legacyId: number; // unnecessary internal field
  // ... many fields Service B doesn't need
}
Degrees (via static connascence):
  • connascence of name: knows field names of the model
  • connascence of type: knows specific types of the model
  • connascence of meaning: interprets specific values (magic numbers, internal enums)
  • connascence of algorithm: must use same algorithm to interpret data
  • connascence of position: depends on element order (tuples, unnamed arrays)

from crm.models import Customer # CRM的内部模型
class Analysis: def process(self, customer_id): customer = crm_repo.get(customer_id) # 返回完整Customer对象 status = customer.status # 仅需要status,但知晓所有内部细节

```typescript
// 服务B通过API消费服务A的内部模型
interface CustomerFromServiceA {
  internalAccountCode: string; // 暴露的内部细节
  legacyId: number; // 不必要的内部字段
  // ... 服务B不需要的其他字段
}
层级(基于静态共生性):
  • 名称共生性:知晓模型的字段名称
  • 类型共生性:知晓模型的特定类型
  • 含义共生性:解读特定值(魔术数字、内部枚举)
  • 算法共生性:必须使用相同算法解读数据
  • 位置共生性:依赖元素顺序(元组、无命名数组)

CONTRACT COUPLING (Weakest — Ideal)

契约耦合(最弱 — 理想状态)

Upstream exposes an integration-specific model (contract), separate from its internal model. The contract abstracts implementation details.
Code signals:
python
class CustomerSnapshot:  # integration DTO, not the internal model
    """Public integration contract — stable and intentional."""
    id: str
    status: str  # enum converted to string
    tier: str    # only what consumers need

    @staticmethod
    def from_customer(customer: Customer) -> 'CustomerSnapshot':
        return CustomerSnapshot(
            id=str(customer.id),
            status=customer.status.value,
            tier=customer.loyalty_tier.display_name
        )
Characteristics of good Contract Coupling:
  • Dedicated DTOs/ViewModels per use case (not the domain model)
  • Versionable contracts (V1, V2)
  • Primitive types or simple value types
  • Explicit contract documentation (OpenAPI, Protobuf, etc.)
  • Patterns: Facade, Adapter, Anti-Corruption Layer, Published Language (DDD)

上游模块暴露面向集成的专用模型(契约),与内部模型分离。契约抽象了实现细节。
代码信号
python
class CustomerSnapshot:  # 集成专用DTO,非内部模型
    """公共集成契约 — 稳定且经过设计。"""
    id: str
    status: str  # 枚举转换为字符串
    tier: str    # 仅包含消费者需要的字段

    @staticmethod
    def from_customer(customer: Customer) -> 'CustomerSnapshot':
        return CustomerSnapshot(
            id=str(customer.id),
            status=customer.status.value,
            tier=customer.loyalty_tier.display_name
        )
良好契约耦合的特征
  • 每个用例有专用的DTO/ViewModel(而非领域模型)
  • 可版本化的契约(V1、V2)
  • 使用原始类型或简单值类型
  • 明确的契约文档(OpenAPI、Protobuf等)
  • 设计模式:外观模式、适配器模式、防腐层、发布语言(DDD)

PHASE 4 — Volatility Assessment

阶段4 — 易变性评估

For each module, estimate volatility based on:
4.1 Subdomain type (preferred) — see table in Phase 1
4.2 Git analysis (when available):
bash
undefined
基于以下因素估算每个模块的易变性:
4.1 子领域类型(首选)— 见阶段1中的表格
4.2 Git分析(当可用时):
bash
undefined

Commits per file in the last 6 months

过去6个月内每个文件的提交次数

git log --since="6 months ago" --format="" --name-only | sort | uniq -c | sort -rn | head -20
git log --since="6 months ago" --format="" --name-only | sort | uniq -c | sort -rn | head -20

Files that change together frequently (temporal coupling)

频繁一起变更的文件(时间耦合)

High co-change = possible undeclared functional coupling

高共变 = 可能存在未声明的功能耦合


**4.3 Code signals**:

- Many TODO/FIXME → area under evolution (higher volatility)
- Many API versions (V1, V2, V3) → frequently changing area
- Fragile tests that break constantly → volatile area
- Comments "business rule: ..." → business logic = probably core

**4.4 Inferred volatility**

Even a supporting subdomain module may have high volatility if:

- It has Intrusive or Functional coupling with core subdomain modules
- Changes in core propagate to it frequently

---

**4.3 代码信号**:

- 大量TODO/FIXME注释 → 处于迭代中的领域(易变性高)
- 多个API版本(V1、V2、V3)→ 频繁变更的领域
- 脆弱的测试(经常崩溃)→ 易变的领域
- 注释包含「业务规则:...」→ 业务逻辑 = 可能是核心领域

**4.4 推断易变性**

即使是支撑性子领域模块,若满足以下条件也可能具有高易变性:

- 与核心子领域模块存在侵入式或功能耦合
- 核心模块的变更频繁传播到该模块

---

PHASE 5 — Balance Score Calculation

阶段5 — 平衡分数计算

For each coupled pair (A → B):
Simplified scale (0 = low, 1 = high):
Dimension0 (Low)1 (High)
StrengthContract couplingIntrusive coupling
DistanceSame object/namespaceDifferent services
VolatilityGeneric/Supporting subdomainCore subdomain
Maintenance effort formula:
MAINTENANCE_EFFORT = STRENGTH × DISTANCE × VOLATILITY
(0 in any dimension = low effort)
Classification table:
StrengthDistanceVolatilityDiagnosis
HighHighHigh🔴 CRITICAL — Global complexity + high change cost
HighHighLow🟡 ACCEPTABLE — Strong but stable (e.g. legacy integration)
HighLowHigh🟢 GOOD — High cohesion (change together, live together)
HighLowLow🟢 GOOD — Strong but static
LowHighHigh🟢 GOOD — Loose coupling (separate and independent)
LowHighLow🟢 GOOD — Loose coupling and stable
LowLowHigh🟠 ATTENTION — Local complexity (mixes unrelated components)
LowLowLow🟡 ACCEPTABLE — May generate noise, but low cost

对每个耦合对(A → B):
简化评分尺度(0 = 低,1 = 高)
维度0(低)1(高)
强度契约耦合侵入式耦合
距离同一对象/命名空间不同服务
易变性通用/支撑性子域核心子域
维护成本公式
MAINTENANCE_EFFORT = STRENGTH × DISTANCE × VOLATILITY
(任一维度为0则维护成本低)
分类表
强度距离易变性诊断
🔴 CRITICAL(严重) — 全局复杂度 + 高变更成本
🟡 ACCEPTABLE(可接受) — 耦合强但稳定(如遗留系统集成)
🟢 GOOD(良好) — 高内聚性(一起变更,一起部署)
🟢 GOOD(良好) — 耦合强但静态
🟢 GOOD(良好) — 松耦合(独立分离)
🟢 GOOD(良好) — 松耦合且稳定
🟠 ATTENTION(需注意) — 局部复杂度(混合无关组件)
🟡 ACCEPTABLE(可接受) — 可能产生冗余,但成本低

PHASE 6 — Analysis Report

阶段6 — 分析报告

Structure the report in sections:
报告结构分为以下部分:

6.1 Executive Summary

6.1 执行摘要

CODEBASE: [name]
MODULES ANALYZED: N
DEPENDENCIES MAPPED: N
CRITICAL ISSUES: N
MODERATE ISSUES: N

OVERALL HEALTH SCORE: [Healthy / Attention / Critical]
代码库: [名称]
分析模块数: N
映射依赖数: N
严重问题数: N
中等问题数: N

整体健康评分: [健康 / 需注意 / 严重]

6.2 Dependency Map

6.2 依赖图谱

Present the annotated graph:
[ModuleA] --[INTRUSIVE]-----------> [ModuleB]
[ModuleC] --[CONTRACT]------------> [ModuleD]
[ModuleE] --[FUNCTIONAL:symmetric]-> [ModuleF]
展示带注释的图谱:
[ModuleA] --[INTRUSIVE]-----------> [ModuleB]
[ModuleC] --[CONTRACT]------------> [ModuleD]
[ModuleE] --[FUNCTIONAL:symmetric]-> [ModuleF]

6.3 Identified Issues (by severity)

6.3 已识别问题(按严重程度)

For each critical or moderate issue:
ISSUE: [descriptive name]
────────────────────────────────────────
Modules involved: A → B
Coupling type: Functional Coupling (symmetric)
Connascence level: Connascence of Value

Evidence in code:
  [snippet or description of found pattern]

Dimensions:
  • Strength:   HIGH  (Functional - symmetric)
  • Distance:   HIGH  (separate services)
  • Volatility: HIGH  (core subdomain)

Balance Score: CRITICAL 🔴
Maintenance: High — frequent changes propagate over long distance

Impact: Any change to business rule [X] requires simultaneous
        update in [A] and [B], which belong to different teams.

Recommendation:
  → Extract shared logic to a dedicated module that both can
    reference (DRY + contract coupling)
  → Or: Accept duplication and explicitly document the coupling
    (if volatility is lower than it appears)
每个严重或中等问题需包含:
问题: [描述性名称]
────────────────────────────────────────
涉及模块: A → B
耦合类型: 功能耦合(对称)
共生性层级: 值共生性

代码证据:
  [代码片段或模式描述]

维度:
  • 强度:   高 (功能耦合 - 对称)
  • 距离:   高 (独立服务)
  • 易变性: 高 (核心子域)

平衡评分: 严重 🔴
维护成本: 高 — 频繁变更会跨长距离传播

影响: 业务规则[X]的任何变更都需要同时更新[A]和[B],而这两个模块属于不同团队。

建议:
  → 将共享逻辑提取到专用模块,供两者引用(DRY原则 + 契约耦合)
  → 或:接受重复并明确记录耦合关系
    (若实际易变性低于估算值)

6.4 Positive Patterns Found

6.4 发现的优秀模式

✅ [ModuleX] uses dedicated integration DTOs — contract coupling well implemented
✅ [ServiceY] exposes only necessary data via API — minimizes model coupling
✅ [PackageZ] encapsulates its internal model well — low implementation leakage
✅ [ModuleX] 使用专用集成DTO — 契约耦合实现良好
✅ [ServiceY] 通过API仅暴露必要数据 — 最小化模型耦合
✅ [PackageZ] 对内部模型封装良好 — 实现细节泄漏少

6.5 Prioritized Recommendations

6.5 优先级建议

High priority (high impact, blocking evolution):
  1. ...
Medium priority (improve architectural health): 2. ...
Low priority (incremental improvements): 3. ...

高优先级(影响大,阻碍迭代):
  1. ...
中优先级(提升架构健康度):2. ...
低优先级(增量改进):3. ...

Quick Reference: Pattern → Integration Strength

快速参考:模式 → 集成强度

Pattern foundIntegration StrengthAction
Reflection to access private membersIntrusiveRefactor urgently
Reading another service's DBIntrusiveRefactor urgently
Duplicated business logicFunctional (symmetric)Extract to shared module
Distributed transaction / SagaFunctional (transactional)Evaluate if cohesion would be better
Mandatory execution orderFunctional (sequential)Document protocol or encapsulate
Rich domain object returnedModel couplingCreate integration DTO
Internal enum shared externallyModel couplingCreate public contract enum
Use-case-specific DTOContract coupling✅ Correct pattern
Versioned public interface/protocolContract coupling✅ Correct pattern
Anti-Corruption LayerContract coupling✅ Correct pattern
发现的模式集成强度行动
使用反射访问私有成员侵入式紧急重构
读取其他服务的数据库侵入式紧急重构
重复的业务逻辑功能耦合(对称)提取到共享模块
分布式事务 / 分布式事务协调器功能耦合(事务性)评估是否内聚性更优
强制执行顺序功能耦合(顺序)记录协议或封装
返回富领域对象模型耦合创建集成DTO
外部共享内部枚举模型耦合创建公共契约枚举
面向用例的DTO契约耦合✅ 正确模式
版本化公共接口/协议契约耦合✅ 正确模式
防腐层契约耦合✅ 正确模式

Quick Heuristics

快速启发式规则

For Integration Strength:
  • "If I change an internal detail of module X, how many other modules need to change?"
  • "Was the integration contract designed to be public, or is it accidental?"
  • "Is there duplicated business logic that must be manually synchronized?"
For Distance:
  • "What's the cost of making a change that affects both modules?"
  • "Do teams maintaining these modules need to coordinate deployments?"
  • "If one module fails, does the other stop working?"
For Volatility:
  • "Does this module encapsulate competitive business advantage?"
  • "Does the business team frequently request changes in this area?"
  • "Is there a history of many refactors in this area?"
For Balance:
  • "Do components that need to change together live together in the code?"
  • "Are independent components well separated?"
  • "Where is there strong coupling with volatile and distant components?" (→ this is the main problem)
集成强度
  • 「若我修改模块X的内部细节,有多少其他模块需要变更?」
  • 「集成契约是为公开设计的,还是偶然形成的?」
  • 「是否存在必须手动同步的重复业务逻辑?」
距离
  • 「修改影响两个模块的成本是多少?」
  • 「维护这些模块的团队需要协调部署吗?」
  • 「若一个模块故障,另一个会停止工作吗?」
易变性
  • 「该模块是否封装了竞争业务优势?」
  • 「业务团队是否频繁要求该领域的变更?」
  • 「该领域是否有多次重构的历史?」
平衡度
  • 「需要一起变更的组件是否在代码中放在一起?」
  • 「独立组件是否充分分离?」
  • 「哪些地方存在强耦合、高易变性且距离远的组件?」(→ 主要问题点)

Known Limitations

已知局限性

  • Volatility is best estimated with real git data rather than static analysis alone
  • Symmetric functional coupling requires semantic code reading — static analysis tools generally don't detect it
  • Organizational distance (different teams) requires user input
  • Dynamic connascence (timing, value, identity) is hard to detect without runtime observation
  • Analysis is a starting point — business context always refines the conclusions
  • 易变性:结合真实Git数据估算效果最佳,仅靠静态分析不够准确
  • 对称功能耦合:需要语义代码读取 — 静态分析工具通常无法检测
  • 组织距离(不同团队):需要用户提供输入
  • 动态共生性(时序、值、标识):若无运行时观测则难以检测
  • 分析仅为起点 — 业务上下文始终会细化结论

Book References

书籍参考

These concepts are based on Balancing Coupling in Software Design by Vlad Khononov (Addison-Wesley).
这些概念基于Vlad Khononov所著《Balancing Coupling in Software Design》(Addison-Wesley出版社)。