Lesson 4

Using AI for Code and Debugging

Use AI coding assistants for explanation, refactoring, tests, debugging, and review without giving up engineering judgment.

AI coding assistants are most useful when they work inside a bounded engineering loop: understand the problem, propose a small change, run or describe checks, and revise based on evidence. They should accelerate judgment, not replace it.

Good coding assistant tasks

Use AI for tasks where review is realistic:

  • Explain a function or error message.
  • Draft unit tests from expected behavior.
  • Suggest a small refactor.
  • Compare implementation options.
  • Generate documentation from existing code.
  • Find likely causes of a failing test.

Avoid handing over large, unclear tasks without requirements or validation.

Debugging prompt pattern

For debugging, provide:

  • What you expected.
  • What happened instead.
  • The smallest relevant code snippet.
  • The exact error message.
  • Inputs that reproduce the issue.
  • Any test or command you already ran.

Then ask for likely causes, the smallest fix, and a verification step.

The AI coding loop

Use this loop for day-to-day coding work:

  1. Explain the problem and ask for likely causes.
  2. Ask for the smallest safe change.
  3. Apply or inspect the change yourself.
  4. Run tests, type checks, linters, or a focused manual check.
  5. Feed the result back only if more iteration is needed.

This keeps the assistant inside a controlled loop instead of turning one prompt into a large unreviewed patch.

Refactoring with guardrails

When asking for refactors, say what must not change: public API, output format, database schema, accessibility behavior, or performance constraints. Without guardrails, AI may “improve” the code by changing the contract.

Review generated code like a patch

Do not judge generated code by how polished it looks. Review it like any other patch: correctness, edge cases, security, dependency changes, readability, and tests.

When to stop and inspect manually

Pause the AI workflow when the assistant repeats the same failed fix, introduces unrelated rewrites, changes public behavior without being asked, or cannot explain how to verify the result. At that point, inspect the code, reduce the problem, and restart with a smaller prompt.

Key takeaways

  • AI is strongest in small, reviewable coding loops.
  • Debugging prompts should include symptoms, reproduction steps, and expected behavior.
  • Generated code still needs tests and review.
  • Stop and narrow the task when AI starts expanding the change surface.

Next lesson

Next, learn a repeatable way to check AI answers before trusting them.

Back to course overview