From code-practices
Implement the obvious correct solution first, then optimize while preserving correctness. Use for algorithms, data transformations, and critical code paths.
npx claudepluginhub joshuarweaver/cascade-code-general-misc-2 --plugin rahulsub-code-practices-pluginThis skill uses the workspace's default tool permissions.
Use when implementing algorithms, data transformations, or any code where correctness is critical.
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Searches prompts.chat for AI prompt templates by keyword or category, retrieves by ID with variable handling, and improves prompts via AI. Use for discovering or enhancing prompts.
Checks Next.js compilation errors using a running Turbopack dev server after code edits. Fixes actionable issues before reporting complete. Replaces `next build`.
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());
}
});