/code-practices:test-firstfrom code-practices
Write failing tests before implementation. Enables LLM self-correction through feedback loops. Use for new features and bug fixes.
Ships in code-practices. Installing the plugin adds it to Claude Code.
Use when implementing new features or fixing bugs. Especially valuable for complex logic.
Karpathy insight: "Get it to write tests first and then pass them." Tests create a feedback loop that lets LLMs self-correct. Tests are success criteria that enable autonomous iteration.
Before writing any code:
describe('calculateDiscount', () => {
it('returns 0 for orders under $50', () => {
expect(calculateDiscount(49)).toBe(0);
});
it('returns 10% for orders $50-$100', () => {
expect(calculateDiscount(50)).toBe(5);
expect(calculateDiscount(100)).toBe(10);
});
it('returns 20% for orders over $100', () => {
expect(calculateDiscount(101)).toBe(20.2);
});
it('handles zero', () => {
expect(calculateDiscount(0)).toBe(0);
});
it('throws on negative values', () => {
expect(() => calculateDiscount(-1)).toThrow();
});
});
Verify tests fail for the right reasons. This confirms tests are actually testing something.
Don't over-engineer. Write the simplest code that makes tests green.
Now that tests pass, clean up the code while keeping tests green.
Write test → Run (should fail) → Implement → Run (should pass) → Refactor → Run (should still pass)
From Anthropic's Claude Code best practices: Use independent subagents to verify implementations aren't overfitting to tests.
After implementation passes tests, spawn a separate verification:
Use a subagent to review the implementation of calculateDiscount.
Verify it handles the actual business requirements, not just the test cases.
Check for:
- Edge cases not covered by tests
- Hardcoded values that only work for test inputs
- Logic that's correct by accident
LLMs can "overfit" to test cases - writing code that passes the specific tests but fails on real-world inputs. An independent reviewer catches:
// Tests use ids 1, 2, 3
// BAD: Overfitting to test data
function getUser(id: number) {
if (id === 1) return { name: "Alice" };
if (id === 2) return { name: "Bob" };
return null;
}
// GOOD: Actual implementation
function getUser(id: number) {
return database.users.find(u => u.id === id);
}
The subagent review catches these patterns before they reach production.
What the command file sets; muted values are Claude Code's defaults
7-day visitors to each command page
npx claudepluginhub rahulsub/code-practices-plugin --plugin code-practices13 commands in this plugin
/cleanupSystematic code cleanup after completing features. Use after refactoring or periodically to remove dead code, console statements, TODOs, and tech debt./code-reviewReview code like a skeptical senior engineer. Use when reviewing PRs, after generating significant code, or before merging changes./context-managementManage Claude Code sessions effectively. Use during long sessions, multi-step tasks, or when responses start degrading. Covers /clear, checklists, and subagents.Other plugins' commands for the same job