Simplifying and Debugging Code
Markdown--- name: simplifying-and-debugging-code description: Analyzes existing or AI-generated code, simplifies it using standard/basic libraries where possible without breaking functionality, then runs iterative bug checks until the code is clean and stable. Use when given messy, over-engineered, or "AI slop" code that needs to be made readable, maintainable, and bug-free before shipping. ---
Given a block of code, immediately run this loop:
- Analyze: Read the code, identify its actual purpose and features.
- Simplify: Can this be done with fewer dependencies, simpler constructs, or standard/built-in libraries instead of heavy/custom/obscure ones?
- Verify: Does the simplified version still do everything the original did? No feature loss, no new bugs?
- Bug check: Trace through logic, edge cases, inputs/outputs. Fix anything broken.
- Repeat steps 2-4 until no further simplification is possible and no bugs remain.
No preamble needed — just do the pass and show before/after.
Progress:
- Step 1: Read and understand the full code — what it's supposed to do, its inputs/outputs, dependencies
- Step 2: Flag unnecessary complexity (custom implementations of things stdlib already does, excessive abstraction, unused imports, redundant logic, overly clever one-liners)
- Step 3: Propose simplification using basic/standard libraries — ask: "Can this be simplified using [specific basic library/approach]?"
- Step 4: Apply simplification, preserving all original features/behavior
- Step 5: Run a bug check pass on the new version (logic errors, edge cases, type mismatches, off-by-ones, broken imports)
- Step 6: Fix any bugs found
- Step 7: Repeat Steps 3-6 until: (a) no more meaningful simplification is possible, AND (b) no bugs remain
- Step 8: Present final clean version with a short summary of what changed and why
Treat this as a loop, not a single pass. Don't stop at the first clean-looking result — check again.
Example 1: Input:
Pythonimport functools, itertools class StringReverser: def __init__(self, s): self.s = s def reverse(self): return functools.reduce(lambda a, b: b + a, list(self.s), '') sr = StringReverser("hello") print(sr.reverse())
Output:
Python# Simplified: no need for a class, functools, or itertools for this def reverse(s): return s[::-1] print(reverse("hello"))
Bug check: confirmed output "olleh" matches original behavior. No edge cases broken (empty string, unicode both handled fine by slicing).
Example 2:
Input: A 200-line script using a custom-built retry decorator, a hand-rolled JSON parser, and nested threading for a simple API call with retries.
Output: Replaced custom retry logic with tenacity-free basic for loop + time.sleep (or standard lib only, no extra deps if possible), replaced hand-rolled JSON parsing with built-in json module, removed threading since the task was sequential. Final script: ~40 lines, same functionality, bug check confirmed retries, timeouts, and error handling all still work.
- Always ask (internally or explicitly): "Is there a basic/standard library that already does this?" before accepting custom implementations.
- Prefer built-in language features over third-party packages when they're sufficient.
- Never simplify at the cost of a feature — if unsure whether something is used elsewhere, trace it before removing.
- Bug-check after every simplification pass, not just once at the end — simplification can introduce new bugs.
- Keep a mental (or written) diff of what changed each loop iteration so regressions are traceable.
- Stop simplifying when further changes would hurt readability more than they help (don't golf the code).
- Final output should be understandable by someone with basic knowledge of the language — that's the bar.
- Don't remove code just because it looks redundant without confirming it's actually unused.
- Don't swap in a "simpler" library that actually adds a new dependency — simpler means fewer/more common dependencies, not just fewer lines.
- Don't declare "bug-free" after a single read-through — actually trace edge cases (empty inputs, nulls, large inputs, wrong types).
- Don't stop after one simplification pass if more obvious simplifications remain — keep looping.
- Don't sacrifice correctness for brevity — a shorter buggy version is worse than a longer correct one.
- Don't skip re-testing after fixing a bug — a fix can introduce a new bug.