oh-my-codex-cli 0.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/.agent/skills/agent-kb/HOW_TO_USE.md +428 -0
- package/.agent/skills/agent-kb/README.md +46 -0
- package/.agent/skills/agent-kb/SKILL.md +128 -0
- package/.agent/skills/agent-kb/references/intelligent-analysis-explained.md +333 -0
- package/.agent/skills/agent-kb/references/query-optimization.md +225 -0
- package/.agent/skills/aireview/SKILL.md +704 -0
- package/.agent/skills/analyze/SKILL.md +81 -0
- package/.agent/skills/architect-planner/HOW_TO_USE.md +238 -0
- package/.agent/skills/architect-planner/README.md +41 -0
- package/.agent/skills/architect-planner/SKILL.md +539 -0
- package/.agent/skills/auto-mbti/SKILL.md +291 -0
- package/.agent/skills/autopilot/SKILL.md +222 -0
- package/.agent/skills/backend-patterns/SKILL.md +602 -0
- package/.agent/skills/bdd-generator/README.md +78 -0
- package/.agent/skills/bdd-generator/SKILL.md +436 -0
- package/.agent/skills/brainstorming/HOW_TO_USE.md +289 -0
- package/.agent/skills/brainstorming/README.md +41 -0
- package/.agent/skills/brainstorming/SKILL.md +165 -0
- package/.agent/skills/build-fix/SKILL.md +190 -0
- package/.agent/skills/cancel/SKILL.md +658 -0
- package/.agent/skills/checkpoint/SKILL.md +94 -0
- package/.agent/skills/code-review/SKILL.md +273 -0
- package/.agent/skills/coding-standards/SKILL.md +535 -0
- package/.agent/skills/conductor/SKILL.md +128 -0
- package/.agent/skills/conductor/commands/conductor/implement.toml +358 -0
- package/.agent/skills/conductor/commands/conductor/newTrack.toml +142 -0
- package/.agent/skills/conductor/commands/conductor/revert.toml +123 -0
- package/.agent/skills/conductor/commands/conductor/setup.toml +429 -0
- package/.agent/skills/conductor/commands/conductor/status.toml +57 -0
- package/.agent/skills/conductor/scripts/install.sh +89 -0
- package/.agent/skills/conductor/templates/code_styleguides/csharp.md +115 -0
- package/.agent/skills/conductor/templates/code_styleguides/dart.md +238 -0
- package/.agent/skills/conductor/templates/code_styleguides/general.md +23 -0
- package/.agent/skills/conductor/templates/code_styleguides/go.md +48 -0
- package/.agent/skills/conductor/templates/code_styleguides/html-css.md +49 -0
- package/.agent/skills/conductor/templates/code_styleguides/javascript.md +51 -0
- package/.agent/skills/conductor/templates/code_styleguides/python.md +37 -0
- package/.agent/skills/conductor/templates/code_styleguides/typescript.md +43 -0
- package/.agent/skills/conductor/templates/rules/README.md +23 -0
- package/.agent/skills/conductor/templates/rules/agents.md +49 -0
- package/.agent/skills/conductor/templates/rules/coding-style.md +70 -0
- package/.agent/skills/conductor/templates/rules/dev.md +20 -0
- package/.agent/skills/conductor/templates/rules/git-workflow.md +45 -0
- package/.agent/skills/conductor/templates/rules/hooks.md +6 -0
- package/.agent/skills/conductor/templates/rules/patterns.md +55 -0
- package/.agent/skills/conductor/templates/rules/performance.md +47 -0
- package/.agent/skills/conductor/templates/rules/research.md +26 -0
- package/.agent/skills/conductor/templates/rules/review.md +22 -0
- package/.agent/skills/conductor/templates/rules/security.md +36 -0
- package/.agent/skills/conductor/templates/rules/testing.md +30 -0
- package/.agent/skills/conductor/templates/workflow.md +333 -0
- package/.agent/skills/consensus/HOW_TO_USE.md +191 -0
- package/.agent/skills/consensus/README.md +41 -0
- package/.agent/skills/consensus/SKILL.md +317 -0
- package/.agent/skills/content-research-writer/SKILL.md +537 -0
- package/.agent/skills/debug-analysis/SKILL.md +331 -0
- package/.agent/skills/deepinit/SKILL.md +347 -0
- package/.agent/skills/deepsearch/SKILL.md +56 -0
- package/.agent/skills/doctor/SKILL.md +158 -0
- package/.agent/skills/drawio/EXAMPLES.md +382 -0
- package/.agent/skills/drawio/QUICK_START.md +237 -0
- package/.agent/skills/drawio/README.md +315 -0
- package/.agent/skills/drawio/SETUP_GUIDE.md +254 -0
- package/.agent/skills/drawio/SKILL.md +1176 -0
- package/.agent/skills/e2e/SKILL.md +396 -0
- package/.agent/skills/ecomode/SKILL.md +160 -0
- package/.agent/skills/electron-driver/SKILL.md +144 -0
- package/.agent/skills/electron-driver/scripts/driver-template.js +71 -0
- package/.agent/skills/eval/SKILL.md +140 -0
- package/.agent/skills/eval-harness/SKILL.md +242 -0
- package/.agent/skills/evolve/SKILL.md +213 -0
- package/.agent/skills/frontend-design/SKILL.md +42 -0
- package/.agent/skills/frontend-patterns/SKILL.md +646 -0
- package/.agent/skills/frontend-ui-ux/SKILL.md +70 -0
- package/.agent/skills/git-master/SKILL.md +75 -0
- package/.agent/skills/help/SKILL.md +89 -0
- package/.agent/skills/iterative-retrieval/SKILL.md +217 -0
- package/.agent/skills/local-skills-setup/SKILL.md +483 -0
- package/.agent/skills/log-analyzer/SKILL.md +187 -0
- package/.agent/skills/mcp-setup/SKILL.md +226 -0
- package/.agent/skills/multi-model-research/HOW_TO_USE.md +614 -0
- package/.agent/skills/multi-model-research/README.md +233 -0
- package/.agent/skills/multi-model-research/SKILL.md +541 -0
- package/.agent/skills/multi-model-research/references/troubleshooting.md +415 -0
- package/.agent/skills/note/SKILL.md +80 -0
- package/.agent/skills/omc-setup/SKILL.md +219 -0
- package/.agent/skills/orchestrate/SKILL.md +620 -0
- package/.agent/skills/patent-workflow/IMPLEMENTATION_SUMMARY.md +500 -0
- package/.agent/skills/patent-workflow/README.md +455 -0
- package/.agent/skills/patent-workflow/SKILL.md +1036 -0
- package/.agent/skills/patent-workflow/tools/irr_checker.py +260 -0
- package/.agent/skills/patent-workflow/tools/sample_terminology.json +49 -0
- package/.agent/skills/patent-workflow/tools/term_checker.py +355 -0
- package/.agent/skills/pattern-recognition/SKILL.md +792 -0
- package/.agent/skills/pipeline/SKILL.md +448 -0
- package/.agent/skills/plan/SKILL.md +309 -0
- package/.agent/skills/planning-methodology/SKILL.md +370 -0
- package/.agent/skills/planning-with-files/SKILL.md +210 -0
- package/.agent/skills/planning-with-files/examples.md +202 -0
- package/.agent/skills/planning-with-files/reference.md +218 -0
- package/.agent/skills/planning-with-files/scripts/check-complete.ps1 +42 -0
- package/.agent/skills/planning-with-files/scripts/check-complete.sh +44 -0
- package/.agent/skills/planning-with-files/scripts/init-session.ps1 +120 -0
- package/.agent/skills/planning-with-files/scripts/init-session.sh +120 -0
- package/.agent/skills/planning-with-files/scripts/session-catchup.py +208 -0
- package/.agent/skills/planning-with-files/templates/findings.md +95 -0
- package/.agent/skills/planning-with-files/templates/progress.md +114 -0
- package/.agent/skills/planning-with-files/templates/task_plan.md +132 -0
- package/.agent/skills/project-analyze/CLAUDE.md +18 -0
- package/.agent/skills/project-analyze/HOW_TO_USE.md +145 -0
- package/.agent/skills/project-analyze/README.md +42 -0
- package/.agent/skills/project-analyze/SKILL.md +289 -0
- package/.agent/skills/project-analyze/SKILL.md.backup +287 -0
- package/.agent/skills/project-analyze/SKILL.md.backup_20260105_093646 +287 -0
- package/.agent/skills/project-analyze/assets/analysis-report-template.md +433 -0
- package/.agent/skills/project-analyze/references/analysis-patterns.md +422 -0
- package/.agent/skills/project-analyze/references/projectmind-explained.md +535 -0
- package/.agent/skills/project-session-manager/SKILL.md +428 -0
- package/.agent/skills/project-session-manager/lib/config.sh +86 -0
- package/.agent/skills/project-session-manager/lib/parse.sh +121 -0
- package/.agent/skills/project-session-manager/lib/session.sh +132 -0
- package/.agent/skills/project-session-manager/lib/tmux.sh +103 -0
- package/.agent/skills/project-session-manager/lib/worktree.sh +171 -0
- package/.agent/skills/project-session-manager/psm.sh +629 -0
- package/.agent/skills/project-session-manager/templates/feature.md +56 -0
- package/.agent/skills/project-session-manager/templates/issue-fix.md +57 -0
- package/.agent/skills/project-session-manager/templates/pr-review.md +65 -0
- package/.agent/skills/project-session-manager/templates/projects.json +19 -0
- package/.agent/skills/quality-check/HOW_TO_USE.md +171 -0
- package/.agent/skills/quality-check/README.md +50 -0
- package/.agent/skills/quality-check/SKILL.md +240 -0
- package/.agent/skills/quality-check/SKILL.md.backup +238 -0
- package/.agent/skills/quality-check/SKILL.md.backup_20260105_093646 +238 -0
- package/.agent/skills/quality-check/assets/quality-report-template.md +437 -0
- package/.agent/skills/quality-check/references/refactoring-patterns.md +550 -0
- package/.agent/skills/quality-check/references/scoring-criteria.md +454 -0
- package/.agent/skills/quality-validation/SKILL.md +519 -0
- package/.agent/skills/quality-validation/SKILL.md.backup +573 -0
- package/.agent/skills/quality-validation/SKILL.md.backup_20260105_093646 +573 -0
- package/.agent/skills/ralph/SKILL.md +236 -0
- package/.agent/skills/ralph-init/SKILL.md +78 -0
- package/.agent/skills/ralplan/SKILL.md +58 -0
- package/.agent/skills/refactor-clean/SKILL.md +49 -0
- package/.agent/skills/release/SKILL.md +84 -0
- package/.agent/skills/research/SKILL.md +526 -0
- package/.agent/skills/research-methodology/SKILL.md +268 -0
- package/.agent/skills/review/SKILL.md +53 -0
- package/.agent/skills/security-review/SKILL.md +509 -0
- package/.agent/skills/security-review/cloud-infrastructure-security.md +361 -0
- package/.agent/skills/setup-pm/SKILL.md +102 -0
- package/.agent/skills/skill/SKILL.md +424 -0
- package/.agent/skills/skill-create/SKILL.md +209 -0
- package/.agent/skills/skill-debugger/HOW_TO_USE.md +244 -0
- package/.agent/skills/skill-debugger/README.md +44 -0
- package/.agent/skills/skill-debugger/SKILL.md +326 -0
- package/.agent/skills/skill-debugger/diagnostic_checklist.md +115 -0
- package/.agent/skills/skill-development/SKILL.md +661 -0
- package/.agent/skills/skill-development/references/skill-creator-original.md +209 -0
- package/.agent/skills/skill-doc-generator/README.md +37 -0
- package/.agent/skills/skill-doc-generator/SKILL.md +331 -0
- package/.agent/skills/skill-quality-analyzer/HOW_TO_USE.md +243 -0
- package/.agent/skills/skill-quality-analyzer/README.md +61 -0
- package/.agent/skills/skill-quality-analyzer/SKILL.md +247 -0
- package/.agent/skills/skill-quality-analyzer/analyzer.py +209 -0
- package/.agent/skills/skill-quality-analyzer/expected_output.json +81 -0
- package/.agent/skills/skill-quality-analyzer/sample_input.json +9 -0
- package/.agent/skills/skill-tester/README.md +46 -0
- package/.agent/skills/skill-tester/SKILL.md +345 -0
- package/.agent/skills/start-dev/SKILL.md +701 -0
- package/.agent/skills/swarm/SKILL.md +691 -0
- package/.agent/skills/task-kb-lookup/SKILL.md +211 -0
- package/.agent/skills/task-kb-record/SKILL.md +417 -0
- package/.agent/skills/tdd/SKILL.md +446 -0
- package/.agent/skills/tdd-generator/DEMO.md +516 -0
- package/.agent/skills/tdd-generator/README.md +89 -0
- package/.agent/skills/tdd-generator/SKILL.md +278 -0
- package/.agent/skills/tdd-workflow/SKILL.md +424 -0
- package/.agent/skills/test-coverage/SKILL.md +48 -0
- package/.agent/skills/thinkdeep/HOW_TO_USE.md +183 -0
- package/.agent/skills/thinkdeep/README.md +41 -0
- package/.agent/skills/thinkdeep/SKILL.md +343 -0
- package/.agent/skills/ui-ux-pro-max/SKILL.md +228 -0
- package/.agent/skills/ui-ux-pro-max/data/charts.csv +26 -0
- package/.agent/skills/ui-ux-pro-max/data/colors.csv +97 -0
- package/.agent/skills/ui-ux-pro-max/data/landing.csv +31 -0
- package/.agent/skills/ui-ux-pro-max/data/products.csv +97 -0
- package/.agent/skills/ui-ux-pro-max/data/prompts.csv +24 -0
- package/.agent/skills/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
- package/.agent/skills/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
- package/.agent/skills/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
- package/.agent/skills/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
- package/.agent/skills/ui-ux-pro-max/data/stacks/react.csv +54 -0
- package/.agent/skills/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
- package/.agent/skills/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
- package/.agent/skills/ui-ux-pro-max/data/stacks/vue.csv +50 -0
- package/.agent/skills/ui-ux-pro-max/data/styles.csv +59 -0
- package/.agent/skills/ui-ux-pro-max/data/typography.csv +58 -0
- package/.agent/skills/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
- package/.agent/skills/ui-ux-pro-max/scripts/core.py +236 -0
- package/.agent/skills/ui-ux-pro-max/scripts/search.py +61 -0
- package/.agent/skills/ultrapilot/SKILL.md +647 -0
- package/.agent/skills/ultraqa/SKILL.md +152 -0
- package/.agent/skills/ultrawork/SKILL.md +123 -0
- package/.agent/skills/update-codemaps/SKILL.md +38 -0
- package/.agent/skills/update-docs/SKILL.md +52 -0
- package/.agent/skills/verification-loop/SKILL.md +140 -0
- package/.agent/skills/verify/SKILL.md +80 -0
- package/.agent/skills/writer-memory/SKILL.md +459 -0
- package/.agent/skills/writer-memory/lib/character-tracker.ts +338 -0
- package/.agent/skills/writer-memory/lib/memory-manager.ts +804 -0
- package/.agent/skills/writer-memory/lib/relationship-graph.ts +400 -0
- package/.agent/skills/writer-memory/lib/scene-organizer.ts +544 -0
- package/.agent/skills/writer-memory/lib/synopsis-builder.ts +339 -0
- package/.agent/skills/writer-memory/templates/synopsis-template.md +46 -0
- package/.governance/skill-lint.allowlist +4 -0
- package/.governance/skill-llm.allowlist +4 -0
- package/AGENTS.md +59 -0
- package/LICENSE +21 -0
- package/README.md +169 -0
- package/README.zh.md +145 -0
- package/bin/omcodex.js +8 -0
- package/commands/conductor/implement.toml +358 -0
- package/commands/conductor/newTrack.toml +142 -0
- package/commands/conductor/revert.toml +123 -0
- package/commands/conductor/setup.toml +429 -0
- package/commands/conductor/status.toml +57 -0
- package/docs/ALIGNMENT.md +40 -0
- package/docs/CODEX.md +133 -0
- package/docs/NOTIFY.md +81 -0
- package/docs/SKILL_GOVERNANCE.md +72 -0
- package/docs/SKILL_GOVERNANCE_FRAMEWORK.md +182 -0
- package/docs/SKILL_GOVERNANCE_FRAMEWORK.zh.md +170 -0
- package/package.json +50 -0
- package/prompts/architect.md +105 -0
- package/prompts/executor.md +134 -0
- package/prompts/planner.md +113 -0
- package/scripts/check-skill-governance.sh +84 -0
- package/scripts/check-skill-llm-governance.js +302 -0
- package/scripts/eval-skills.js +217 -0
- package/scripts/generate-catalog-docs.js +95 -0
- package/scripts/generate-codex-mcp-config.sh +22 -0
- package/scripts/install-codex-force.sh +5 -0
- package/scripts/install-codex-incremental.sh +5 -0
- package/scripts/install-codex.sh +79 -0
- package/scripts/notify-dispatch.js +15 -0
- package/scripts/setup-package-manager.js +137 -0
- package/src/catalog/generated/public-catalog.json +547 -0
- package/src/catalog/manifest.json +542 -0
- package/src/catalog/reader.js +43 -0
- package/src/catalog/schema.js +79 -0
- package/src/cli/doctor.js +62 -0
- package/src/cli/index.js +85 -0
- package/src/cli/notify.js +127 -0
- package/src/cli/route.js +43 -0
- package/src/cli/setup.js +155 -0
- package/src/cli/team.js +125 -0
- package/src/config/generator.js +119 -0
- package/src/mcp/memory-server.js +241 -0
- package/src/mcp/state-server.js +112 -0
- package/src/mcp/trace-server.js +168 -0
- package/src/notify/dispatch.js +74 -0
- package/src/notify/extensibility/dispatcher.js +113 -0
- package/src/notify/extensibility/events.js +15 -0
- package/src/notify/extensibility/loader.js +54 -0
- package/src/router/skill-router.js +90 -0
- package/src/team/auto-advance.js +72 -0
- package/src/team/orchestrator.js +82 -0
- package/src/team/state-store.js +33 -0
- package/src/utils/paths.js +33 -0
- package/templates/AGENTS.md +15 -0
- package/templates/catalog-manifest.json +542 -0
- package/templates/code_styleguides/csharp.md +115 -0
- package/templates/code_styleguides/dart.md +238 -0
- package/templates/code_styleguides/general.md +23 -0
- package/templates/code_styleguides/go.md +48 -0
- package/templates/code_styleguides/html-css.md +49 -0
- package/templates/code_styleguides/javascript.md +51 -0
- package/templates/code_styleguides/python.md +37 -0
- package/templates/code_styleguides/typescript.md +43 -0
- package/templates/rules/README.md +23 -0
- package/templates/rules/agents.md +49 -0
- package/templates/rules/coding-style.md +70 -0
- package/templates/rules/dev.md +20 -0
- package/templates/rules/git-workflow.md +45 -0
- package/templates/rules/notify.md +6 -0
- package/templates/rules/patterns.md +55 -0
- package/templates/rules/performance.md +47 -0
- package/templates/rules/research.md +26 -0
- package/templates/rules/review.md +22 -0
- package/templates/rules/security.md +36 -0
- package/templates/rules/testing.md +30 -0
- package/templates/workflow.md +333 -0
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
# Common Refactoring Patterns
|
|
2
|
+
|
|
3
|
+
## ROI-Optimized Refactoring Strategy
|
|
4
|
+
|
|
5
|
+
**Principle**: Fix high-impact, low-effort issues first
|
|
6
|
+
|
|
7
|
+
### Priority Matrix
|
|
8
|
+
|
|
9
|
+
| Impact \ Effort | Low Effort | Medium Effort | High Effort |
|
|
10
|
+
|----------------|------------|---------------|-------------|
|
|
11
|
+
| **High Impact** | **DO FIRST** | Do Second | Consider |
|
|
12
|
+
| **Medium Impact** | Do Second | Consider | Defer |
|
|
13
|
+
| **Low Impact** | Quick Wins | Defer | Avoid |
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Pattern 1: Extract Magic Numbers
|
|
18
|
+
|
|
19
|
+
**When**: Code has hardcoded values without explanation
|
|
20
|
+
|
|
21
|
+
**Example**:
|
|
22
|
+
```javascript
|
|
23
|
+
// Before (Poor - Clarity: 3, Maintainability: 2)
|
|
24
|
+
if (user.age >= 18 && user.accountBalance > 1000) {
|
|
25
|
+
approveCredit();
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Refactored**:
|
|
30
|
+
```javascript
|
|
31
|
+
// After (Better - Clarity: 9, Maintainability: 8)
|
|
32
|
+
const MINIMUM_AGE = 18;
|
|
33
|
+
const MINIMUM_BALANCE_FOR_CREDIT = 1000;
|
|
34
|
+
|
|
35
|
+
if (user.age >= MINIMUM_AGE && user.accountBalance > MINIMUM_BALANCE_FOR_CREDIT) {
|
|
36
|
+
approveCredit();
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Impact**: High (improves clarity, makes values discoverable)
|
|
41
|
+
**Effort**: Low (5-10 minutes)
|
|
42
|
+
**Quality Gain**: +6 Clarity, +6 Maintainability
|
|
43
|
+
**ROI**: ⭐⭐⭐⭐⭐
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Pattern 2: Extract Method
|
|
48
|
+
|
|
49
|
+
**When**: Function does multiple things or has complex logic
|
|
50
|
+
|
|
51
|
+
**Example**:
|
|
52
|
+
```javascript
|
|
53
|
+
// Before (Poor - Structure: 4, Clarity: 4, Maintainability: 3)
|
|
54
|
+
function processOrder(order) {
|
|
55
|
+
// Validate
|
|
56
|
+
if (!order.items || order.items.length === 0) {
|
|
57
|
+
throw new Error('No items');
|
|
58
|
+
}
|
|
59
|
+
if (!order.shippingAddress) {
|
|
60
|
+
throw new Error('No address');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Calculate total
|
|
64
|
+
let total = 0;
|
|
65
|
+
for (let item of order.items) {
|
|
66
|
+
total += item.price * item.quantity;
|
|
67
|
+
}
|
|
68
|
+
const tax = total * 0.08;
|
|
69
|
+
const shipping = total > 50 ? 0 : 5.99;
|
|
70
|
+
total = total + tax + shipping;
|
|
71
|
+
|
|
72
|
+
// Save
|
|
73
|
+
database.save(order);
|
|
74
|
+
sendConfirmationEmail(order);
|
|
75
|
+
|
|
76
|
+
return total;
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Refactored**:
|
|
81
|
+
```javascript
|
|
82
|
+
// After (Better - Structure: 9, Clarity: 9, Maintainability: 9)
|
|
83
|
+
function processOrder(order) {
|
|
84
|
+
validateOrder(order);
|
|
85
|
+
const total = calculateOrderTotal(order);
|
|
86
|
+
saveAndNotify(order);
|
|
87
|
+
return total;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function validateOrder(order) {
|
|
91
|
+
if (!order.items || order.items.length === 0) {
|
|
92
|
+
throw new Error('Order must contain items');
|
|
93
|
+
}
|
|
94
|
+
if (!order.shippingAddress) {
|
|
95
|
+
throw new Error('Order must have shipping address');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function calculateOrderTotal(order) {
|
|
100
|
+
const subtotal = order.items.reduce((sum, item) => sum + item.price * item.quantity, 0);
|
|
101
|
+
const tax = subtotal * 0.08;
|
|
102
|
+
const shipping = subtotal > 50 ? 0 : 5.99;
|
|
103
|
+
return subtotal + tax + shipping;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function saveAndNotify(order) {
|
|
107
|
+
database.save(order);
|
|
108
|
+
sendConfirmationEmail(order);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**Impact**: High (improves structure, testability, readability)
|
|
113
|
+
**Effort**: Low-Medium (15-30 minutes)
|
|
114
|
+
**Quality Gain**: +5 Structure, +5 Clarity, +6 Maintainability
|
|
115
|
+
**ROI**: ⭐⭐⭐⭐⭐
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Pattern 3: Replace Conditional with Guard Clauses
|
|
120
|
+
|
|
121
|
+
**When**: Deep nesting due to conditional checks
|
|
122
|
+
|
|
123
|
+
**Example**:
|
|
124
|
+
```javascript
|
|
125
|
+
// Before (Poor - Clarity: 4, Maintainability: 3)
|
|
126
|
+
function calculateDiscount(user, order) {
|
|
127
|
+
if (user) {
|
|
128
|
+
if (user.isPremium) {
|
|
129
|
+
if (order.total > 100) {
|
|
130
|
+
return order.total * 0.2;
|
|
131
|
+
} else {
|
|
132
|
+
return order.total * 0.1;
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**Refactored**:
|
|
144
|
+
```javascript
|
|
145
|
+
// After (Better - Clarity: 8, Maintainability: 8)
|
|
146
|
+
function calculateDiscount(user, order) {
|
|
147
|
+
if (!user || !user.isPremium) {
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return order.total > 100 ? order.total * 0.2 : order.total * 0.1;
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**Impact**: Medium (improves readability)
|
|
156
|
+
**Effort**: Low (5-10 minutes)
|
|
157
|
+
**Quality Gain**: +4 Clarity, +5 Maintainability
|
|
158
|
+
**ROI**: ⭐⭐⭐⭐
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
## Pattern 4: Introduce Parameter Object
|
|
163
|
+
|
|
164
|
+
**When**: Functions have too many parameters (> 4)
|
|
165
|
+
|
|
166
|
+
**Example**:
|
|
167
|
+
```javascript
|
|
168
|
+
// Before (Poor - Clarity: 4, Maintainability: 3, Structure: 4)
|
|
169
|
+
function createUser(firstName, lastName, email, phone, address, city, state, zip, country) {
|
|
170
|
+
// Implementation
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
createUser('John', 'Doe', 'john@example.com', '555-1234', '123 Main St', 'Seattle', 'WA', '98101', 'USA');
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
**Refactored**:
|
|
177
|
+
```javascript
|
|
178
|
+
// After (Better - Clarity: 8, Maintainability: 9, Structure: 9)
|
|
179
|
+
function createUser(userDetails) {
|
|
180
|
+
const { firstName, lastName, email, phone, address } = userDetails;
|
|
181
|
+
// Implementation
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
createUser({
|
|
185
|
+
firstName: 'John',
|
|
186
|
+
lastName: 'Doe',
|
|
187
|
+
email: 'john@example.com',
|
|
188
|
+
phone: '555-1234',
|
|
189
|
+
address: {
|
|
190
|
+
street: '123 Main St',
|
|
191
|
+
city: 'Seattle',
|
|
192
|
+
state: 'WA',
|
|
193
|
+
zip: '98101',
|
|
194
|
+
country: 'USA'
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
**Impact**: Medium-High (improves API usability, extensibility)
|
|
200
|
+
**Effort**: Medium (20-40 minutes)
|
|
201
|
+
**Quality Gain**: +4 Clarity, +6 Maintainability, +5 Structure
|
|
202
|
+
**ROI**: ⭐⭐⭐⭐
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
## Pattern 5: Replace Type Code with Polymorphism
|
|
207
|
+
|
|
208
|
+
**When**: Long if-else chains based on type codes
|
|
209
|
+
|
|
210
|
+
**Example**:
|
|
211
|
+
```javascript
|
|
212
|
+
// Before (Poor - Structure: 3, Maintainability: 3, Best Practices: 4)
|
|
213
|
+
function calculateShipping(order) {
|
|
214
|
+
if (order.shippingMethod === 'standard') {
|
|
215
|
+
return order.weight * 0.5;
|
|
216
|
+
} else if (order.shippingMethod === 'express') {
|
|
217
|
+
return order.weight * 1.5 + 10;
|
|
218
|
+
} else if (order.shippingMethod === 'overnight') {
|
|
219
|
+
return order.weight * 3 + 25;
|
|
220
|
+
} else {
|
|
221
|
+
throw new Error('Unknown shipping method');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
**Refactored**:
|
|
227
|
+
```javascript
|
|
228
|
+
// After (Better - Structure: 9, Maintainability: 9, Best Practices: 9)
|
|
229
|
+
class ShippingStrategy {
|
|
230
|
+
calculate(order) {
|
|
231
|
+
throw new Error('Must implement calculate()');
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
class StandardShipping extends ShippingStrategy {
|
|
236
|
+
calculate(order) {
|
|
237
|
+
return order.weight * 0.5;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
class ExpressShipping extends ShippingStrategy {
|
|
242
|
+
calculate(order) {
|
|
243
|
+
return order.weight * 1.5 + 10;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
class OvernightShipping extends ShippingStrategy {
|
|
248
|
+
calculate(order) {
|
|
249
|
+
return order.weight * 3 + 25;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const shippingStrategies = {
|
|
254
|
+
'standard': new StandardShipping(),
|
|
255
|
+
'express': new ExpressShipping(),
|
|
256
|
+
'overnight': new OvernightShipping()
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
function calculateShipping(order) {
|
|
260
|
+
const strategy = shippingStrategies[order.shippingMethod];
|
|
261
|
+
if (!strategy) {
|
|
262
|
+
throw new Error(`Unknown shipping method: ${order.shippingMethod}`);
|
|
263
|
+
}
|
|
264
|
+
return strategy.calculate(order);
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
**Impact**: High (extensibility, testability, follows OCP)
|
|
269
|
+
**Effort**: High (1-2 hours)
|
|
270
|
+
**Quality Gain**: +6 Structure, +6 Maintainability, +5 Best Practices
|
|
271
|
+
**ROI**: ⭐⭐⭐
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
## Pattern 6: Consolidate Duplicate Code
|
|
276
|
+
|
|
277
|
+
**When**: Same or similar code appears in multiple places
|
|
278
|
+
|
|
279
|
+
**Example**:
|
|
280
|
+
```javascript
|
|
281
|
+
// Before (Poor - Structure: 3, Maintainability: 2)
|
|
282
|
+
function sendWelcomeEmail(user) {
|
|
283
|
+
const emailBody = `Welcome, ${user.name}!`;
|
|
284
|
+
emailService.send(user.email, 'Welcome', emailBody);
|
|
285
|
+
logger.log(`Sent welcome email to ${user.email}`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function sendResetPasswordEmail(user) {
|
|
289
|
+
const emailBody = `Reset your password: ${resetLink}`;
|
|
290
|
+
emailService.send(user.email, 'Reset Password', emailBody);
|
|
291
|
+
logger.log(`Sent reset password email to ${user.email}`);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function sendOrderConfirmationEmail(user, order) {
|
|
295
|
+
const emailBody = `Order ${order.id} confirmed!`;
|
|
296
|
+
emailService.send(user.email, 'Order Confirmation', emailBody);
|
|
297
|
+
logger.log(`Sent order confirmation email to ${user.email}`);
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
**Refactored**:
|
|
302
|
+
```javascript
|
|
303
|
+
// After (Better - Structure: 8, Maintainability: 9)
|
|
304
|
+
function sendEmail(user, subject, body) {
|
|
305
|
+
emailService.send(user.email, subject, body);
|
|
306
|
+
logger.log(`Sent "${subject}" email to ${user.email}`);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function sendWelcomeEmail(user) {
|
|
310
|
+
sendEmail(user, 'Welcome', `Welcome, ${user.name}!`);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function sendResetPasswordEmail(user, resetLink) {
|
|
314
|
+
sendEmail(user, 'Reset Password', `Reset your password: ${resetLink}`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function sendOrderConfirmationEmail(user, order) {
|
|
318
|
+
sendEmail(user, 'Order Confirmation', `Order ${order.id} confirmed!`);
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
**Impact**: High (DRY, single source of truth)
|
|
323
|
+
**Effort**: Low-Medium (15-30 minutes)
|
|
324
|
+
**Quality Gain**: +5 Structure, +7 Maintainability
|
|
325
|
+
**ROI**: ⭐⭐⭐⭐⭐
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## Pattern 7: Add Input Validation
|
|
330
|
+
|
|
331
|
+
**When**: Functions assume inputs are always valid
|
|
332
|
+
|
|
333
|
+
**Example**:
|
|
334
|
+
```javascript
|
|
335
|
+
// Before (Poor - Trigger Detection: 2, Best Practices: 3)
|
|
336
|
+
function divide(a, b) {
|
|
337
|
+
return a / b;
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**Refactored**:
|
|
342
|
+
```javascript
|
|
343
|
+
// After (Better - Trigger Detection: 9, Best Practices: 9)
|
|
344
|
+
function divide(a, b) {
|
|
345
|
+
if (typeof a !== 'number' || typeof b !== 'number') {
|
|
346
|
+
throw new TypeError('Both arguments must be numbers');
|
|
347
|
+
}
|
|
348
|
+
if (b === 0) {
|
|
349
|
+
throw new Error('Division by zero is not allowed');
|
|
350
|
+
}
|
|
351
|
+
if (!Number.isFinite(a) || !Number.isFinite(b)) {
|
|
352
|
+
throw new Error('Arguments must be finite numbers');
|
|
353
|
+
}
|
|
354
|
+
return a / b;
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
**Impact**: High (prevents bugs, improves robustness)
|
|
359
|
+
**Effort**: Low (10-15 minutes)
|
|
360
|
+
**Quality Gain**: +7 Trigger Detection, +6 Best Practices
|
|
361
|
+
**ROI**: ⭐⭐⭐⭐⭐
|
|
362
|
+
|
|
363
|
+
---
|
|
364
|
+
|
|
365
|
+
## Pattern 8: Replace Nested Callbacks with Async/Await
|
|
366
|
+
|
|
367
|
+
**When**: Callback hell in asynchronous code
|
|
368
|
+
|
|
369
|
+
**Example**:
|
|
370
|
+
```javascript
|
|
371
|
+
// Before (Poor - Clarity: 3, Best Practices: 3, Maintainability: 2)
|
|
372
|
+
function processOrder(orderId, callback) {
|
|
373
|
+
getOrder(orderId, function(err, order) {
|
|
374
|
+
if (err) return callback(err);
|
|
375
|
+
|
|
376
|
+
validateOrder(order, function(err, valid) {
|
|
377
|
+
if (err) return callback(err);
|
|
378
|
+
if (!valid) return callback(new Error('Invalid order'));
|
|
379
|
+
|
|
380
|
+
chargePayment(order, function(err, receipt) {
|
|
381
|
+
if (err) return callback(err);
|
|
382
|
+
|
|
383
|
+
sendConfirmation(order, function(err) {
|
|
384
|
+
if (err) return callback(err);
|
|
385
|
+
callback(null, { success: true, receipt });
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
**Refactored**:
|
|
394
|
+
```javascript
|
|
395
|
+
// After (Better - Clarity: 9, Best Practices: 9, Maintainability: 9)
|
|
396
|
+
async function processOrder(orderId) {
|
|
397
|
+
try {
|
|
398
|
+
const order = await getOrder(orderId);
|
|
399
|
+
const valid = await validateOrder(order);
|
|
400
|
+
|
|
401
|
+
if (!valid) {
|
|
402
|
+
throw new Error('Invalid order');
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const receipt = await chargePayment(order);
|
|
406
|
+
await sendConfirmation(order);
|
|
407
|
+
|
|
408
|
+
return { success: true, receipt };
|
|
409
|
+
} catch (error) {
|
|
410
|
+
logger.error('Order processing failed', { orderId, error });
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
**Impact**: Very High (readability, error handling, modern practices)
|
|
417
|
+
**Effort**: Medium (30-60 minutes)
|
|
418
|
+
**Quality Gain**: +6 Clarity, +6 Best Practices, +7 Maintainability
|
|
419
|
+
**ROI**: ⭐⭐⭐⭐⭐
|
|
420
|
+
|
|
421
|
+
---
|
|
422
|
+
|
|
423
|
+
## Pattern 9: Improve Error Messages
|
|
424
|
+
|
|
425
|
+
**When**: Error messages are vague or unhelpful
|
|
426
|
+
|
|
427
|
+
**Example**:
|
|
428
|
+
```javascript
|
|
429
|
+
// Before (Poor - Trigger Detection: 4, Examples: 3)
|
|
430
|
+
function validateEmail(email) {
|
|
431
|
+
if (!email) {
|
|
432
|
+
throw new Error('Invalid email');
|
|
433
|
+
}
|
|
434
|
+
if (!email.includes('@')) {
|
|
435
|
+
throw new Error('Invalid email');
|
|
436
|
+
}
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
**Refactored**:
|
|
442
|
+
```javascript
|
|
443
|
+
// After (Better - Trigger Detection: 9, Examples: 8)
|
|
444
|
+
function validateEmail(email) {
|
|
445
|
+
if (!email) {
|
|
446
|
+
throw new Error('Email is required. Please provide an email address.');
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
450
|
+
if (!emailRegex.test(email)) {
|
|
451
|
+
throw new Error(
|
|
452
|
+
`Invalid email format: "${email}". ` +
|
|
453
|
+
'Email must contain "@" and a valid domain (e.g., user@example.com).'
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return true;
|
|
458
|
+
}
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
**Impact**: Medium (better debugging, user experience)
|
|
462
|
+
**Effort**: Low (10-15 minutes)
|
|
463
|
+
**Quality Gain**: +5 Trigger Detection, +5 Examples
|
|
464
|
+
**ROI**: ⭐⭐⭐⭐
|
|
465
|
+
|
|
466
|
+
---
|
|
467
|
+
|
|
468
|
+
## Pattern 10: Add Documentation and Examples
|
|
469
|
+
|
|
470
|
+
**When**: Code lacks usage examples or API documentation
|
|
471
|
+
|
|
472
|
+
**Example**:
|
|
473
|
+
```javascript
|
|
474
|
+
// Before (Poor - Examples: 1, Maintainability: 4)
|
|
475
|
+
function formatCurrency(amount, locale, currency) {
|
|
476
|
+
return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(amount);
|
|
477
|
+
}
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
**Refactored**:
|
|
481
|
+
```javascript
|
|
482
|
+
// After (Better - Examples: 9, Maintainability: 8)
|
|
483
|
+
/**
|
|
484
|
+
* Formats a number as currency according to locale and currency code.
|
|
485
|
+
*
|
|
486
|
+
* @param {number} amount - The numeric amount to format
|
|
487
|
+
* @param {string} locale - BCP 47 language tag (e.g., 'en-US', 'de-DE', 'ja-JP')
|
|
488
|
+
* @param {string} currency - ISO 4217 currency code (e.g., 'USD', 'EUR', 'JPY')
|
|
489
|
+
* @returns {string} Formatted currency string
|
|
490
|
+
*
|
|
491
|
+
* @example
|
|
492
|
+
* formatCurrency(1234.56, 'en-US', 'USD')
|
|
493
|
+
* // Returns: "$1,234.56"
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* formatCurrency(1234.56, 'de-DE', 'EUR')
|
|
497
|
+
* // Returns: "1.234,56 €"
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* formatCurrency(1234, 'ja-JP', 'JPY')
|
|
501
|
+
* // Returns: "¥1,234"
|
|
502
|
+
*/
|
|
503
|
+
function formatCurrency(amount, locale, currency) {
|
|
504
|
+
if (typeof amount !== 'number' || !Number.isFinite(amount)) {
|
|
505
|
+
throw new TypeError('Amount must be a finite number');
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return new Intl.NumberFormat(locale, {
|
|
509
|
+
style: 'currency',
|
|
510
|
+
currency
|
|
511
|
+
}).format(amount);
|
|
512
|
+
}
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
**Impact**: Medium (discoverability, onboarding)
|
|
516
|
+
**Effort**: Low-Medium (15-30 minutes)
|
|
517
|
+
**Quality Gain**: +8 Examples, +4 Maintainability
|
|
518
|
+
**ROI**: ⭐⭐⭐⭐
|
|
519
|
+
|
|
520
|
+
---
|
|
521
|
+
|
|
522
|
+
## Refactoring Decision Tree
|
|
523
|
+
|
|
524
|
+
```
|
|
525
|
+
Is the issue a security vulnerability?
|
|
526
|
+
├─ Yes → FIX IMMEDIATELY (Critical priority)
|
|
527
|
+
└─ No
|
|
528
|
+
├─ Is it causing production bugs?
|
|
529
|
+
│ ├─ Yes → Fix urgently (High priority)
|
|
530
|
+
│ └─ No
|
|
531
|
+
│ ├─ Can it be fixed in < 30 minutes?
|
|
532
|
+
│ │ ├─ Yes → Do it now (Quick win)
|
|
533
|
+
│ │ └─ No
|
|
534
|
+
│ │ ├─ Will it improve 2+ quality dimensions by 3+ points?
|
|
535
|
+
│ │ │ ├─ Yes → Schedule for next sprint
|
|
536
|
+
│ │ │ └─ No → Add to backlog (Low priority)
|
|
537
|
+
│ └─ Continue to next issue
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
---
|
|
541
|
+
|
|
542
|
+
## Summary: Highest ROI Refactorings
|
|
543
|
+
|
|
544
|
+
1. **Extract Magic Numbers** - 5 min, +12 points
|
|
545
|
+
2. **Add Input Validation** - 10 min, +13 points
|
|
546
|
+
3. **Consolidate Duplicate Code** - 20 min, +12 points
|
|
547
|
+
4. **Replace Callbacks with Async/Await** - 45 min, +19 points
|
|
548
|
+
5. **Extract Method** - 25 min, +16 points
|
|
549
|
+
|
|
550
|
+
**Golden Rule**: If refactoring takes longer than 1 hour, break it into smaller steps or reconsider if it's worth it.
|