LeadershipTechnical ManagerTechnical Leadership

Session 02 - Tech Debt Strategy

"Tech debt giống nợ tài chính - một chút thì leverage, nhiều quá thì phá sản." - Ward Cunningham


1. Types of Tech Debt

Deliberate vs Accidental

Deliberate (Cố ý)Accidental (Vô tình)
Biết là nợ"Ship trước, refactor sau"Không nhận ra đang tạo nợ
Ví dụHardcode config để kịp deadlineCopy-paste code vì không biết có utility sẵn
Quản lýGhi vào backlog, plan trả nợPhát hiện qua code review, static analysis

Reckless vs Prudent

Reckless (Liều lĩnh)Prudent (Cân nhắc)
Deliberate"Không có thời gian design""Ship nhanh, biết phải refactor"
Accidental"Design pattern là gì?""Giờ mới biết cách tốt hơn"

2. Tech Debt Quadrant (Martin Fowler)

                    Deliberate
                       │
         ┌─────────────┼─────────────┐
         │             │             │
         │  RECKLESS   │  PRUDENT    │
         │             │             │
         │ "We don't   │ "We must    │
         │  have time  │  ship now   │
         │  for design"│  and deal   │
         │             │  with       │
         │             │  consequences│
Reckless ─┼─────────────┼─────────────┼─ Prudent
         │             │             │
         │ "What's     │ "Now we     │
         │  layered    │  know how   │
         │  architecture│  we should  │
         │  ?"         │  have done  │
         │             │  it"        │
         │  RECKLESS   │  PRUDENT    │
         │             │             │
         └─────────────┼─────────────┘
                       │
                   Accidental

Hành động theo quadrant

QuadrantAction
Reckless + DeliberateStop. Training + code review cần thiết ngay
Reckless + AccidentalĐầu tư vào learning, pair programming, mentoring
Prudent + DeliberateOK nếu có plan trả nợ rõ ràng trong backlog
Prudent + AccidentalTự nhiên - refactor khi touch code (Boy Scout Rule)

3. How to Identify and Catalog Tech Debt

Detection Methods

MethodTool / ApproachWhat it finds
Static AnalysisSonarQube, NDepend, Roslyn AnalyzersCode smells, complexity, duplication
Code ReviewPull request reviewsDesign issues, naming, patterns
Architecture TestsArchUnitNETDependency violations, layer breaches
Runtime MonitoringApplication Insights, DatadogPerformance debt, memory leaks
Developer FeedbackSurvey, retro, tech debt backlogPain points chỉ dev biết
Dependency ScanDependabot, SnykOutdated/vulnerable packages

Tech Debt Catalog Template

markdown
## Tech Debt Item: TD-042

**Title:** OrderService God Class - 2,500 lines, 47 methods
**Category:** Design Debt
**Location:** `src/Services/OrderService.cs`
**Created:** 2024-01-15
**Severity:** High

### Description
OrderService đảm nhiệm quá nhiều responsibilities: order creation,
validation, payment processing, inventory check, notification.
Vi phạm Single Responsibility Principle.

### Impact
- Mỗi lần thay đổi 1 feature → risk regression feature khác
- Unit test coverage chỉ 35% vì quá phức tạp để mock
- Onboarding dev mới mất 2 tuần mới hiểu file này

### Proposed Solution
Tách thành: OrderCreationService, OrderValidationService,
PaymentService, InventoryService, NotificationService.
Apply Mediator pattern cho orchestration.

### Estimated Effort
- 8-13 story points (2-3 Sprint nếu làm incremental)
- Cần regression test toàn bộ order flow

### Business Risk if Not Fixed
- Feature velocity giảm 30% cho order-related stories
- Bug rate tăng mỗi Sprint (3 bugs/Sprint liên quan order)

4. Quantifying Tech Debt

Time Cost Measurement

MetricCách đoTarget
Interest RateThời gian extra mỗi Sprint vì debtTrack qua Sprint retro
Fix Time MultiplierBug fix mất bao lâu so với expected< 2x = acceptable
Onboarding TimeDev mới mất bao lâu productive< 2 tuần cho senior
Change Failure Rate% deployments gây incident< 15%
Lead Time IncreaseStory completion time trendKhông tăng qua Sprints

Cost Formula

Annual Cost of Debt = 
  (Extra hours per Sprint due to debt) x 
  (Number of Sprints per year) x 
  (Average developer hourly rate)

Ví dụ:
  5 hours/Sprint x 26 Sprints/year x $50/hour = $6,500/year per item
  20 items in backlog = $130,000/year đang "trả lãi"

Risk Scoring

