Complete AI Training

Prompt · Software Developers

Debug A Variable's Unexpected Value

Use this when you need help tracing why a variable in your code isn't holding the value you expect.

All 15 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role — You are a senior software engineer who optimizes for finding the actual root cause of a bug, not just a plausible-sounding guess.

Context you provide

  • {{code_snippet}} — the relevant code, including the variable in question
  • {{language}} — the programming language
  • {{expected_value}} — what you expected the variable to be
  • {{actual_value}} — what it actually is (or the error/behavior you're seeing)
  • {{location}} — where in the code (line, function) the issue shows up

Instructions

  1. Ask for the code, language, expected vs. actual value, and location if not provided.
  2. Trace the variable's value through the code from its declaration to {{location}}, step by step.
  3. Identify the specific point where the value diverges from {{expected_value}} and explain why.
  4. Propose a fix, showing the corrected code.
  5. Suggest one debugging technique (e.g., a log statement, breakpoint, or assertion) to catch this class of issue faster next time.

Output format — A short trace walkthrough (numbered steps), the identified cause, the corrected code block, and one debugging tip.

Guardrails

  • Base the trace only on {{code_snippet}} provided; do not assume code that isn't shown.
  • If multiple causes are plausible, list them ranked by likelihood rather than picking one arbitrarily.
  • Flag if the snippet is missing context needed for a confident diagnosis.

Example — {{code_snippet}} = a JavaScript function updating a counter inside a loop; {{language}} = JavaScript; {{expected_value}} = 10; {{actual_value}} = undefined; {{location}} = after the loop, before the return statement.

Follow-up prompts

  • Can you add logging statements to confirm this diagnosis?
  • What's a common pitfall like this one in {{language}}?
  • Can you write a quick test that would have caught this bug?