@brainst0rm/core 0.13.0 → 0.14.1
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/dist/chunk-M7BBX56R.js +340 -0
- package/dist/chunk-M7BBX56R.js.map +1 -0
- package/dist/{chunk-SWXTFHC7.js → chunk-Z5D2QZY6.js} +3 -3
- package/dist/chunk-Z5D2QZY6.js.map +1 -0
- package/dist/chunk-Z6ZWNWWR.js +34 -0
- package/dist/index.d.ts +2717 -188
- package/dist/index.js +16178 -7949
- package/dist/index.js.map +1 -1
- package/dist/self-extend-47LWSK3E.js +52 -0
- package/dist/self-extend-47LWSK3E.js.map +1 -0
- package/dist/skills/builtin/api-and-interface-design/SKILL.md +300 -0
- package/dist/skills/builtin/browser-testing-with-devtools/SKILL.md +307 -0
- package/dist/skills/builtin/ci-cd-and-automation/SKILL.md +391 -0
- package/dist/skills/builtin/code-review-and-quality/SKILL.md +353 -0
- package/dist/skills/builtin/code-simplification/SKILL.md +340 -0
- package/dist/skills/builtin/context-engineering/SKILL.md +301 -0
- package/dist/skills/builtin/daemon-operations/SKILL.md +55 -0
- package/dist/skills/builtin/debugging-and-error-recovery/SKILL.md +306 -0
- package/dist/skills/builtin/deprecation-and-migration/SKILL.md +207 -0
- package/dist/skills/builtin/documentation-and-adrs/SKILL.md +295 -0
- package/dist/skills/builtin/frontend-ui-engineering/SKILL.md +333 -0
- package/dist/skills/builtin/git-workflow-and-versioning/SKILL.md +303 -0
- package/dist/skills/builtin/github-collaboration/SKILL.md +215 -0
- package/dist/skills/builtin/godmode-operations/SKILL.md +68 -0
- package/dist/skills/builtin/idea-refine/SKILL.md +186 -0
- package/dist/skills/builtin/idea-refine/examples.md +244 -0
- package/dist/skills/builtin/idea-refine/frameworks.md +101 -0
- package/dist/skills/builtin/idea-refine/refinement-criteria.md +126 -0
- package/dist/skills/builtin/idea-refine/scripts/idea-refine.sh +15 -0
- package/dist/skills/builtin/incremental-implementation/SKILL.md +243 -0
- package/dist/skills/builtin/memory-init/SKILL.md +54 -0
- package/dist/skills/builtin/memory-reflection/SKILL.md +59 -0
- package/dist/skills/builtin/multi-model-routing/SKILL.md +56 -0
- package/dist/skills/builtin/performance-optimization/SKILL.md +291 -0
- package/dist/skills/builtin/planning-and-task-breakdown/SKILL.md +240 -0
- package/dist/skills/builtin/security-and-hardening/SKILL.md +368 -0
- package/dist/skills/builtin/shipping-and-launch/SKILL.md +310 -0
- package/dist/skills/builtin/spec-driven-development/SKILL.md +212 -0
- package/dist/skills/builtin/test-driven-development/SKILL.md +376 -0
- package/dist/skills/builtin/using-agent-skills/SKILL.md +173 -0
- package/dist/trajectory-analyzer-ZAI2XUAI.js +14 -0
- package/dist/{trajectory-capture-RF7TUN6I.js → trajectory-capture-ERPIVYQJ.js} +3 -3
- package/package.json +14 -11
- package/dist/chunk-OU3NPQBH.js +0 -87
- package/dist/chunk-OU3NPQBH.js.map +0 -1
- package/dist/chunk-PZ5AY32C.js +0 -10
- package/dist/chunk-SWXTFHC7.js.map +0 -1
- package/dist/trajectory-MOCIJBV6.js +0 -8
- /package/dist/{chunk-PZ5AY32C.js.map → chunk-Z6ZWNWWR.js.map} +0 -0
- /package/dist/{trajectory-MOCIJBV6.js.map → trajectory-analyzer-ZAI2XUAI.js.map} +0 -0
- /package/dist/{trajectory-capture-RF7TUN6I.js.map → trajectory-capture-ERPIVYQJ.js.map} +0 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: code-review-and-quality
|
|
3
|
+
description: Conducts multi-axis code review. Use before merging any change. Use when reviewing code written by yourself, another agent, or a human. Use when you need to assess code quality across multiple dimensions before it enters the main branch.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Code Review and Quality
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
Multi-dimensional code review with quality gates. Every change gets reviewed before merge — no exceptions. Review covers five axes: correctness, readability, architecture, security, and performance.
|
|
11
|
+
|
|
12
|
+
**The approval standard:** Approve a change when it definitely improves overall code health, even if it isn't perfect. Perfect code doesn't exist — the goal is continuous improvement. Don't block a change because it isn't exactly how you would have written it. If it improves the codebase and follows the project's conventions, approve it.
|
|
13
|
+
|
|
14
|
+
## When to Use
|
|
15
|
+
|
|
16
|
+
- Before merging any PR or change
|
|
17
|
+
- After completing a feature implementation
|
|
18
|
+
- When another agent or model produced code you need to evaluate
|
|
19
|
+
- When refactoring existing code
|
|
20
|
+
- After any bug fix (review both the fix and the regression test)
|
|
21
|
+
|
|
22
|
+
## The Five-Axis Review
|
|
23
|
+
|
|
24
|
+
Every review evaluates code across these dimensions:
|
|
25
|
+
|
|
26
|
+
### 1. Correctness
|
|
27
|
+
|
|
28
|
+
Does the code do what it claims to do?
|
|
29
|
+
|
|
30
|
+
- Does it match the spec or task requirements?
|
|
31
|
+
- Are edge cases handled (null, empty, boundary values)?
|
|
32
|
+
- Are error paths handled (not just the happy path)?
|
|
33
|
+
- Does it pass all tests? Are the tests actually testing the right things?
|
|
34
|
+
- Are there off-by-one errors, race conditions, or state inconsistencies?
|
|
35
|
+
|
|
36
|
+
### 2. Readability & Simplicity
|
|
37
|
+
|
|
38
|
+
Can another engineer (or agent) understand this code without the author explaining it?
|
|
39
|
+
|
|
40
|
+
- Are names descriptive and consistent with project conventions? (No `temp`, `data`, `result` without context)
|
|
41
|
+
- Is the control flow straightforward (avoid nested ternaries, deep callbacks)?
|
|
42
|
+
- Is the code organized logically (related code grouped, clear module boundaries)?
|
|
43
|
+
- Are there any "clever" tricks that should be simplified?
|
|
44
|
+
- **Could this be done in fewer lines?** (1000 lines where 100 suffice is a failure)
|
|
45
|
+
- **Are abstractions earning their complexity?** (Don't generalize until the third use case)
|
|
46
|
+
- Would comments help clarify non-obvious intent? (But don't comment obvious code.)
|
|
47
|
+
- Are there dead code artifacts: no-op variables (`_unused`), backwards-compat shims, or `// removed` comments?
|
|
48
|
+
|
|
49
|
+
### 3. Architecture
|
|
50
|
+
|
|
51
|
+
Does the change fit the system's design?
|
|
52
|
+
|
|
53
|
+
- Does it follow existing patterns or introduce a new one? If new, is it justified?
|
|
54
|
+
- Does it maintain clean module boundaries?
|
|
55
|
+
- Is there code duplication that should be shared?
|
|
56
|
+
- Are dependencies flowing in the right direction (no circular dependencies)?
|
|
57
|
+
- Is the abstraction level appropriate (not over-engineered, not too coupled)?
|
|
58
|
+
|
|
59
|
+
### 4. Security
|
|
60
|
+
|
|
61
|
+
For detailed security guidance, see `security-and-hardening`. Does the change introduce vulnerabilities?
|
|
62
|
+
|
|
63
|
+
- Is user input validated and sanitized?
|
|
64
|
+
- Are secrets kept out of code, logs, and version control?
|
|
65
|
+
- Is authentication/authorization checked where needed?
|
|
66
|
+
- Are SQL queries parameterized (no string concatenation)?
|
|
67
|
+
- Are outputs encoded to prevent XSS?
|
|
68
|
+
- Are dependencies from trusted sources with no known vulnerabilities?
|
|
69
|
+
- Is data from external sources (APIs, logs, user content, config files) treated as untrusted?
|
|
70
|
+
- Are external data flows validated at system boundaries before use in logic or rendering?
|
|
71
|
+
|
|
72
|
+
### 5. Performance
|
|
73
|
+
|
|
74
|
+
For detailed profiling and optimization, see `performance-optimization`. Does the change introduce performance problems?
|
|
75
|
+
|
|
76
|
+
- Any N+1 query patterns?
|
|
77
|
+
- Any unbounded loops or unconstrained data fetching?
|
|
78
|
+
- Any synchronous operations that should be async?
|
|
79
|
+
- Any unnecessary re-renders in UI components?
|
|
80
|
+
- Any missing pagination on list endpoints?
|
|
81
|
+
- Any large objects created in hot paths?
|
|
82
|
+
|
|
83
|
+
## Change Sizing
|
|
84
|
+
|
|
85
|
+
Small, focused changes are easier to review, faster to merge, and safer to deploy. Target these sizes:
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
~100 lines changed → Good. Reviewable in one sitting.
|
|
89
|
+
~300 lines changed → Acceptable if it's a single logical change.
|
|
90
|
+
~1000 lines changed → Too large. Split it.
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**What counts as "one change":** A single self-contained modification that addresses one thing, includes related tests, and keeps the system functional after submission. One part of a feature — not the whole feature.
|
|
94
|
+
|
|
95
|
+
**Splitting strategies when a change is too large:**
|
|
96
|
+
|
|
97
|
+
| Strategy | How | When |
|
|
98
|
+
| ----------------- | ------------------------------------------------------- | ----------------------- |
|
|
99
|
+
| **Stack** | Submit a small change, start the next one based on it | Sequential dependencies |
|
|
100
|
+
| **By file group** | Separate changes for groups needing different reviewers | Cross-cutting concerns |
|
|
101
|
+
| **Horizontal** | Create shared code/stubs first, then consumers | Layered architecture |
|
|
102
|
+
| **Vertical** | Break into smaller full-stack slices of the feature | Feature work |
|
|
103
|
+
|
|
104
|
+
**When large changes are acceptable:** Complete file deletions and automated refactoring where the reviewer only needs to verify intent, not every line.
|
|
105
|
+
|
|
106
|
+
**Separate refactoring from feature work.** A change that refactors existing code and adds new behavior is two changes — submit them separately. Small cleanups (variable renaming) can be included at reviewer discretion.
|
|
107
|
+
|
|
108
|
+
## Change Descriptions
|
|
109
|
+
|
|
110
|
+
Every change needs a description that stands alone in version control history.
|
|
111
|
+
|
|
112
|
+
**First line:** Short, imperative, standalone. "Delete the FizzBuzz RPC" not "Deleting the FizzBuzz RPC." Must be informative enough that someone searching history can understand the change without reading the diff.
|
|
113
|
+
|
|
114
|
+
**Body:** What is changing and why. Include context, decisions, and reasoning not visible in the code itself. Link to bug numbers, benchmark results, or design docs where relevant. Acknowledge approach shortcomings when they exist.
|
|
115
|
+
|
|
116
|
+
**Anti-patterns:** "Fix bug," "Fix build," "Add patch," "Moving code from A to B," "Phase 1," "Add convenience functions."
|
|
117
|
+
|
|
118
|
+
## Review Process
|
|
119
|
+
|
|
120
|
+
### Step 1: Understand the Context
|
|
121
|
+
|
|
122
|
+
Before looking at code, understand the intent:
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
- What is this change trying to accomplish?
|
|
126
|
+
- What spec or task does it implement?
|
|
127
|
+
- What is the expected behavior change?
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Step 2: Review the Tests First
|
|
131
|
+
|
|
132
|
+
Tests reveal intent and coverage:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
- Do tests exist for the change?
|
|
136
|
+
- Do they test behavior (not implementation details)?
|
|
137
|
+
- Are edge cases covered?
|
|
138
|
+
- Do tests have descriptive names?
|
|
139
|
+
- Would the tests catch a regression if the code changed?
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Step 3: Review the Implementation
|
|
143
|
+
|
|
144
|
+
Walk through the code with the five axes in mind:
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
For each file changed:
|
|
148
|
+
1. Correctness: Does this code do what the test says it should?
|
|
149
|
+
2. Readability: Can I understand this without help?
|
|
150
|
+
3. Architecture: Does this fit the system?
|
|
151
|
+
4. Security: Any vulnerabilities?
|
|
152
|
+
5. Performance: Any bottlenecks?
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Step 4: Categorize Findings
|
|
156
|
+
|
|
157
|
+
Label every comment with its severity so the author knows what's required vs optional:
|
|
158
|
+
|
|
159
|
+
| Prefix | Meaning | Author Action |
|
|
160
|
+
| ----------------------------- | ------------------ | ------------------------------------------------------- |
|
|
161
|
+
| _(no prefix)_ | Required change | Must address before merge |
|
|
162
|
+
| **Critical:** | Blocks merge | Security vulnerability, data loss, broken functionality |
|
|
163
|
+
| **Nit:** | Minor, optional | Author may ignore — formatting, style preferences |
|
|
164
|
+
| **Optional:** / **Consider:** | Suggestion | Worth considering but not required |
|
|
165
|
+
| **FYI** | Informational only | No action needed — context for future reference |
|
|
166
|
+
|
|
167
|
+
This prevents authors from treating all feedback as mandatory and wasting time on optional suggestions.
|
|
168
|
+
|
|
169
|
+
### Step 5: Verify the Verification
|
|
170
|
+
|
|
171
|
+
Check the author's verification story:
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
- What tests were run?
|
|
175
|
+
- Did the build pass?
|
|
176
|
+
- Was the change tested manually?
|
|
177
|
+
- Are there screenshots for UI changes?
|
|
178
|
+
- Is there a before/after comparison?
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Multi-Model Review Pattern
|
|
182
|
+
|
|
183
|
+
Use different models for different review perspectives:
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
Model A writes the code
|
|
187
|
+
│
|
|
188
|
+
▼
|
|
189
|
+
Model B reviews for correctness and architecture
|
|
190
|
+
│
|
|
191
|
+
▼
|
|
192
|
+
Model A addresses the feedback
|
|
193
|
+
│
|
|
194
|
+
▼
|
|
195
|
+
Human makes the final call
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
This catches issues that a single model might miss — different models have different blind spots.
|
|
199
|
+
|
|
200
|
+
**Example prompt for a review agent:**
|
|
201
|
+
|
|
202
|
+
```
|
|
203
|
+
Review this code change for correctness, security, and adherence to
|
|
204
|
+
our project conventions. The spec says [X]. The change should [Y].
|
|
205
|
+
Flag any issues as Critical, Important, or Suggestion.
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Dead Code Hygiene
|
|
209
|
+
|
|
210
|
+
After any refactoring or implementation change, check for orphaned code:
|
|
211
|
+
|
|
212
|
+
1. Identify code that is now unreachable or unused
|
|
213
|
+
2. List it explicitly
|
|
214
|
+
3. **Ask before deleting:** "Should I remove these now-unused elements: [list]?"
|
|
215
|
+
|
|
216
|
+
Don't leave dead code lying around — it confuses future readers and agents. But don't silently delete things you're not sure about. When in doubt, ask.
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
DEAD CODE IDENTIFIED:
|
|
220
|
+
- formatLegacyDate() in src/utils/date.ts — replaced by formatDate()
|
|
221
|
+
- OldTaskCard component in src/components/ — replaced by TaskCard
|
|
222
|
+
- LEGACY_API_URL constant in src/config.ts — no remaining references
|
|
223
|
+
→ Safe to remove these?
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Review Speed
|
|
227
|
+
|
|
228
|
+
Slow reviews block entire teams. The cost of context-switching to review is less than the waiting cost imposed on others.
|
|
229
|
+
|
|
230
|
+
- **Respond within one business day** — this is the maximum, not the target
|
|
231
|
+
- **Ideal cadence:** Respond shortly after a review request arrives, unless deep in focused coding. A typical change should complete multiple review rounds in a single day
|
|
232
|
+
- **Prioritize fast individual responses** over quick final approval. Quick feedback reduces frustration even if multiple rounds are needed
|
|
233
|
+
- **Large changes:** Ask the author to split them rather than reviewing one massive changeset
|
|
234
|
+
|
|
235
|
+
## Handling Disagreements
|
|
236
|
+
|
|
237
|
+
When resolving review disputes, apply this hierarchy:
|
|
238
|
+
|
|
239
|
+
1. **Technical facts and data** override opinions and preferences
|
|
240
|
+
2. **Style guides** are the absolute authority on style matters
|
|
241
|
+
3. **Software design** must be evaluated on engineering principles, not personal preference
|
|
242
|
+
4. **Codebase consistency** is acceptable if it doesn't degrade overall health
|
|
243
|
+
|
|
244
|
+
**Don't accept "I'll clean it up later."** Experience shows deferred cleanup rarely happens. Require cleanup before submission unless it's a genuine emergency. If surrounding issues can't be addressed in this change, require filing a bug with self-assignment.
|
|
245
|
+
|
|
246
|
+
## Honesty in Review
|
|
247
|
+
|
|
248
|
+
When reviewing code — whether written by you, another agent, or a human:
|
|
249
|
+
|
|
250
|
+
- **Don't rubber-stamp.** "LGTM" without evidence of review helps no one.
|
|
251
|
+
- **Don't soften real issues.** "This might be a minor concern" when it's a bug that will hit production is dishonest.
|
|
252
|
+
- **Quantify problems when possible.** "This N+1 query will add ~50ms per item in the list" is better than "this could be slow."
|
|
253
|
+
- **Push back on approaches with clear problems.** Sycophancy is a failure mode in reviews. If the implementation has issues, say so directly and propose alternatives.
|
|
254
|
+
- **Accept override gracefully.** If the author has full context and disagrees, defer to their judgment. Comment on code, not people — reframe personal critiques to focus on the code itself.
|
|
255
|
+
|
|
256
|
+
## Dependency Discipline
|
|
257
|
+
|
|
258
|
+
Part of code review is dependency review:
|
|
259
|
+
|
|
260
|
+
**Before adding any dependency:**
|
|
261
|
+
|
|
262
|
+
1. Does the existing stack solve this? (Often it does.)
|
|
263
|
+
2. How large is the dependency? (Check bundle impact.)
|
|
264
|
+
3. Is it actively maintained? (Check last commit, open issues.)
|
|
265
|
+
4. Does it have known vulnerabilities? (`npm audit`)
|
|
266
|
+
5. What's the license? (Must be compatible with the project.)
|
|
267
|
+
|
|
268
|
+
**Rule:** Prefer standard library and existing utilities over new dependencies. Every dependency is a liability.
|
|
269
|
+
|
|
270
|
+
## The Review Checklist
|
|
271
|
+
|
|
272
|
+
```markdown
|
|
273
|
+
## Review: [PR/Change title]
|
|
274
|
+
|
|
275
|
+
### Context
|
|
276
|
+
|
|
277
|
+
- [ ] I understand what this change does and why
|
|
278
|
+
|
|
279
|
+
### Correctness
|
|
280
|
+
|
|
281
|
+
- [ ] Change matches spec/task requirements
|
|
282
|
+
- [ ] Edge cases handled
|
|
283
|
+
- [ ] Error paths handled
|
|
284
|
+
- [ ] Tests cover the change adequately
|
|
285
|
+
|
|
286
|
+
### Readability
|
|
287
|
+
|
|
288
|
+
- [ ] Names are clear and consistent
|
|
289
|
+
- [ ] Logic is straightforward
|
|
290
|
+
- [ ] No unnecessary complexity
|
|
291
|
+
|
|
292
|
+
### Architecture
|
|
293
|
+
|
|
294
|
+
- [ ] Follows existing patterns
|
|
295
|
+
- [ ] No unnecessary coupling or dependencies
|
|
296
|
+
- [ ] Appropriate abstraction level
|
|
297
|
+
|
|
298
|
+
### Security
|
|
299
|
+
|
|
300
|
+
- [ ] No secrets in code
|
|
301
|
+
- [ ] Input validated at boundaries
|
|
302
|
+
- [ ] No injection vulnerabilities
|
|
303
|
+
- [ ] Auth checks in place
|
|
304
|
+
- [ ] External data sources treated as untrusted
|
|
305
|
+
|
|
306
|
+
### Performance
|
|
307
|
+
|
|
308
|
+
- [ ] No N+1 patterns
|
|
309
|
+
- [ ] No unbounded operations
|
|
310
|
+
- [ ] Pagination on list endpoints
|
|
311
|
+
|
|
312
|
+
### Verification
|
|
313
|
+
|
|
314
|
+
- [ ] Tests pass
|
|
315
|
+
- [ ] Build succeeds
|
|
316
|
+
- [ ] Manual verification done (if applicable)
|
|
317
|
+
|
|
318
|
+
### Verdict
|
|
319
|
+
|
|
320
|
+
- [ ] **Approve** — Ready to merge
|
|
321
|
+
- [ ] **Request changes** — Issues must be addressed
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
## Common Rationalizations
|
|
325
|
+
|
|
326
|
+
| Rationalization | Reality |
|
|
327
|
+
| ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- |
|
|
328
|
+
| "It works, that's good enough" | Working code that's unreadable, insecure, or architecturally wrong creates debt that compounds. |
|
|
329
|
+
| "I wrote it, so I know it's correct" | Authors are blind to their own assumptions. Every change benefits from another set of eyes. |
|
|
330
|
+
| "We'll clean it up later" | Later never comes. The review is the quality gate — use it. Require cleanup before merge, not after. |
|
|
331
|
+
| "AI-generated code is probably fine" | AI code needs more scrutiny, not less. It's confident and plausible, even when wrong. |
|
|
332
|
+
| "The tests pass, so it's good" | Tests are necessary but not sufficient. They don't catch architecture problems, security issues, or readability concerns. |
|
|
333
|
+
|
|
334
|
+
## Red Flags
|
|
335
|
+
|
|
336
|
+
- PRs merged without any review
|
|
337
|
+
- Review that only checks if tests pass (ignoring other axes)
|
|
338
|
+
- "LGTM" without evidence of actual review
|
|
339
|
+
- Security-sensitive changes without security-focused review
|
|
340
|
+
- Large PRs that are "too big to review properly" (split them)
|
|
341
|
+
- No regression tests with bug fix PRs
|
|
342
|
+
- Review comments without severity labels — makes it unclear what's required vs optional
|
|
343
|
+
- Accepting "I'll fix it later" — it never happens
|
|
344
|
+
|
|
345
|
+
## Verification
|
|
346
|
+
|
|
347
|
+
After review is complete:
|
|
348
|
+
|
|
349
|
+
- [ ] All Critical issues are resolved
|
|
350
|
+
- [ ] All Important issues are resolved or explicitly deferred with justification
|
|
351
|
+
- [ ] Tests pass
|
|
352
|
+
- [ ] Build succeeds
|
|
353
|
+
- [ ] The verification story is documented (what changed, how it was verified)
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: code-simplification
|
|
3
|
+
description: Simplifies code for clarity. Use when refactoring code for clarity without changing behavior. Use when code works but is harder to read, maintain, or extend than it should be. Use when reviewing code that has accumulated unnecessary complexity.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Code Simplification
|
|
7
|
+
|
|
8
|
+
> Inspired by the [Claude Code Simplifier plugin](https://github.com/anthropics/claude-plugins-official/blob/main/plugins/code-simplifier/agents/code-simplifier.md). Adapted here as a model-agnostic, process-driven skill for any AI coding agent.
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
Simplify code by reducing complexity while preserving exact behavior. The goal is not fewer lines — it's code that is easier to read, understand, modify, and debug. Every simplification must pass a simple test: "Would a new team member understand this faster than the original?"
|
|
13
|
+
|
|
14
|
+
## When to Use
|
|
15
|
+
|
|
16
|
+
- After a feature is working and tests pass, but the implementation feels heavier than it needs to be
|
|
17
|
+
- During code review when readability or complexity issues are flagged
|
|
18
|
+
- When you encounter deeply nested logic, long functions, or unclear names
|
|
19
|
+
- When refactoring code written under time pressure
|
|
20
|
+
- When consolidating related logic scattered across files
|
|
21
|
+
- After merging changes that introduced duplication or inconsistency
|
|
22
|
+
|
|
23
|
+
**When NOT to use:**
|
|
24
|
+
|
|
25
|
+
- Code is already clean and readable — don't simplify for the sake of it
|
|
26
|
+
- You don't understand what the code does yet — comprehend before you simplify
|
|
27
|
+
- The code is performance-critical and the "simpler" version would be measurably slower
|
|
28
|
+
- You're about to rewrite the module entirely — simplifying throwaway code wastes effort
|
|
29
|
+
|
|
30
|
+
## The Five Principles
|
|
31
|
+
|
|
32
|
+
### 1. Preserve Behavior Exactly
|
|
33
|
+
|
|
34
|
+
Don't change what the code does — only how it expresses it. All inputs, outputs, side effects, error behavior, and edge cases must remain identical. If you're not sure a simplification preserves behavior, don't make it.
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
ASK BEFORE EVERY CHANGE:
|
|
38
|
+
→ Does this produce the same output for every input?
|
|
39
|
+
→ Does this maintain the same error behavior?
|
|
40
|
+
→ Does this preserve the same side effects and ordering?
|
|
41
|
+
→ Do all existing tests still pass without modification?
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. Follow Project Conventions
|
|
45
|
+
|
|
46
|
+
Simplification means making code more consistent with the codebase, not imposing external preferences. Before simplifying:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
1. Read CLAUDE.md / project conventions
|
|
50
|
+
2. Study how neighboring code handles similar patterns
|
|
51
|
+
3. Match the project's style for:
|
|
52
|
+
- Import ordering and module system
|
|
53
|
+
- Function declaration style
|
|
54
|
+
- Naming conventions
|
|
55
|
+
- Error handling patterns
|
|
56
|
+
- Type annotation depth
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Simplification that breaks project consistency is not simplification — it's churn.
|
|
60
|
+
|
|
61
|
+
### 3. Prefer Clarity Over Cleverness
|
|
62
|
+
|
|
63
|
+
Explicit code is better than compact code when the compact version requires a mental pause to parse.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
// UNCLEAR: Dense ternary chain
|
|
67
|
+
const label = isNew
|
|
68
|
+
? "New"
|
|
69
|
+
: isUpdated
|
|
70
|
+
? "Updated"
|
|
71
|
+
: isArchived
|
|
72
|
+
? "Archived"
|
|
73
|
+
: "Active";
|
|
74
|
+
|
|
75
|
+
// CLEAR: Readable mapping
|
|
76
|
+
function getStatusLabel(item: Item): string {
|
|
77
|
+
if (item.isNew) return "New";
|
|
78
|
+
if (item.isUpdated) return "Updated";
|
|
79
|
+
if (item.isArchived) return "Archived";
|
|
80
|
+
return "Active";
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// UNCLEAR: Chained reduces with inline logic
|
|
86
|
+
const result = items.reduce(
|
|
87
|
+
(acc, item) => ({
|
|
88
|
+
...acc,
|
|
89
|
+
[item.id]: { ...acc[item.id], count: (acc[item.id]?.count ?? 0) + 1 },
|
|
90
|
+
}),
|
|
91
|
+
{},
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// CLEAR: Named intermediate step
|
|
95
|
+
const countById = new Map<string, number>();
|
|
96
|
+
for (const item of items) {
|
|
97
|
+
countById.set(item.id, (countById.get(item.id) ?? 0) + 1);
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 4. Maintain Balance
|
|
102
|
+
|
|
103
|
+
Simplification has a failure mode: over-simplification. Watch for these traps:
|
|
104
|
+
|
|
105
|
+
- **Inlining too aggressively** — removing a helper that gave a concept a name makes the call site harder to read
|
|
106
|
+
- **Combining unrelated logic** — two simple functions merged into one complex function is not simpler
|
|
107
|
+
- **Removing "unnecessary" abstraction** — some abstractions exist for extensibility or testability, not complexity
|
|
108
|
+
- **Optimizing for line count** — fewer lines is not the goal; easier comprehension is
|
|
109
|
+
|
|
110
|
+
### 5. Scope to What Changed
|
|
111
|
+
|
|
112
|
+
Default to simplifying recently modified code. Avoid drive-by refactors of unrelated code unless explicitly asked to broaden scope. Unscoped simplification creates noise in diffs and risks unintended regressions.
|
|
113
|
+
|
|
114
|
+
## The Simplification Process
|
|
115
|
+
|
|
116
|
+
### Step 1: Understand Before Touching (Chesterton's Fence)
|
|
117
|
+
|
|
118
|
+
Before changing or removing anything, understand why it exists. This is Chesterton's Fence: if you see a fence across a road and don't understand why it's there, don't tear it down. First understand the reason, then decide if the reason still applies.
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
BEFORE SIMPLIFYING, ANSWER:
|
|
122
|
+
- What is this code's responsibility?
|
|
123
|
+
- What calls it? What does it call?
|
|
124
|
+
- What are the edge cases and error paths?
|
|
125
|
+
- Are there tests that define the expected behavior?
|
|
126
|
+
- Why might it have been written this way? (Performance? Platform constraint? Historical reason?)
|
|
127
|
+
- Check git blame: what was the original context for this code?
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
If you can't answer these, you're not ready to simplify. Read more context first.
|
|
131
|
+
|
|
132
|
+
### Step 2: Identify Simplification Opportunities
|
|
133
|
+
|
|
134
|
+
Scan for these patterns — each one is a concrete signal, not a vague smell:
|
|
135
|
+
|
|
136
|
+
**Structural complexity:**
|
|
137
|
+
|
|
138
|
+
| Pattern | Signal | Simplification |
|
|
139
|
+
| -------------------------- | ---------------------------------- | --------------------------------------------------------- |
|
|
140
|
+
| Deep nesting (3+ levels) | Hard to follow control flow | Extract conditions into guard clauses or helper functions |
|
|
141
|
+
| Long functions (50+ lines) | Multiple responsibilities | Split into focused functions with descriptive names |
|
|
142
|
+
| Nested ternaries | Requires mental stack to parse | Replace with if/else chains, switch, or lookup objects |
|
|
143
|
+
| Boolean parameter flags | `doThing(true, false, true)` | Replace with options objects or separate functions |
|
|
144
|
+
| Repeated conditionals | Same `if` check in multiple places | Extract to a well-named predicate function |
|
|
145
|
+
|
|
146
|
+
**Naming and readability:**
|
|
147
|
+
|
|
148
|
+
| Pattern | Signal | Simplification |
|
|
149
|
+
| -------------------------- | ---------------------------------------------- | ------------------------------------------------------------------------ |
|
|
150
|
+
| Generic names | `data`, `result`, `temp`, `val`, `item` | Rename to describe the content: `userProfile`, `validationErrors` |
|
|
151
|
+
| Abbreviated names | `usr`, `cfg`, `btn`, `evt` | Use full words unless the abbreviation is universal (`id`, `url`, `api`) |
|
|
152
|
+
| Misleading names | Function named `get` that also mutates state | Rename to reflect actual behavior |
|
|
153
|
+
| Comments explaining "what" | `// increment counter` above `count++` | Delete the comment — the code is clear enough |
|
|
154
|
+
| Comments explaining "why" | `// Retry because the API is flaky under load` | Keep these — they carry intent the code can't express |
|
|
155
|
+
|
|
156
|
+
**Redundancy:**
|
|
157
|
+
|
|
158
|
+
| Pattern | Signal | Simplification |
|
|
159
|
+
| ------------------------- | ------------------------------------------------------------ | --------------------------------------------------------- |
|
|
160
|
+
| Duplicated logic | Same 5+ lines in multiple places | Extract to a shared function |
|
|
161
|
+
| Dead code | Unreachable branches, unused variables, commented-out blocks | Remove (after confirming it's truly dead) |
|
|
162
|
+
| Unnecessary abstractions | Wrapper that adds no value | Inline the wrapper, call the underlying function directly |
|
|
163
|
+
| Over-engineered patterns | Factory-for-a-factory, strategy-with-one-strategy | Replace with the simple direct approach |
|
|
164
|
+
| Redundant type assertions | Casting to a type that's already inferred | Remove the assertion |
|
|
165
|
+
|
|
166
|
+
### Step 3: Apply Changes Incrementally
|
|
167
|
+
|
|
168
|
+
Make one simplification at a time. Run tests after each change. **Submit refactoring changes separately from feature or bug fix changes.** A PR that refactors and adds a feature is two PRs — split them.
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
FOR EACH SIMPLIFICATION:
|
|
172
|
+
1. Make the change
|
|
173
|
+
2. Run the test suite
|
|
174
|
+
3. If tests pass → commit (or continue to next simplification)
|
|
175
|
+
4. If tests fail → revert and reconsider
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Avoid batching multiple simplifications into a single untested change. If something breaks, you need to know which simplification caused it.
|
|
179
|
+
|
|
180
|
+
**The Rule of 500:** If a refactoring would touch more than 500 lines, invest in automation (codemods, sed scripts, AST transforms) rather than making the changes by hand. Manual edits at that scale are error-prone and exhausting to review.
|
|
181
|
+
|
|
182
|
+
### Step 4: Verify the Result
|
|
183
|
+
|
|
184
|
+
After all simplifications, step back and evaluate the whole:
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
COMPARE BEFORE AND AFTER:
|
|
188
|
+
- Is the simplified version genuinely easier to understand?
|
|
189
|
+
- Did you introduce any new patterns inconsistent with the codebase?
|
|
190
|
+
- Is the diff clean and reviewable?
|
|
191
|
+
- Would a teammate approve this change?
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
If the "simplified" version is harder to understand or review, revert. Not every simplification attempt succeeds.
|
|
195
|
+
|
|
196
|
+
## Language-Specific Guidance
|
|
197
|
+
|
|
198
|
+
### TypeScript / JavaScript
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
// SIMPLIFY: Unnecessary async wrapper
|
|
202
|
+
// Before
|
|
203
|
+
async function getUser(id: string): Promise<User> {
|
|
204
|
+
return await userService.findById(id);
|
|
205
|
+
}
|
|
206
|
+
// After
|
|
207
|
+
function getUser(id: string): Promise<User> {
|
|
208
|
+
return userService.findById(id);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// SIMPLIFY: Verbose conditional assignment
|
|
212
|
+
// Before
|
|
213
|
+
let displayName: string;
|
|
214
|
+
if (user.nickname) {
|
|
215
|
+
displayName = user.nickname;
|
|
216
|
+
} else {
|
|
217
|
+
displayName = user.fullName;
|
|
218
|
+
}
|
|
219
|
+
// After
|
|
220
|
+
const displayName = user.nickname || user.fullName;
|
|
221
|
+
|
|
222
|
+
// SIMPLIFY: Manual array building
|
|
223
|
+
// Before
|
|
224
|
+
const activeUsers: User[] = [];
|
|
225
|
+
for (const user of users) {
|
|
226
|
+
if (user.isActive) {
|
|
227
|
+
activeUsers.push(user);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// After
|
|
231
|
+
const activeUsers = users.filter((user) => user.isActive);
|
|
232
|
+
|
|
233
|
+
// SIMPLIFY: Redundant boolean return
|
|
234
|
+
// Before
|
|
235
|
+
function isValid(input: string): boolean {
|
|
236
|
+
if (input.length > 0 && input.length < 100) {
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
// After
|
|
242
|
+
function isValid(input: string): boolean {
|
|
243
|
+
return input.length > 0 && input.length < 100;
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Python
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
# SIMPLIFY: Verbose dictionary building
|
|
251
|
+
# Before
|
|
252
|
+
result = {}
|
|
253
|
+
for item in items:
|
|
254
|
+
result[item.id] = item.name
|
|
255
|
+
# After
|
|
256
|
+
result = {item.id: item.name for item in items}
|
|
257
|
+
|
|
258
|
+
# SIMPLIFY: Nested conditionals with early return
|
|
259
|
+
# Before
|
|
260
|
+
def process(data):
|
|
261
|
+
if data is not None:
|
|
262
|
+
if data.is_valid():
|
|
263
|
+
if data.has_permission():
|
|
264
|
+
return do_work(data)
|
|
265
|
+
else:
|
|
266
|
+
raise PermissionError("No permission")
|
|
267
|
+
else:
|
|
268
|
+
raise ValueError("Invalid data")
|
|
269
|
+
else:
|
|
270
|
+
raise TypeError("Data is None")
|
|
271
|
+
# After
|
|
272
|
+
def process(data):
|
|
273
|
+
if data is None:
|
|
274
|
+
raise TypeError("Data is None")
|
|
275
|
+
if not data.is_valid():
|
|
276
|
+
raise ValueError("Invalid data")
|
|
277
|
+
if not data.has_permission():
|
|
278
|
+
raise PermissionError("No permission")
|
|
279
|
+
return do_work(data)
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### React / JSX
|
|
283
|
+
|
|
284
|
+
```tsx
|
|
285
|
+
// SIMPLIFY: Verbose conditional rendering
|
|
286
|
+
// Before
|
|
287
|
+
function UserBadge({ user }: Props) {
|
|
288
|
+
if (user.isAdmin) {
|
|
289
|
+
return <Badge variant="admin">Admin</Badge>;
|
|
290
|
+
} else {
|
|
291
|
+
return <Badge variant="default">User</Badge>;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// After
|
|
295
|
+
function UserBadge({ user }: Props) {
|
|
296
|
+
const variant = user.isAdmin ? "admin" : "default";
|
|
297
|
+
const label = user.isAdmin ? "Admin" : "User";
|
|
298
|
+
return <Badge variant={variant}>{label}</Badge>;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// SIMPLIFY: Prop drilling through intermediate components
|
|
302
|
+
// Before — consider whether context or composition solves this better.
|
|
303
|
+
// This is a judgment call — flag it, don't auto-refactor.
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Common Rationalizations
|
|
307
|
+
|
|
308
|
+
| Rationalization | Reality |
|
|
309
|
+
| ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
310
|
+
| "It's working, no need to touch it" | Working code that's hard to read will be hard to fix when it breaks. Simplifying now saves time on every future change. |
|
|
311
|
+
| "Fewer lines is always simpler" | A 1-line nested ternary is not simpler than a 5-line if/else. Simplicity is about comprehension speed, not line count. |
|
|
312
|
+
| "I'll just quickly simplify this unrelated code too" | Unscoped simplification creates noisy diffs and risks regressions in code you didn't intend to change. Stay focused. |
|
|
313
|
+
| "The types make it self-documenting" | Types document structure, not intent. A well-named function explains _why_ better than a type signature explains _what_. |
|
|
314
|
+
| "This abstraction might be useful later" | Don't preserve speculative abstractions. If it's not used now, it's complexity without value. Remove it and re-add when needed. |
|
|
315
|
+
| "The original author must have had a reason" | Maybe. Check git blame — apply Chesterton's Fence. But accumulated complexity often has no reason; it's just the residue of iteration under pressure. |
|
|
316
|
+
| "I'll refactor while adding this feature" | Separate refactoring from feature work. Mixed changes are harder to review, revert, and understand in history. |
|
|
317
|
+
|
|
318
|
+
## Red Flags
|
|
319
|
+
|
|
320
|
+
- Simplification that requires modifying tests to pass (you likely changed behavior)
|
|
321
|
+
- "Simplified" code that is longer and harder to follow than the original
|
|
322
|
+
- Renaming things to match your preferences rather than project conventions
|
|
323
|
+
- Removing error handling because "it makes the code cleaner"
|
|
324
|
+
- Simplifying code you don't fully understand
|
|
325
|
+
- Batching many simplifications into one large, hard-to-review commit
|
|
326
|
+
- Refactoring code outside the scope of the current task without being asked
|
|
327
|
+
|
|
328
|
+
## Verification
|
|
329
|
+
|
|
330
|
+
After completing a simplification pass:
|
|
331
|
+
|
|
332
|
+
- [ ] All existing tests pass without modification
|
|
333
|
+
- [ ] Build succeeds with no new warnings
|
|
334
|
+
- [ ] Linter/formatter passes (no style regressions)
|
|
335
|
+
- [ ] Each simplification is a reviewable, incremental change
|
|
336
|
+
- [ ] The diff is clean — no unrelated changes mixed in
|
|
337
|
+
- [ ] Simplified code follows project conventions (checked against CLAUDE.md or equivalent)
|
|
338
|
+
- [ ] No error handling was removed or weakened
|
|
339
|
+
- [ ] No dead code was left behind (unused imports, unreachable branches)
|
|
340
|
+
- [ ] A teammate or review agent would approve the change as a net improvement
|