Writing code with Claude

Features, tests, refactoring — with hands-on examples

The write-run-fix loop

Claude Code’s power is the loop: write code → run it → see what happens → fix. This is what pair programming with a human feels like, except Claude doesn’t get tired. Your job is to direct: describe what you want, review what it does, and steer when it goes off track.

Adding a feature

"In /src/api/products.js, add a function called getOutOfStockProducts() that queries Shopify for all products where inventory_quantity = 0.

It should:
- Use the existing shopifyClient from line 8
- Return an array of { id, title, sku } objects
- Handle pagination (Shopify returns max 250 per page)
- Log any API errors using the logger module

Don't modify the existing getProducts() function. Write a test for it in /tests/api/products.test.js"

Refactoring

"The sync.js file has gotten too long — it's doing too many things. Refactor it to separate concerns:
- Keep the orchestration logic in sync.js
- Move the transform functions to a new /transforms/catalog.js
- Move the error handling to /lib/errorHandler.js

Make sure all existing tests still pass after the refactor. Don't change any function signatures — just move them."

Debugging

"The catalog sync is failing intermittently. Run the sync in test mode and capture the error output. Then look at the error and trace it back through the code to find the root cause.

To run test mode: npm run sync:test"

Working iteratively

  • Small steps — ask for one function at a time, test it, then build on it
  • Review before moving on — read what Claude wrote before asking for the next piece
  • Run tests early and often — “Run the tests and fix anything failing before we continue”
  • Commit checkpoints — “Commit what we have so far with a descriptive message”

Stay in the loop: Claude Code can move fast and make many changes. Review what it’s doing, especially when it’s changing multiple files. A quick “what did you just change and why?” is always appropriate.