@haposoft/cafekit 0.3.2 → 0.3.6

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.
@@ -0,0 +1,695 @@
1
+ # Industry Techniques - Code Change Impact Analysis
2
+
3
+ Tổng hợp các kỹ thuật và công cụ phân tích tác động code changes từ industry và research.
4
+
5
+ ## 📚 Nguồn Tham Khảo
6
+
7
+ Dựa trên research và industry best practices từ:
8
+ - Academic papers (ACM, ResearchGate, arXiv)
9
+ - Static analysis tools (NDepend, CppDepend, SonarQube)
10
+ - Software engineering practices
11
+ - Open source tools
12
+
13
+ ---
14
+
15
+ ## 🎯 Các Phương Pháp Chính
16
+
17
+ ### 1. Traceability-Based Impact Analysis
18
+
19
+ **Khái niệm:**
20
+ Sử dụng links giữa requirements, specifications, design elements, và tests để xác định phạm vi thay đổi.
21
+
22
+ **Cách hoạt động:**
23
+ ```
24
+ Requirement → Design → Code → Tests
25
+ ↓ ↓ ↓ ↓
26
+ Change → Impact → Affected → Test Cases
27
+ ```
28
+
29
+ **Ứng dụng trong Impact Analysis:**
30
+ - Map code changes về requirements
31
+ - Identify affected user stories
32
+ - Generate test scenarios từ requirements
33
+
34
+ **Ví dụ:**
35
+ ```javascript
36
+ // Requirement: FR-1 - User can login with biometric
37
+ // Design: Add BiometricHelper module
38
+ // Code: src/utils/biometric/biometricHelper.ts
39
+ // Tests: Test biometric authentication flow
40
+
41
+ // Khi sửa biometricHelper.ts:
42
+ // → Affected requirement: FR-1
43
+ // → Affected user story: "As a user, I want to login with Face ID"
44
+ // → Test scenarios: Biometric login happy path, error cases
45
+ ```
46
+
47
+ **Tools:**
48
+ - JIRA (requirement tracking)
49
+ - Azure DevOps (work item linking)
50
+ - Custom traceability matrices
51
+
52
+ ---
53
+
54
+ ### 2. Dependency-Based Impact Analysis
55
+
56
+ **Khái niệm:**
57
+ Phân tích dependencies (imports, function calls, class usage) để tìm affected files.
58
+
59
+ **Cách hoạt động:**
60
+
61
+ #### A. Call Graph Analysis
62
+ ```
63
+ Function A calls Function B
64
+ Function B calls Function C
65
+ → Change in C affects B and A
66
+ ```
67
+
68
+ **Ví dụ:**
69
+ ```typescript
70
+ // authService.ts
71
+ export function login(email, password) {
72
+ const user = validateUser(email, password); // calls validateUser
73
+ return generateToken(user); // calls generateToken
74
+ }
75
+
76
+ // Khi sửa validateUser():
77
+ // → Affected: login() function
78
+ // → Affected: All callers of login()
79
+ // → Test: Login flow, validation logic
80
+ ```
81
+
82
+ #### B. Dependency Graph
83
+ ```
84
+ Module A imports Module B
85
+ Module B imports Module C
86
+ → Change in C may affect A through B
87
+ ```
88
+
89
+ **Tools:**
90
+ - **NDepend** (.NET): Dependency matrix, metrics
91
+ - **CppDepend** (C++): Call graphs, architecture validation
92
+ - **Madge** (JavaScript): Circular dependency detection
93
+ - **jdeps** (Java): Package dependencies
94
+
95
+ **Ứng dụng:**
96
+ ```bash
97
+ # Find all files importing authService
98
+ grep -r "from.*authService" src/
99
+
100
+ # Find all function calls to login()
101
+ grep -r "login(" src/
102
+
103
+ # Build dependency graph
104
+ madge --image graph.png src/
105
+ ```
106
+
107
+ ---
108
+
109
+ ### 3. AST-Based Impact Analysis
110
+
111
+ **Khái niệm:**
112
+ Sử dụng Abstract Syntax Tree để phân tích code structure và detect changes ở semantic level.
113
+
114
+ **Cách hoạt động:**
115
+ ```
116
+ Source Code → Parser → AST → Analysis → Impact Report
117
+ ```
118
+
119
+ **AST Nodes:**
120
+ - Function declarations
121
+ - Class definitions
122
+ - Import statements
123
+ - Function calls
124
+ - Variable assignments
125
+
126
+ **Ví dụ:**
127
+ ```javascript
128
+ // Before AST:
129
+ function login(email, password) {
130
+ return authenticate(email, password);
131
+ }
132
+
133
+ // After change:
134
+ function login(email, password, rememberMe) {
135
+ return authenticate(email, password, rememberMe);
136
+ }
137
+
138
+ // AST Analysis detects:
139
+ // - Function signature changed (new parameter)
140
+ // - All callers need update
141
+ // - Breaking change detected
142
+ ```
143
+
144
+ **Tools:**
145
+ - **Babel** (JavaScript): AST transformation
146
+ - **TypeScript Compiler API**: Type-aware AST
147
+ - **Python ast module**: Python AST analysis
148
+ - **Roslyn** (.NET): C# AST analysis
149
+
150
+ **Ứng dụng:**
151
+ ```javascript
152
+ // Detect function signature changes
153
+ const ast = parse(sourceCode);
154
+ ast.body.forEach(node => {
155
+ if (node.type === 'FunctionDeclaration') {
156
+ const params = node.params.length;
157
+ // Check if params changed
158
+ }
159
+ });
160
+ ```
161
+
162
+ ---
163
+
164
+ ### 4. Static Analysis-Based Impact
165
+
166
+ **Khái niệm:**
167
+ Analyze code without executing để detect issues, dependencies, và potential impacts.
168
+
169
+ **Techniques:**
170
+
171
+ #### A. Data Flow Analysis
172
+ Track how data flows through the program:
173
+ ```
174
+ Input → Function A → Function B → Output
175
+ ```
176
+
177
+ **Ví dụ:**
178
+ ```typescript
179
+ // Data flow:
180
+ const email = getUserInput(); // Source
181
+ const validated = validateEmail(email); // Transform
182
+ const user = findUser(validated); // Use
183
+ const token = generateToken(user); // Output
184
+
185
+ // Change in validateEmail():
186
+ // → Affects: findUser, generateToken
187
+ // → Test: Email validation, user lookup
188
+ ```
189
+
190
+ #### B. Control Flow Analysis
191
+ Track execution paths:
192
+ ```
193
+ if (condition) {
194
+ path A
195
+ } else {
196
+ path B
197
+ }
198
+ ```
199
+
200
+ **Ví dụ:**
201
+ ```typescript
202
+ function login(email, password) {
203
+ if (isBiometricEnabled()) {
204
+ return biometricLogin(); // Path A
205
+ } else {
206
+ return passwordLogin(email, password); // Path B
207
+ }
208
+ }
209
+
210
+ // Change in biometricLogin():
211
+ // → Affects: Path A only
212
+ // → Test: Biometric enabled scenario
213
+ ```
214
+
215
+ #### C. Type Analysis
216
+ Track type changes and compatibility:
217
+ ```typescript
218
+ // Before:
219
+ interface User {
220
+ id: string;
221
+ name: string;
222
+ }
223
+
224
+ // After:
225
+ interface User {
226
+ id: string;
227
+ name: string;
228
+ email: string; // New field
229
+ }
230
+
231
+ // Impact:
232
+ // - All User consumers may need update
233
+ // - Serialization/deserialization affected
234
+ // - Database schema may need migration
235
+ ```
236
+
237
+ **Tools:**
238
+ - **SonarQube**: Code quality, security
239
+ - **ESLint**: JavaScript linting
240
+ - **Pylint**: Python static analysis
241
+ - **Checkstyle**: Java code style
242
+
243
+ ---
244
+
245
+ ### 5. Model-Based Impact Analysis
246
+
247
+ **Khái niệm:**
248
+ Sử dụng models (UML, architecture diagrams) để predict impact trước khi code.
249
+
250
+ **Cách hoạt động:**
251
+ ```
252
+ Architecture Model → Component Dependencies → Impact Prediction
253
+ ```
254
+
255
+ **Ví dụ:**
256
+ ```
257
+ [Frontend] → [API Gateway] → [Auth Service] → [Database]
258
+
259
+ [User Service]
260
+
261
+ // Change in Auth Service:
262
+ // → Affects: API Gateway, User Service
263
+ // → May affect: Frontend (if API contract changes)
264
+ ```
265
+
266
+ **Tools:**
267
+ - **Enterprise Architect**: UML modeling
268
+ - **ArchiMate**: Architecture modeling
269
+ - **C4 Model**: Software architecture diagrams
270
+
271
+ ---
272
+
273
+ ### 6. Test-Based Impact Analysis
274
+
275
+ **Khái niệm:**
276
+ Sử dụng test coverage để identify affected tests và features.
277
+
278
+ **Cách hoạt động:**
279
+ ```
280
+ Code Change → Test Coverage Map → Affected Tests → Affected Features
281
+ ```
282
+
283
+ **Ví dụ:**
284
+ ```javascript
285
+ // authService.ts changed
286
+ // → Tests covering authService:
287
+ // - authService.test.ts (unit tests)
288
+ // - login.integration.test.ts (integration tests)
289
+ // - e2e/auth.spec.ts (e2e tests)
290
+ // → Features tested:
291
+ // - Login flow
292
+ // - Token generation
293
+ // - Session management
294
+ ```
295
+
296
+ **Tools:**
297
+ - **Jest**: JavaScript test coverage
298
+ - **Coverage.py**: Python coverage
299
+ - **JaCoCo**: Java code coverage
300
+ - **Istanbul**: JavaScript coverage
301
+
302
+ **Ứng dụng:**
303
+ ```bash
304
+ # Run tests with coverage
305
+ npm test -- --coverage
306
+
307
+ # Find tests covering changed files
308
+ jest --findRelatedTests src/services/authService.ts
309
+
310
+ # Generate coverage report
311
+ jest --coverage --coverageReporters=html
312
+ ```
313
+
314
+ ---
315
+
316
+ ### 7. Behavior-Driven Impact Analysis
317
+
318
+ **Khái niệm:**
319
+ Map code changes về user behaviors và scenarios (BDD approach).
320
+
321
+ **Cách hoạt động:**
322
+ ```
323
+ Code Change → Feature Mapping → User Scenarios → Test Scenarios
324
+ ```
325
+
326
+ **BDD Format:**
327
+ ```gherkin
328
+ Feature: User Login
329
+ Scenario: Login with biometric
330
+ Given user has Face ID enabled
331
+ When user taps "Login with Face ID"
332
+ Then Face ID prompt appears
333
+ And user is logged in after authentication
334
+ ```
335
+
336
+ **Ví dụ:**
337
+ ```typescript
338
+ // Changed: biometricHelper.ts
339
+
340
+ // Affected Feature: User Login
341
+ // Affected Scenarios:
342
+ // - Login with Face ID (iOS)
343
+ // - Login with Touch ID (iOS)
344
+ // - Login with Fingerprint (Android)
345
+ // - Biometric not enrolled
346
+ // - Permission denied
347
+
348
+ // Test Scenarios:
349
+ describe('Biometric Login', () => {
350
+ it('should login with Face ID', async () => {
351
+ // Given
352
+ await enableBiometric();
353
+ // When
354
+ await tapLoginButton();
355
+ // Then
356
+ expect(faceIDPrompt).toBeVisible();
357
+ });
358
+ });
359
+ ```
360
+
361
+ **Tools:**
362
+ - **Cucumber**: BDD testing framework
363
+ - **SpecFlow** (.NET): BDD for .NET
364
+ - **Behave** (Python): BDD for Python
365
+
366
+ ---
367
+
368
+ ### 8. Feature Mapping Techniques
369
+
370
+ **Khái niệm:**
371
+ Map code files về features và user actions.
372
+
373
+ **Techniques:**
374
+
375
+ #### A. Pattern-Based Mapping
376
+ ```javascript
377
+ const featurePatterns = {
378
+ 'Authentication': [
379
+ '**/auth/**',
380
+ '**/login/**',
381
+ '**/services/auth*'
382
+ ],
383
+ 'User Profile': [
384
+ '**/profile/**',
385
+ '**/user/**'
386
+ ]
387
+ };
388
+
389
+ // Match changed files to features
390
+ const changedFiles = ['src/services/authService.ts'];
391
+ const affectedFeatures = matchPatterns(changedFiles, featurePatterns);
392
+ // → ['Authentication']
393
+ ```
394
+
395
+ #### B. Keyword-Based Mapping
396
+ ```javascript
397
+ const featureKeywords = {
398
+ 'Authentication': ['login', 'signin', 'authenticate', 'token'],
399
+ 'Payment': ['payment', 'checkout', 'stripe', 'paypal']
400
+ };
401
+
402
+ // Scan file content for keywords
403
+ const content = readFile('src/services/authService.ts');
404
+ const affectedFeatures = matchKeywords(content, featureKeywords);
405
+ // → ['Authentication']
406
+ ```
407
+
408
+ #### C. Annotation-Based Mapping
409
+ ```typescript
410
+ /**
411
+ * @feature Authentication
412
+ * @userAction Login
413
+ * @priority P0
414
+ */
415
+ export function login(email: string, password: string) {
416
+ // ...
417
+ }
418
+
419
+ // Parse annotations to map features
420
+ ```
421
+
422
+ ---
423
+
424
+ ### 9. Machine Learning-Based Impact Analysis
425
+
426
+ **Khái niệm:**
427
+ Sử dụng ML models để predict impact dựa trên historical data.
428
+
429
+ **Cách hoạt động:**
430
+ ```
431
+ Historical Changes + Outcomes → ML Model → Predict Impact
432
+ ```
433
+
434
+ **Ví dụ:**
435
+ ```python
436
+ # Train model on historical data
437
+ features = [
438
+ 'files_changed',
439
+ 'lines_changed',
440
+ 'complexity',
441
+ 'dependencies_count'
442
+ ]
443
+ labels = [
444
+ 'bugs_introduced',
445
+ 'tests_failed',
446
+ 'features_affected'
447
+ ]
448
+
449
+ model = train_model(features, labels)
450
+
451
+ # Predict impact for new change
452
+ new_change = {
453
+ 'files_changed': 5,
454
+ 'lines_changed': 200,
455
+ 'complexity': 15,
456
+ 'dependencies_count': 8
457
+ }
458
+ predicted_impact = model.predict(new_change)
459
+ # → High risk, 3 features affected, 12 tests may fail
460
+ ```
461
+
462
+ **Tools:**
463
+ - **GPT-based models**: Code understanding
464
+ - **CodeBERT**: Pre-trained model for code
465
+ - **GraphCodeBERT**: Graph-based code model
466
+
467
+ **Research:**
468
+ - "Using GPT for Code-change Impact Analysis" (arXiv 2024)
469
+ - "Enhancing Code Understanding with Transformers" (ACM 2024)
470
+
471
+ ---
472
+
473
+ ## 🛠️ Industry Tools
474
+
475
+ ### Static Analysis Tools
476
+
477
+ | Tool | Language | Features | Use Case |
478
+ |------|----------|----------|----------|
479
+ | **NDepend** | .NET | Dependency matrix, metrics, trends | .NET projects |
480
+ | **CppDepend** | C++ | Call graphs, architecture validation | C++ projects |
481
+ | **SonarQube** | Multi | Code quality, security, coverage | All projects |
482
+ | **Understand** | Multi | Code visualization, metrics | Large codebases |
483
+ | **CodeScene** | Multi | Behavioral code analysis | Team analytics |
484
+
485
+ ### Dependency Analysis Tools
486
+
487
+ | Tool | Language | Features |
488
+ |------|----------|----------|
489
+ | **Madge** | JavaScript | Dependency graphs, circular deps |
490
+ | **jdeps** | Java | Package dependencies |
491
+ | **pipdeptree** | Python | Package dependency tree |
492
+ | **cargo tree** | Rust | Dependency tree |
493
+
494
+ ### AST Tools
495
+
496
+ | Tool | Language | Features |
497
+ |------|----------|----------|
498
+ | **Babel** | JavaScript | AST transformation |
499
+ | **TypeScript Compiler API** | TypeScript | Type-aware AST |
500
+ | **Roslyn** | C# | Compiler as a service |
501
+ | **Python ast** | Python | AST manipulation |
502
+
503
+ ---
504
+
505
+ ## 💡 Best Practices từ Industry
506
+
507
+ ### 1. Combine Multiple Techniques
508
+
509
+ Không dùng một technique duy nhất, mà combine nhiều:
510
+
511
+ ```
512
+ Dependency Analysis + AST Analysis + Test Coverage
513
+ → Comprehensive Impact Report
514
+ ```
515
+
516
+ ### 2. Automate Impact Analysis
517
+
518
+ Integrate vào CI/CD pipeline:
519
+
520
+ ```yaml
521
+ # .github/workflows/impact-analysis.yml
522
+ on: [pull_request]
523
+ jobs:
524
+ impact-analysis:
525
+ runs-on: ubuntu-latest
526
+ steps:
527
+ - uses: actions/checkout@v2
528
+ - name: Run Impact Analysis
529
+ run: npm run impact-analysis
530
+ - name: Comment on PR
531
+ uses: actions/github-script@v6
532
+ with:
533
+ script: |
534
+ github.rest.issues.createComment({
535
+ issue_number: context.issue.number,
536
+ body: impactReport
537
+ })
538
+ ```
539
+
540
+ ### 3. Focus on User Impact
541
+
542
+ Không chỉ technical impact, mà focus vào user impact:
543
+
544
+ ```
545
+ Code Change → Feature Impact → User Action Impact → Test Scenarios
546
+ ```
547
+
548
+ ### 4. Prioritize by Risk
549
+
550
+ Sử dụng risk scoring:
551
+
552
+ ```javascript
553
+ const riskScore =
554
+ (affectedUsers * 3) +
555
+ (dataLossRisk * 5) +
556
+ (securityRisk * 5) +
557
+ (businessImpact * 2);
558
+
559
+ if (riskScore >= 15) return 'CRITICAL';
560
+ if (riskScore >= 10) return 'HIGH';
561
+ if (riskScore >= 5) return 'MEDIUM';
562
+ return 'LOW';
563
+ ```
564
+
565
+ ### 5. Generate Actionable Reports
566
+
567
+ Report phải actionable:
568
+
569
+ ```markdown
570
+ ## Vấn Đề Phát Hiện
571
+
572
+ **1. Thiếu Validate Kích Thước**
573
+ - Vấn đề: [description]
574
+ - Tác động: [impact]
575
+ - Cách sửa: [code example]
576
+ - Thời gian: 15 phút
577
+ ```
578
+
579
+ ---
580
+
581
+ ## 🔬 Research Insights
582
+
583
+ ### Key Findings từ Academic Research:
584
+
585
+ 1. **Traceability IA** (Bohner & Arnold)
586
+ - Links giữa requirements và code giúp predict impact chính xác hơn
587
+ - Traceability matrix giảm 40% effort trong maintenance
588
+
589
+ 2. **Model-Based IA** (Consensus Academic)
590
+ - Model dependencies giúp discover critical components sớm
591
+ - Speed up maintenance process 30-50%
592
+
593
+ 3. **AST-Based Analysis** (ACM 2024)
594
+ - Combining transformers với program dependence graphs
595
+ - Improve accuracy 25% so với traditional methods
596
+
597
+ 4. **Call Graph Analysis** (ResearchGate)
598
+ - Call graphs fundamental cho inter-procedural analysis
599
+ - Enable security analysis, dependency management, debloating
600
+
601
+ 5. **GPT-Based Impact Analysis** (arXiv 2024)
602
+ - LLMs có thể predict impact với 70-80% accuracy
603
+ - Best khi combine với static analysis
604
+
605
+ ---
606
+
607
+ ## 📊 Comparison: Techniques
608
+
609
+ | Technique | Accuracy | Speed | Automation | Best For |
610
+ |-----------|----------|-------|------------|----------|
611
+ | Traceability | High | Slow | Medium | Requirements-driven |
612
+ | Dependency | High | Fast | High | Code-level impact |
613
+ | AST | Very High | Medium | High | Semantic changes |
614
+ | Static Analysis | High | Fast | High | Code quality |
615
+ | Model-Based | Medium | Fast | Medium | Architecture |
616
+ | Test-Based | High | Medium | High | Test coverage |
617
+ | BDD | Medium | Slow | Low | User scenarios |
618
+ | Feature Mapping | Medium | Fast | High | User impact |
619
+ | ML-Based | Medium | Fast | High | Prediction |
620
+
621
+ ---
622
+
623
+ ## 🎯 Recommendations cho Impact Analysis
624
+
625
+ ### Minimum Viable Approach
626
+
627
+ 1. **Dependency Analysis** (must have)
628
+ - Find affected files
629
+ - Build dependency graph
630
+
631
+ 2. **Feature Mapping** (must have)
632
+ - Map files to features
633
+ - Identify user actions
634
+
635
+ 3. **Test Coverage** (should have)
636
+ - Find affected tests
637
+ - Generate test scenarios
638
+
639
+ ### Advanced Approach
640
+
641
+ 4. **AST Analysis** (nice to have)
642
+ - Detect semantic changes
643
+ - Breaking change detection
644
+
645
+ 5. **Static Analysis** (nice to have)
646
+ - Code quality checks
647
+ - Security analysis
648
+
649
+ 6. **ML-Based Prediction** (future)
650
+ - Predict risk
651
+ - Suggest test scenarios
652
+
653
+ ---
654
+
655
+ ## 📚 Further Reading
656
+
657
+ ### Papers
658
+ - "A survey of code-based change impact analysis techniques" (ResearchGate 2013)
659
+ - "Enhancing Code Understanding for Impact Analysis" (ACM 2024)
660
+ - "Using GPT for Code-change Impact Analysis" (arXiv 2024)
661
+
662
+ ### Books
663
+ - "Software Change Impact Analysis" by Bohner & Arnold
664
+ - "Working Effectively with Legacy Code" by Michael Feathers
665
+
666
+ ### Tools Documentation
667
+ - NDepend: https://www.ndepend.com/docs
668
+ - SonarQube: https://docs.sonarqube.org
669
+ - Madge: https://github.com/pahen/madge
670
+
671
+ ### Online Resources
672
+ - AST Explorer: https://astexplorer.net
673
+ - Call Graph Visualization: https://github.com/jrfonseca/gprof2dot
674
+
675
+ ---
676
+
677
+ ## ✅ Integration với Impact Analysis Skill
678
+
679
+ Các techniques này đã được integrate vào skill:
680
+
681
+ 1. ✅ **Dependency Analysis** - `dependency-scouting.md`
682
+ 2. ✅ **Feature Mapping** - `project-detection.md`
683
+ 3. ✅ **Test Scenarios** - `test-scenario-generation.md`
684
+ 4. ✅ **Edge Cases** - `edge-case-identification.md`
685
+ 5. ✅ **Change Detection** - `change-detection.md`
686
+
687
+ **Next steps:**
688
+ - [ ] Add AST-based analysis
689
+ - [ ] Add ML-based prediction
690
+ - [ ] Add automated risk scoring
691
+ - [ ] Add CI/CD integration examples
692
+
693
+ ---
694
+
695
+ **Content rephrased for compliance with licensing restrictions. Original research and tool documentation available at cited sources.**