/code-practices:naive-then-optimizefrom code-practices
Implement the obvious correct solution first, then optimize while preserving correctness. Use for algorithms, data transformations, and critical code paths.
Ships in code-practices. Installing the plugin adds it to Claude Code.
Use when implementing algorithms, data transformations, or any code where correctness is critical.
Karpathy: "Write the naive algorithm that is very likely correct first, then ask it to optimize it while preserving correctness."
Don't think about performance. Think about correctness. Write the dumbest, most straightforward code that works.
// Naive: O(n²) but obviously correct
function findDuplicates(arr: number[]): number[] {
const duplicates: number[] = [];
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j] && !duplicates.includes(arr[i])) {
duplicates.push(arr[i]);
}
}
}
return duplicates;
}
describe('findDuplicates', () => {
it('finds duplicates', () => {
expect(findDuplicates([1, 2, 2, 3])).toEqual([2]);
});
it('handles multiple duplicates', () => {
expect(findDuplicates([1, 1, 2, 2, 3])).toEqual([1, 2]);
});
it('handles no duplicates', () => {
expect(findDuplicates([1, 2, 3])).toEqual([]);
});
it('handles empty array', () => {
expect(findDuplicates([])).toEqual([]);
});
});
Run the tests. They should all pass. If they don't, fix the naive implementation first.
Is the naive version fast enough? Profile it with realistic data.
console.time('naive');
findDuplicates(largeArray);
console.timeEnd('naive');
If it's fast enough, stop here. Don't optimize code that doesn't need it.
Now optimize, but keep the same tests passing:
// Optimized: O(n) using a Set
function findDuplicates(arr: number[]): number[] {
const seen = new Set<number>();
const duplicates = new Set<number>();
for (const num of arr) {
if (seen.has(num)) {
duplicates.add(num);
}
seen.add(num);
}
return Array.from(duplicates);
}
For critical code, test that naive and optimized produce same results:
it('optimized matches naive for random inputs', () => {
for (let i = 0; i < 1000; i++) {
const input = generateRandomArray();
const naiveResult = findDuplicatesNaive(input);
const optimizedResult = findDuplicates(input);
expect(optimizedResult.sort()).toEqual(naiveResult.sort());
}
});
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