Debt Priority Score = Impact (1-5) x Frequency (1-5) x Effort_Inverse (5/effort_in_sprints)

Ví dụ:
- God Class OrderService:  Impact=5, Frequency=5, Effort=3 Sprint
  Score = 5 x 5 x (5/3) = 41.7 → HIGH PRIORITY

- Outdated logging library: Impact=2, Frequency=1, Effort=0.5 Sprint  
  Score = 2 x 1 x (5/0.5) = 20 → MEDIUM PRIORITY

5. Prioritization Framework

Tech Debt Decision Matrix

Prioritization Criteria

Tiêu chíWeightLow (1)Medium (3)High (5)
Business Impact30%No user impactSlowdownFeature blocked
Developer Productivity25%Minor annoyanceAdds 1-2h/weekAdds 1+ day/Sprint
Risk25%Low probabilityModerate probabilityTicking time bomb
Effort to Fix20%> 2 Sprints1 Sprint< 3 days

6. Tech Debt Sprint Allocation - 20% Rule

The 20% Budget

Dành 20% Sprint capacity cho tech debt và improvements:

  • Sprint 2 tuần, team 5 devs = 50 dev-days
  • 20% = 10 dev-days cho tech debt

Implementation Models

ModelMô tảPhù hợp khi
ContinuousMỗi Sprint dành 20% cho tech debtTeam mature, PO đồng ý
Tech Debt SprintMỗi 4-5 Sprint, 1 Sprint full tech debtDebt tích tụ nhiều
Boy Scout RuleRefactor khi touch code (leave it better)Debt nhỏ, scattered
Dedicated Team1-2 devs full-time tech debtLarge legacy system
Hack Week1 tuần/quarter focus innovation + debtTeam morale boost

Track Tech Debt Velocity

Sprint | Feature Points | Tech Debt Points | Ratio
-------|---------------|-----------------|------
S1     | 34            | 5               | 15%
S2     | 30            | 8               | 27%
S3     | 32            | 8               | 25%
S4     | 28            | 5               | 18%
S5     | 35            | 8               | 23%
                                    Average: 21% ✅

7. Communicating Tech Debt to Stakeholders

Nói ngôn ngữ Business, không nói ngôn ngữ Tech

Dev nói (bad)Business nói (good)
"Code smell trong OrderService""Mỗi feature mới liên quan đơn hàng mất gấp đôi thời gian"
"Cần upgrade .NET 6 lên .NET 8"".NET 6 hết support tháng 11, security patches không còn"
"Cần refactor database layer""Query chậm dần, report generation tăng từ 2s lên 8s"
"Technical debt tích tụ nhiều""Velocity giảm 20% trong 3 tháng, dự kiến tiếp tục giảm"

Stakeholder Communication Template

markdown
## Tech Debt Quarterly Report - Q1 2025

### Summary
- **Total debt items:** 24 (tăng 6 so với Q4)
- **Estimated annual cost:** $130,000 (developer productivity loss)
- **Velocity trend:** Giảm 15% so với Q4

### Top 3 Critical Items
1. **OrderService refactoring** - blocking 3 upcoming features,
   estimated 2 Sprint delay if not addressed. Fix: 8 points.
2. **.NET 6 → .NET 8 migration** - security support ends Nov 2024.
   Fix: 13 points across 2 Sprints.
3. **Database query optimization** - report generation degrading
   30% per quarter. Fix: 5 points.

### Recommendation
Allocate 1 full Sprint (Sprint 8) for tech debt reduction.
Expected outcome: recover 20% velocity, unblock Q2 features.

### Investment vs Return
- **Investment:** 1 Sprint (50 dev-days = $25,000)
- **Return:** $130,000/year savings + faster Q2 delivery
- **ROI:** 5.2x in first year

Visualization cho Management

Feature Velocity Trend (Story Points/Sprint)
40 |  ██
35 |  ██ ██       ██
30 |  ██ ██ ██    ██ ██
25 |  ██ ██ ██ ██ ██ ██
20 |  ██ ██ ██ ██ ██ ██ ←── "Nếu không trả nợ"
15 |  ██ ██ ██ ██ ██ ██
   |________________________
    Q1  Q2  Q3  Q4  Q1  Q2
              ↑
        Tech debt Sprint here
        → Velocity phục hồi

Tip: Đừng bao giờ nói "cần refactor vì code xấu". Nói "cần refactor để ship feature X nhanh hơn 2 tuần và giảm bug rate 40%."


Session tiếp theo: Session 03 - Engineering Standards sẽ đi sâu vào các tiêu chuẩn kỹ thuật cho dự án .NET.