universal-dev-standards 4.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundled/core/refactoring-standards.md +342 -120
- package/bundled/locales/zh-CN/core/refactoring-standards.md +10 -9
- package/bundled/locales/zh-CN/skills/claude-code/commands/refactor.md +178 -0
- package/bundled/locales/zh-CN/skills/claude-code/refactoring-assistant/SKILL.md +243 -97
- package/bundled/locales/zh-TW/core/refactoring-standards.md +347 -125
- package/bundled/locales/zh-TW/skills/claude-code/commands/refactor.md +178 -0
- package/bundled/locales/zh-TW/skills/claude-code/refactoring-assistant/SKILL.md +198 -52
- package/bundled/skills/claude-code/commands/refactor.md +173 -0
- package/bundled/skills/claude-code/refactoring-assistant/SKILL.md +161 -63
- package/package.json +1 -1
- package/standards-registry.json +12 -8
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
> **Language**: English | [繁體中文](../locales/zh-TW/core/refactoring-standards.md)
|
|
4
4
|
|
|
5
|
-
**Version**:
|
|
6
|
-
**Last Updated**: 2026-01-
|
|
5
|
+
**Version**: 2.0.0
|
|
6
|
+
**Last Updated**: 2026-01-21
|
|
7
7
|
**Applicability**: All software projects undertaking code improvement initiatives
|
|
8
8
|
|
|
9
9
|
---
|
|
@@ -23,17 +23,29 @@ This standard defines comprehensive guidelines for code refactoring, covering ev
|
|
|
23
23
|
## Table of Contents
|
|
24
24
|
|
|
25
25
|
1. [Refactoring vs. Rewriting Decision Matrix](#refactoring-vs-rewriting-decision-matrix)
|
|
26
|
-
2. [Refactoring Strategies
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
2. [Tactical Refactoring Strategies](#tactical-refactoring-strategies)
|
|
27
|
+
- [Preparatory Refactoring](#preparatory-refactoring)
|
|
28
|
+
- [The Boy Scout Rule](#the-boy-scout-rule)
|
|
29
|
+
- [Red-Green-Refactor](#red-green-refactor)
|
|
30
|
+
3. [Strategic Refactoring Strategies](#strategic-refactoring-strategies)
|
|
31
|
+
- [Strangler Fig Pattern](#strangler-fig-pattern)
|
|
32
|
+
- [Anti-Corruption Layer](#anti-corruption-layer)
|
|
33
|
+
- [Branch by Abstraction](#branch-by-abstraction)
|
|
34
|
+
- [Parallel Change](#parallel-change-expand-migrate-contract)
|
|
35
|
+
4. [Safety Strategies for Legacy Code](#safety-strategies-for-legacy-code)
|
|
36
|
+
- [Characterization Tests](#characterization-tests)
|
|
37
|
+
- [Scratch Refactoring](#scratch-refactoring)
|
|
38
|
+
- [Finding Seams](#finding-seams)
|
|
39
|
+
- [Sprout and Wrap Techniques](#sprout-and-wrap-techniques)
|
|
29
40
|
5. [Database Refactoring](#database-refactoring)
|
|
30
41
|
6. [Safe Refactoring Workflow](#safe-refactoring-workflow)
|
|
31
42
|
7. [Refactoring Metrics](#refactoring-metrics)
|
|
32
43
|
8. [Team Collaboration](#team-collaboration)
|
|
33
44
|
9. [Technical Debt Management](#technical-debt-management)
|
|
34
|
-
10. [
|
|
35
|
-
11. [
|
|
36
|
-
12. [
|
|
45
|
+
10. [Decision Matrix Summary](#decision-matrix-summary)
|
|
46
|
+
11. [Related Standards](#related-standards)
|
|
47
|
+
12. [References](#references)
|
|
48
|
+
13. [Version History](#version-history)
|
|
37
49
|
|
|
38
50
|
---
|
|
39
51
|
|
|
@@ -90,56 +102,268 @@ When rewriting, teams often over-engineer. Avoid:
|
|
|
90
102
|
|
|
91
103
|
---
|
|
92
104
|
|
|
93
|
-
## Refactoring Strategies
|
|
105
|
+
## Tactical Refactoring Strategies
|
|
94
106
|
|
|
95
|
-
|
|
107
|
+
Tactical strategies are applied during daily development work. They operate at minute-to-hour timescales and require minimal coordination.
|
|
96
108
|
|
|
97
|
-
|
|
109
|
+
### Preparatory Refactoring
|
|
110
|
+
|
|
111
|
+
**Definition**: Restructuring existing code to make an upcoming change easier to implement.
|
|
112
|
+
|
|
113
|
+
> "First make the change easy (this might be hard), then make the easy change." — Kent Beck
|
|
114
|
+
|
|
115
|
+
**When to Use**:
|
|
116
|
+
- Existing architecture resists the feature you need to add
|
|
117
|
+
- Code structure needs adjustment to accommodate new requirements
|
|
118
|
+
- You want to reduce friction for upcoming changes
|
|
119
|
+
|
|
120
|
+
**Workflow**:
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
1. Identify the change you want to make
|
|
124
|
+
2. Identify what makes this change difficult
|
|
125
|
+
3. Refactor to make the change easy
|
|
126
|
+
4. Make the (now easy) change
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Example**:
|
|
130
|
+
- Before: Adding caching to a tightly-coupled service is complex
|
|
131
|
+
- Preparatory refactoring: Extract interface, inject dependencies
|
|
132
|
+
- After: Adding caching becomes straightforward
|
|
133
|
+
|
|
134
|
+
**Key Principles**:
|
|
135
|
+
- The preparatory refactoring is a separate commit from the feature
|
|
136
|
+
- Each step maintains passing tests
|
|
137
|
+
- Don't combine refactoring with feature work in the same commit
|
|
138
|
+
|
|
139
|
+
### The Boy Scout Rule
|
|
140
|
+
|
|
141
|
+
**Definition**: Leave the code cleaner than you found it. This is "opportunistic refactoring" — instead of scheduling refactoring sprints, clean up code as you touch it during bug fixes or feature work.
|
|
142
|
+
|
|
143
|
+
> "Leave the campground cleaner than you found it." — Robert C. Martin
|
|
144
|
+
|
|
145
|
+
**When to Use**:
|
|
146
|
+
- Any maintenance task
|
|
147
|
+
- Bug fixes
|
|
148
|
+
- Feature additions
|
|
149
|
+
- Fighting software entropy
|
|
150
|
+
|
|
151
|
+
**Guidelines**:
|
|
152
|
+
- Make only small improvements (minutes, not hours)
|
|
153
|
+
- Don't change behavior
|
|
154
|
+
- Don't break existing tests
|
|
155
|
+
- Keep scope within your current task
|
|
156
|
+
|
|
157
|
+
**Examples of Boy Scout Improvements**:
|
|
158
|
+
- Rename a confusingly-named variable
|
|
159
|
+
- Extract a few lines into a well-named method
|
|
160
|
+
- Remove dead code you notice
|
|
161
|
+
- Add a clarifying comment
|
|
162
|
+
- Fix a minor code style issue
|
|
163
|
+
|
|
164
|
+
**Anti-patterns to Avoid**:
|
|
165
|
+
- Turning a bug fix into a major refactoring project
|
|
166
|
+
- Refactoring unrelated code
|
|
167
|
+
- Making changes without test coverage
|
|
168
|
+
- Scope creep beyond the original task
|
|
169
|
+
|
|
170
|
+
### Red-Green-Refactor
|
|
171
|
+
|
|
172
|
+
Part of the Test-Driven Development (TDD) cycle. See [Test-Driven Development](test-driven-development.md) for complete details.
|
|
98
173
|
|
|
99
174
|
**Characteristics**:
|
|
100
|
-
- Duration: 5-15 minutes
|
|
175
|
+
- Duration: 5-15 minutes per cycle
|
|
101
176
|
- Scope: Single method or class
|
|
102
|
-
- Tests: Must remain green
|
|
177
|
+
- Tests: Must remain green after refactoring
|
|
178
|
+
|
|
179
|
+
**The Cycle**:
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
┌─────────────────────────────────────────┐
|
|
183
|
+
│ │
|
|
184
|
+
│ ┌─────┐ ┌─────┐ ┌─────────┐ │
|
|
185
|
+
│ │ RED │ ──► │GREEN│ ──► │REFACTOR │ │
|
|
186
|
+
│ └─────┘ └─────┘ └─────────┘ │
|
|
187
|
+
│ ▲ │ │
|
|
188
|
+
│ └──────────────────────────┘ │
|
|
189
|
+
│ │
|
|
190
|
+
└─────────────────────────────────────────┘
|
|
191
|
+
```
|
|
103
192
|
|
|
104
|
-
**Common
|
|
193
|
+
**Common Refactoring Techniques**:
|
|
105
194
|
- Extract Method
|
|
106
195
|
- Rename
|
|
107
196
|
- Inline Variable
|
|
108
197
|
- Replace Magic Number with Constant
|
|
198
|
+
- Remove Duplication
|
|
109
199
|
|
|
110
|
-
|
|
200
|
+
---
|
|
111
201
|
|
|
112
|
-
|
|
202
|
+
## Strategic Refactoring Strategies
|
|
113
203
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
204
|
+
Strategic strategies are used for significant architectural changes. They operate at week-to-month timescales and require team coordination.
|
|
205
|
+
|
|
206
|
+
### Strangler Fig Pattern
|
|
207
|
+
|
|
208
|
+
**Definition**: Gradually replacing a legacy system by incrementally routing functionality to a new system.
|
|
118
209
|
|
|
119
|
-
**
|
|
210
|
+
**Origin**: Named after strangler fig trees that grow around a host tree, eventually replacing it.
|
|
211
|
+
|
|
212
|
+
**When to Use**:
|
|
213
|
+
- Gradually replacing a legacy system
|
|
214
|
+
- Cannot afford big-bang rewrite
|
|
215
|
+
- Need to maintain service during migration
|
|
216
|
+
|
|
217
|
+
**Architecture**:
|
|
120
218
|
|
|
121
219
|
```
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
220
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
221
|
+
│ Strangler Fig Pattern │
|
|
222
|
+
├─────────────────────────────────────────────────────────────────┤
|
|
223
|
+
│ │
|
|
224
|
+
│ Phase 1: INTERCEPT │
|
|
225
|
+
│ ┌─────────┐ ┌─────────┐ ┌─────────────┐ │
|
|
226
|
+
│ │ Request │────▶│ Facade │────▶│ Legacy (100%)│ │
|
|
227
|
+
│ └─────────┘ └─────────┘ └─────────────┘ │
|
|
228
|
+
│ │
|
|
229
|
+
│ Phase 2: MIGRATE │
|
|
230
|
+
│ ┌─────────┐ ┌─────────┐ ┌─────────────┐ │
|
|
231
|
+
│ │ Request │────▶│ Facade │──┬─▶│ New (Feature A)│ │
|
|
232
|
+
│ └─────────┘ └─────────┘ │ └─────────────┘ │
|
|
233
|
+
│ └─▶│ Legacy (Rest) │ │
|
|
234
|
+
│ └─────────────┘ │
|
|
235
|
+
│ │
|
|
236
|
+
│ Phase 3: COMPLETE │
|
|
237
|
+
│ ┌─────────┐ ┌─────────────┐ │
|
|
238
|
+
│ │ Request │────▶│ New (100%) │ [Legacy decommissioned] │
|
|
239
|
+
│ └─────────┘ └─────────────┘ │
|
|
240
|
+
│ │
|
|
241
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
127
242
|
```
|
|
128
243
|
|
|
129
|
-
|
|
244
|
+
**Checklist**:
|
|
245
|
+
- [ ] Identify interception point (API gateway, facade, proxy)
|
|
246
|
+
- [ ] Create event capture layer
|
|
247
|
+
- [ ] Implement first feature in new system
|
|
248
|
+
- [ ] Route traffic incrementally
|
|
249
|
+
- [ ] Monitor and compare results
|
|
250
|
+
- [ ] Decommission legacy component
|
|
130
251
|
|
|
131
|
-
|
|
252
|
+
### Anti-Corruption Layer
|
|
132
253
|
|
|
133
|
-
**
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
254
|
+
**Definition**: A translation layer between a legacy system and a new system that prevents the legacy system's chaotic domain model from "corrupting" the new system's clean architecture.
|
|
255
|
+
|
|
256
|
+
**Origin**: Eric Evans, Domain-Driven Design (2003)
|
|
257
|
+
|
|
258
|
+
**When to Use**:
|
|
259
|
+
- New and legacy systems must coexist and interact frequently
|
|
260
|
+
- Legacy system has a complex/chaotic domain model
|
|
261
|
+
- Protecting the new system's Bounded Context
|
|
262
|
+
|
|
263
|
+
**Architecture**:
|
|
264
|
+
|
|
265
|
+
```
|
|
266
|
+
┌────────────────────────────────────────────────────────────┐
|
|
267
|
+
│ Anti-Corruption Layer (ACL) │
|
|
268
|
+
├────────────────────────────────────────────────────────────┤
|
|
269
|
+
│ │
|
|
270
|
+
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
|
|
271
|
+
│ │ New System │────▶│ ACL │────▶│Legacy System│ │
|
|
272
|
+
│ │ (Clean API) │ │ │ │(Messy Model)│ │
|
|
273
|
+
│ └─────────────┘ │ ┌─────────┐ │ └─────────────┘ │
|
|
274
|
+
│ │ │ Adapter │ │ │
|
|
275
|
+
│ │ │ Facade │ │ │
|
|
276
|
+
│ │ │Translator││ │
|
|
277
|
+
│ │ └─────────┘ │ │
|
|
278
|
+
│ └─────────────┘ │
|
|
279
|
+
│ │
|
|
280
|
+
└────────────────────────────────────────────────────────────┘
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
**Components**:
|
|
284
|
+
|
|
285
|
+
| Component | Purpose |
|
|
286
|
+
|-----------|---------|
|
|
287
|
+
| **Facade** | Simplifies complex legacy interfaces |
|
|
288
|
+
| **Adapter** | Converts legacy data formats to new domain model |
|
|
289
|
+
| **Translator** | Maps legacy terminology to ubiquitous language |
|
|
290
|
+
|
|
291
|
+
**Implementation Checklist**:
|
|
292
|
+
- [ ] Define clear interface for the ACL
|
|
293
|
+
- [ ] Map legacy entities to new domain model
|
|
294
|
+
- [ ] Handle data format conversions
|
|
295
|
+
- [ ] Implement error translation
|
|
296
|
+
- [ ] Add logging for debugging
|
|
297
|
+
- [ ] Thoroughly test ACL isolation
|
|
298
|
+
|
|
299
|
+
**Comparison with Strangler Fig**:
|
|
300
|
+
|
|
301
|
+
| Aspect | Strangler Fig | Anti-Corruption Layer |
|
|
302
|
+
|--------|---------------|----------------------|
|
|
303
|
+
| **Goal** | Replace legacy system | Coexist with legacy system |
|
|
304
|
+
| **Direction** | Migrate away from legacy | Integrate with legacy |
|
|
305
|
+
| **End state** | Legacy decommissioned | Both systems continue |
|
|
306
|
+
|
|
307
|
+
### Branch by Abstraction
|
|
308
|
+
|
|
309
|
+
**Definition**: Refactoring shared code without long-lived branches by introducing an abstraction layer.
|
|
310
|
+
|
|
311
|
+
**When to Use**:
|
|
312
|
+
- Refactoring shared code without long-lived branches
|
|
313
|
+
- Need trunk-based development
|
|
314
|
+
- Changes are too risky for single commit
|
|
315
|
+
|
|
316
|
+
**Steps**:
|
|
317
|
+
|
|
318
|
+
```
|
|
319
|
+
Step 1: Introduce Abstraction
|
|
320
|
+
Client → Abstraction (interface) → Old Implementation
|
|
321
|
+
|
|
322
|
+
Step 2: Add New Implementation
|
|
323
|
+
Client → Abstraction → Old Implementation
|
|
324
|
+
└─→ New Implementation (feature-toggled)
|
|
137
325
|
|
|
138
|
-
|
|
326
|
+
Step 3: Switch and Remove
|
|
327
|
+
Client → New Implementation
|
|
328
|
+
[Old Implementation removed]
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
**Key Principles**:
|
|
332
|
+
- All changes on main/trunk (no long branches)
|
|
333
|
+
- Feature toggles control which implementation is active
|
|
334
|
+
- Both implementations can coexist during transition
|
|
335
|
+
|
|
336
|
+
### Parallel Change (Expand-Migrate-Contract)
|
|
337
|
+
|
|
338
|
+
**Definition**: Changing interfaces used by multiple clients through a three-phase approach.
|
|
339
|
+
|
|
340
|
+
**When to Use**:
|
|
341
|
+
- Changing interfaces used by multiple clients
|
|
342
|
+
- Database schema migration
|
|
343
|
+
- Need zero-downtime migration
|
|
344
|
+
|
|
345
|
+
**Phases**:
|
|
346
|
+
|
|
347
|
+
```
|
|
348
|
+
Phase 1: EXPAND
|
|
349
|
+
├─ Add new field/method alongside old
|
|
350
|
+
├─ New code uses new interface
|
|
351
|
+
└─ Old code still works
|
|
352
|
+
|
|
353
|
+
Phase 2: MIGRATE
|
|
354
|
+
├─ Update all clients to use new interface
|
|
355
|
+
├─ Verify all clients migrated
|
|
356
|
+
└─ Data migration (if needed)
|
|
357
|
+
|
|
358
|
+
Phase 3: CONTRACT
|
|
359
|
+
├─ Remove old field/method
|
|
360
|
+
├─ Clean up migration code
|
|
361
|
+
└─ Update documentation
|
|
362
|
+
```
|
|
139
363
|
|
|
140
364
|
---
|
|
141
365
|
|
|
142
|
-
## Legacy Code
|
|
366
|
+
## Safety Strategies for Legacy Code
|
|
143
367
|
|
|
144
368
|
Based on Michael Feathers' "Working Effectively with Legacy Code".
|
|
145
369
|
|
|
@@ -156,7 +380,9 @@ Based on Michael Feathers' "Working Effectively with Legacy Code".
|
|
|
156
380
|
|
|
157
381
|
### Characterization Tests
|
|
158
382
|
|
|
159
|
-
**
|
|
383
|
+
**Definition**: Tests that capture existing behavior (not verify correctness).
|
|
384
|
+
|
|
385
|
+
**Purpose**: Create a safety net before refactoring legacy code.
|
|
160
386
|
|
|
161
387
|
**Process**:
|
|
162
388
|
|
|
@@ -184,6 +410,31 @@ test('calculateDiscount returns 15 for GOLD customers', () => {
|
|
|
184
410
|
});
|
|
185
411
|
```
|
|
186
412
|
|
|
413
|
+
**Key Principle**: Characterization tests document what the code *does*, not what it *should do*.
|
|
414
|
+
|
|
415
|
+
### Scratch Refactoring
|
|
416
|
+
|
|
417
|
+
**Definition**: Refactoring to understand code, without keeping the changes.
|
|
418
|
+
|
|
419
|
+
**Purpose**: Explore and understand undocumented code through hands-on modification.
|
|
420
|
+
|
|
421
|
+
**Workflow**:
|
|
422
|
+
|
|
423
|
+
```
|
|
424
|
+
1. Create a scratch branch (or use git stash)
|
|
425
|
+
2. Aggressively refactor to understand the code
|
|
426
|
+
3. Take notes on what you learn
|
|
427
|
+
4. Discard all changes (git reset --hard)
|
|
428
|
+
5. Apply learnings to write characterization tests
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
**When to Use**:
|
|
432
|
+
- Code is too complex to understand by reading
|
|
433
|
+
- No documentation exists
|
|
434
|
+
- You need to build a mental model quickly
|
|
435
|
+
|
|
436
|
+
**Key Principle**: The goal is understanding, not clean code. Discard everything when done.
|
|
437
|
+
|
|
187
438
|
### Finding Seams
|
|
188
439
|
|
|
189
440
|
**Definition**: A seam is a place where you can alter behavior without editing code.
|
|
@@ -194,6 +445,8 @@ test('calculateDiscount returns 15 for GOLD customers', () => {
|
|
|
194
445
|
| **Preprocessing Seam** | Compile-time substitution | Conditional compilation, macros |
|
|
195
446
|
| **Link Seam** | Replace at link time | Dependency injection, module replacement |
|
|
196
447
|
|
|
448
|
+
**Purpose**: Inject test doubles without modifying legacy code.
|
|
449
|
+
|
|
197
450
|
### Sprout and Wrap Techniques
|
|
198
451
|
|
|
199
452
|
| Technique | When to Use | How |
|
|
@@ -210,7 +463,7 @@ test('calculateDiscount returns 15 for GOLD customers', () => {
|
|
|
210
463
|
Techniques for understanding undocumented code:
|
|
211
464
|
|
|
212
465
|
```
|
|
213
|
-
1. Scratch Refactoring
|
|
466
|
+
1. Scratch Refactoring (see above)
|
|
214
467
|
├─ Refactor to understand, not to keep
|
|
215
468
|
├─ Use git stash or branch
|
|
216
469
|
└─ Discard when done (git reset --hard)
|
|
@@ -233,90 +486,6 @@ Techniques for understanding undocumented code:
|
|
|
233
486
|
|
|
234
487
|
---
|
|
235
488
|
|
|
236
|
-
## Large-Scale Refactoring Patterns
|
|
237
|
-
|
|
238
|
-
### Strangler Fig Pattern
|
|
239
|
-
|
|
240
|
-
**Use when**: Gradually replacing a legacy system
|
|
241
|
-
|
|
242
|
-
```
|
|
243
|
-
┌─────────────────────────────────────────────────────────────────┐
|
|
244
|
-
│ Strangler Fig Pattern │
|
|
245
|
-
├─────────────────────────────────────────────────────────────────┤
|
|
246
|
-
│ │
|
|
247
|
-
│ Phase 1: INTERCEPT │
|
|
248
|
-
│ ┌─────────┐ ┌─────────┐ ┌─────────────┐ │
|
|
249
|
-
│ │ Request │────▶│ Facade │────▶│ Legacy (100%)│ │
|
|
250
|
-
│ └─────────┘ └─────────┘ └─────────────┘ │
|
|
251
|
-
│ │
|
|
252
|
-
│ Phase 2: MIGRATE │
|
|
253
|
-
│ ┌─────────┐ ┌─────────┐ ┌─────────────┐ │
|
|
254
|
-
│ │ Request │────▶│ Facade │──┬─▶│ New (Feature A)│ │
|
|
255
|
-
│ └─────────┘ └─────────┘ │ └─────────────┘ │
|
|
256
|
-
│ └─▶│ Legacy (Rest) │ │
|
|
257
|
-
│ └─────────────┘ │
|
|
258
|
-
│ │
|
|
259
|
-
│ Phase 3: COMPLETE │
|
|
260
|
-
│ ┌─────────┐ ┌─────────────┐ │
|
|
261
|
-
│ │ Request │────▶│ New (100%) │ [Legacy decommissioned] │
|
|
262
|
-
│ └─────────┘ └─────────────┘ │
|
|
263
|
-
│ │
|
|
264
|
-
└─────────────────────────────────────────────────────────────────┘
|
|
265
|
-
```
|
|
266
|
-
|
|
267
|
-
**Checklist**:
|
|
268
|
-
- [ ] Identify interception point (API gateway, facade, proxy)
|
|
269
|
-
- [ ] Create event capture layer
|
|
270
|
-
- [ ] Implement first feature in new system
|
|
271
|
-
- [ ] Route traffic incrementally
|
|
272
|
-
- [ ] Monitor and compare results
|
|
273
|
-
- [ ] Decommission legacy component
|
|
274
|
-
|
|
275
|
-
### Branch by Abstraction
|
|
276
|
-
|
|
277
|
-
**Use when**: Refactoring shared code without long-lived branches
|
|
278
|
-
|
|
279
|
-
```
|
|
280
|
-
Step 1: Introduce Abstraction
|
|
281
|
-
Client → Abstraction (interface) → Old Implementation
|
|
282
|
-
|
|
283
|
-
Step 2: Add New Implementation
|
|
284
|
-
Client → Abstraction → Old Implementation
|
|
285
|
-
└─→ New Implementation (feature-toggled)
|
|
286
|
-
|
|
287
|
-
Step 3: Switch and Remove
|
|
288
|
-
Client → New Implementation
|
|
289
|
-
[Old Implementation removed]
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
**Key Principles**:
|
|
293
|
-
- All changes on main/trunk (no long branches)
|
|
294
|
-
- Feature toggles control which implementation is active
|
|
295
|
-
- Both implementations can coexist during transition
|
|
296
|
-
|
|
297
|
-
### Parallel Change (Expand-Migrate-Contract)
|
|
298
|
-
|
|
299
|
-
**Use when**: Changing interfaces used by multiple clients
|
|
300
|
-
|
|
301
|
-
```
|
|
302
|
-
Phase 1: EXPAND
|
|
303
|
-
├─ Add new field/method alongside old
|
|
304
|
-
├─ New code uses new interface
|
|
305
|
-
└─ Old code still works
|
|
306
|
-
|
|
307
|
-
Phase 2: MIGRATE
|
|
308
|
-
├─ Update all clients to use new interface
|
|
309
|
-
├─ Verify all clients migrated
|
|
310
|
-
└─ Data migration (if needed)
|
|
311
|
-
|
|
312
|
-
Phase 3: CONTRACT
|
|
313
|
-
├─ Remove old field/method
|
|
314
|
-
├─ Clean up migration code
|
|
315
|
-
└─ Update documentation
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
---
|
|
319
|
-
|
|
320
489
|
## Database Refactoring
|
|
321
490
|
|
|
322
491
|
### Expand-Contract Pattern for Schema Changes
|
|
@@ -591,6 +760,54 @@ For each debt item, record:
|
|
|
591
760
|
|
|
592
761
|
---
|
|
593
762
|
|
|
763
|
+
## Decision Matrix Summary
|
|
764
|
+
|
|
765
|
+
Quick reference for selecting the appropriate refactoring strategy:
|
|
766
|
+
|
|
767
|
+
| Strategy | Scale | Risk | Key Use Case |
|
|
768
|
+
|----------|-------|------|--------------|
|
|
769
|
+
| **Preparatory Refactoring** | Small | Low | Reduce friction before feature work |
|
|
770
|
+
| **Boy Scout Rule** | Very Small | Low | Continuous debt repayment |
|
|
771
|
+
| **Red-Green-Refactor** | Small | Low | TDD development cycle |
|
|
772
|
+
| **Strangler Fig** | Large | Medium | System replacement, architecture migration |
|
|
773
|
+
| **Anti-Corruption Layer** | Medium | Low | New-legacy system coexistence |
|
|
774
|
+
| **Branch by Abstraction** | Large | Medium | Long-term refactoring on trunk |
|
|
775
|
+
| **Parallel Change** | Medium | Low | Interface/schema migration |
|
|
776
|
+
| **Characterization Tests** | — | — | **Prerequisite for all legacy refactoring** |
|
|
777
|
+
| **Scratch Refactoring** | Small | Low | Understanding black-box code |
|
|
778
|
+
|
|
779
|
+
### Strategy Selection Guide
|
|
780
|
+
|
|
781
|
+
```
|
|
782
|
+
What's your situation?
|
|
783
|
+
|
|
784
|
+
Feature development blocked by messy code?
|
|
785
|
+
└─► Preparatory Refactoring
|
|
786
|
+
|
|
787
|
+
Touching code during a bug fix?
|
|
788
|
+
└─► Boy Scout Rule
|
|
789
|
+
|
|
790
|
+
Writing new code with TDD?
|
|
791
|
+
└─► Red-Green-Refactor
|
|
792
|
+
|
|
793
|
+
Replacing an entire legacy system?
|
|
794
|
+
└─► Strangler Fig Pattern
|
|
795
|
+
|
|
796
|
+
Need to integrate with legacy without being polluted?
|
|
797
|
+
└─► Anti-Corruption Layer
|
|
798
|
+
|
|
799
|
+
Refactoring shared code without feature branches?
|
|
800
|
+
└─► Branch by Abstraction
|
|
801
|
+
|
|
802
|
+
Changing a widely-used interface?
|
|
803
|
+
└─► Parallel Change (Expand-Migrate-Contract)
|
|
804
|
+
|
|
805
|
+
Working with untested legacy code?
|
|
806
|
+
└─► Characterization Tests + Scratch Refactoring FIRST
|
|
807
|
+
```
|
|
808
|
+
|
|
809
|
+
---
|
|
810
|
+
|
|
594
811
|
## Related Standards
|
|
595
812
|
|
|
596
813
|
- [Test-Driven Development](test-driven-development.md) - TDD cycle including refactoring phase
|
|
@@ -607,11 +824,15 @@ For each debt item, record:
|
|
|
607
824
|
- Martin Fowler - "Refactoring: Improving the Design of Existing Code" (2nd Edition, 2018)
|
|
608
825
|
- Michael Feathers - "Working Effectively with Legacy Code" (2004)
|
|
609
826
|
- Joshua Kerievsky - "Refactoring to Patterns" (2004)
|
|
827
|
+
- Kent Beck - "Implementation Patterns" (2007)
|
|
828
|
+
- Robert C. Martin - "Clean Code" (2008)
|
|
829
|
+
- Eric Evans - "Domain-Driven Design" (2003)
|
|
610
830
|
|
|
611
831
|
### Articles
|
|
612
832
|
|
|
613
833
|
- Martin Fowler - [Strangler Fig Application](https://martinfowler.com/bliki/StranglerFigApplication.html)
|
|
614
834
|
- Martin Fowler - [Branch by Abstraction](https://martinfowler.com/bliki/BranchByAbstraction.html)
|
|
835
|
+
- Martin Fowler - [Preparatory Refactoring](https://martinfowler.com/articles/preparatory-refactoring-example.html)
|
|
615
836
|
- Pete Hodgson - [Feature Toggles](https://martinfowler.com/articles/feature-toggles.html)
|
|
616
837
|
- Martin Fowler - [Technical Debt Quadrant](https://martinfowler.com/bliki/TechnicalDebtQuadrant.html)
|
|
617
838
|
|
|
@@ -627,6 +848,7 @@ For each debt item, record:
|
|
|
627
848
|
|
|
628
849
|
| Version | Date | Changes |
|
|
629
850
|
|---------|------|---------|
|
|
851
|
+
| 2.0.0 | 2026-01-21 | Major restructure: Added tactical strategies (Preparatory Refactoring, Boy Scout Rule), Anti-Corruption Layer, Decision Matrix Summary. Reorganized into Tactical/Strategic/Safety categories. |
|
|
630
852
|
| 1.0.0 | 2026-01-12 | Initial refactoring standards definition |
|
|
631
853
|
|
|
632
854
|
---
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
---
|
|
2
2
|
source: ../../../core/refactoring-standards.md
|
|
3
|
-
source_version:
|
|
4
|
-
translation_version:
|
|
5
|
-
last_synced: 2026-01-
|
|
3
|
+
source_version: 2.0.0
|
|
4
|
+
translation_version: 2.0.0
|
|
5
|
+
last_synced: 2026-01-21
|
|
6
6
|
status: current
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
-
#
|
|
9
|
+
# 重构标准
|
|
10
10
|
|
|
11
11
|
> **语言**: [English](../../../core/refactoring-standards.md) | 简体中文
|
|
12
12
|
|
|
13
|
-
**版本**:
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
**版本**: 2.0.0
|
|
14
|
+
**最后更新**: 2026-01-21
|
|
15
|
+
**适用范围**: 所有进行代码改善的软件项目
|
|
16
16
|
|
|
17
17
|
---
|
|
18
18
|
|
|
@@ -629,11 +629,12 @@ PR 规范:
|
|
|
629
629
|
|
|
630
630
|
---
|
|
631
631
|
|
|
632
|
-
##
|
|
632
|
+
## 版本历程
|
|
633
633
|
|
|
634
634
|
| 版本 | 日期 | 变更 |
|
|
635
635
|
|-----|------|------|
|
|
636
|
-
|
|
|
636
|
+
| 2.0.0 | 2026-01-21 | 重大重组:新增战术性策略(预备性重构、童子军规则)、防腐层、决策矩阵摘要。重组为战术性/战略性/安全防护三类。 |
|
|
637
|
+
| 1.0.0 | 2026-01-12 | 初始重构标准定义 |
|
|
637
638
|
|
|
638
639
|
---
|
|
639
640
|
|