Code Coupling Analysis
by kazuph
|
Skill Details
Repository Files
2 files in this skill directory
name: code-coupling-analysis description: | Analyze code coupling using Vlad Khononov's Balanced Coupling framework. Generates interactive HTML reports with D3.js dependency graphs. Use when analyzing architecture health, identifying risky dependencies, or preparing for refactoring decisions. Language-agnostic design. allowed-tools:
- Read
- Write
- Bash
- Glob
- Grep
Coupling Analysis Skill
Analyze code coupling across three dimensions and generate actionable HTML reports.
Framework: Balanced Coupling (Vlad Khononov)
Three key dimensions determine coupling risk:
1. Integration Strength (How tightly modules connect)
| Level | Score | Detection Pattern |
|---|---|---|
| Intrusive | 1.00 | Direct property access, internal state manipulation |
| Functional | 0.75 | Function/component usage, hooks, methods |
| Model | 0.50 | Data type imports, interfaces used as data |
| Contract | 0.25 | Pure interface/type imports |
2. Distance (Where dependencies live)
| Level | Score | Detection Pattern |
|---|---|---|
| Same Module | 0.25 | Same directory (./Component) |
| Sibling Module | 0.50 | Sibling directory (../utils) |
| Distant Module | 0.75 | Different top-level (@/contexts, ~/lib) |
| External | 1.00 | External packages (react, lodash) |
3. Volatility (Change frequency from Git history)
| Level | Score | Changes (6 months) |
|---|---|---|
| Low | 0.00 | 0-2 changes |
| Medium | 0.50 | 3-10 changes |
| High | 1.00 | 11+ changes |
Balance Score Formula
balance = (strength × volatility) × 0.6 + |strength - (1 - distance)| × 0.4
Interpretation:
- 0.0 - 0.2: Well balanced (ideal)
- 0.2 - 0.4: Acceptable
- 0.4 - 0.6: Needs attention
- 0.6+: Risky, consider refactoring
Execution Steps
Step 1: Identify Target Directory and Language
Determine the primary language by checking for:
tsconfig.jsonor*.ts/*.tsx→ TypeScriptpyproject.tomlor*.py→ Pythongo.modor*.go→ GoCargo.tomlor*.rs→ Rustpackage.jsonwith*.js/*.jsx→ JavaScript
Step 2: Gather File List
# TypeScript/JavaScript
find . -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \) \
-not -path "*/node_modules/*" \
-not -path "*/.git/*" \
-not -name "*.d.ts" \
-not -name "*.stories.*" \
-not -name "*.test.*" \
-not -name "*.spec.*"
# Python
find . -type f -name "*.py" \
-not -path "*/.venv/*" \
-not -path "*/venv/*" \
-not -path "*/__pycache__/*" \
-not -name "*_test.py" \
-not -name "test_*.py"
# Go
find . -type f -name "*.go" \
-not -path "*/vendor/*" \
-not -name "*_test.go"
Step 3: Extract Import Dependencies
For each source file, extract imports using language-specific patterns:
TypeScript/JavaScript:
import\s+(?:(?:\{[^}]+\}|\*\s+as\s+\w+|\w+)\s+from\s+)?['"]([^'"]+)['"]
Python:
(?:from\s+(\S+)\s+import|import\s+(\S+))
Go:
import\s+(?:\w+\s+)?["']([^"']+)["']
Step 4: Calculate Git Volatility
# Get change count for each file (last 6 months)
git log --since="6 months ago" --name-only --pretty=format: -- "*.ts" "*.tsx" | \
grep -v '^$' | sort | uniq -c | sort -rn
Step 5: Analyze Each Coupling
For each import relationship:
- Determine strength from import pattern
- Calculate distance from file paths
- Look up volatility from git history
- Compute balance score
Step 6: Generate HTML Report
The report MUST include these sections:
Summary Statistics Card
- Total files analyzed
- Total couplings found
- Average strength, distance, volatility
- Overall balance score with color indicator
Most Frequently Changed Files
- Top 10 files by change count
- Badge indicating volatility level (High/Medium/Low)
Two-Column Layout
Left Column:
-
Risky Couplings (score ≥ 0.4) with:
- Score badge
- Volatility indicator
- Strength type
- File path → dependency path
- Warning box explaining WHY it's risky
- Action box with specific refactoring suggestions
-
Well-Balanced Couplings (score < 0.2) with:
- Score badge
- Pattern explanation
Right Column:
- D3.js Force-Directed Dependency Graph:
- Nodes colored by volatility (red=high, yellow=medium, green=low)
- Node size by dependency count
- Edges showing coupling relationships
- Filters for strength and volatility
- Zoom and pan support
Legend Section
- Strength levels with scores
- Distance levels with scores
- Balance score interpretation guide
Step 7: Provide "So What?" Context
Every metric MUST include explanatory text:
BAD (raw numbers only):
Average Strength: 0.74
GOOD (with context):
Average Strength: 0.74
→ Component usage is central (Functional level)
→ Most dependencies are function/hook calls, not loose contracts
Output Location
Save the HTML report to:
<project-root>/coupling-analysis.html
Or if analyzing a specific package:
<package-dir>/coupling-analysis.html
Example Insights to Include
For AuthContext-like patterns (high volatility + many dependents):
⚠️ Warning: AuthContextは16回変更されており、12以上のファイルから依存されています。
変更のたびに広範囲に影響が及ぶリスクがあります。
💡 Action:
- 認証状態(isAuthenticated)と認証操作(signIn/signOut)を分離検討
- 状態のみのuseAuthStateと操作のみのuseAuthActionsに分割
- カスタムフックで必要な部分だけを公開
Anti-Patterns to Detect
- God Module: Single file with 10+ dependents AND high volatility
- Shotgun Surgery: One change requires modifying many files
- Distant Intrusive: High strength (0.75+) combined with high distance (0.75+)
- Volatile Core: Core utilities with 10+ changes in 6 months
Report Quality Checklist
- Two-column responsive layout
- D3.js interactive graph with filters
- Every metric has "So What?" explanation
- Risky couplings have Warning + Action boxes
- Color-coded badges (red/yellow/green)
- Dark theme (GitHub-style)
- Japanese explanations for Japanese codebases
- Mobile-responsive design
Related Skills
Mermaid Diagrams
Comprehensive guide for creating software diagrams using Mermaid syntax. Use when users need to create, visualize, or document software through diagrams including class diagrams (domain modeling, object-oriented design), sequence diagrams (application flows, API interactions, code execution), flowcharts (processes, algorithms, user journeys), entity relationship diagrams (database schemas), C4 architecture diagrams (system context, containers, components), state diagrams, git graphs, pie charts,
Matlab
MATLAB and GNU Octave numerical computing for matrix operations, data analysis, visualization, and scientific computing. Use when writing MATLAB/Octave scripts for linear algebra, signal processing, image processing, differential equations, optimization, statistics, or creating scientific visualizations. Also use when the user needs help with MATLAB syntax, functions, or wants to convert between MATLAB and Python code. Scripts can be executed with MATLAB or the open-source GNU Octave interpreter
Dask
Distributed computing for larger-than-RAM pandas/NumPy workflows. Use when you need to scale existing pandas/NumPy code beyond memory or across clusters. Best for parallel file processing, distributed ML, integration with existing pandas code. For out-of-core analytics on single machine use vaex; for in-memory speed use polars.
Consult Zai
Compare z.ai GLM 4.7 and code-searcher responses for comprehensive dual-AI code analysis. Use when you need multiple AI perspectives on code questions.
Writing Effective Prompts
Structure Claude prompts for clarity and better results using roles, explicit instructions, context, positive framing, and strategic organization. Use when crafting prompts for complex tasks, long documents, tool workflows, or code generation.
Analyze Performance
Establish performance baselines and detect regressions using flamegraph analysis. Use when optimizing performance-critical code, investigating performance issues, or before creating commits with performance-sensitive changes.
Flowchart Creator
Create HTML flowcharts and process diagrams with decision trees, color-coded stages, arrows, and swimlanes. Use when users request flowcharts, process diagrams, workflow visualizations, or decision trees.
Bio Reporting Rmarkdown Reports
Create reproducible bioinformatics analysis reports with R Markdown including code, results, and visualizations in HTML, PDF, or Word format. Use when generating analysis reports with RMarkdown.
Desmos Graphing
Create interactive Desmos graphs in Obsidian using desmos-graph code blocks. Use when visualizing functions, parametric curves, inequalities, or mathematical relationships with customizable styling and settings.
Performance
Rendimiento & Optimización - Atoll Tourisme. Use when optimizing performance or profiling code.
