{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "language_info": {
   "name": "csharp"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# Debugging Workflow\n\nDebugging is a repeatable process, not guesswork.",
   "id": "35442d52"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## The Workflow\n\nUse this sequence every time:\n\n1. Reproduce reliably\n2. Minimize the failing case\n3. Observe state with tools\n4. Form a specific hypothesis\n5. Patch one thing\n6. Re-test and check for regressions",
   "id": "e0fbf7fc"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Step 1: Reproduce Reliably\n\nCapture:\n- exact input\n- exact command\n- expected behavior\n- actual behavior\n\nWithout reproducibility, debugging quality collapses.",
   "id": "45abc8b8"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Step 2: Minimize the Failing Case\n\nStart with a *buggy* example — an off-by-one error in a sum loop:",
   "id": "bec8219a"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "// Buggy: skips the last element\nint[] scores = { 85, 92, 78, 90, 88 };\nint total = 0;\n\nfor (int i = 0; i < scores.Length - 1; i++)  // BUG: should be <= or just <\n    total += scores[i];\n\nConsole.WriteLine($\"Total: {total}\");    // prints 345 instead of 433\nConsole.WriteLine($\"Count: {scores.Length}\");",
   "id": "9b215d33"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "Fix: change the loop condition to `i < scores.Length`:",
   "id": "fc94fdb6"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "int[] scores = { 85, 92, 78, 90, 88 };\nint total = 0;\n\nfor (int i = 0; i < scores.Length; i++)\n    total += scores[i];\n\nConsole.WriteLine($\"Total: {total}\");   // 433\nConsole.WriteLine($\"Average: {total / (double)scores.Length:F1}\");  // 86.6",
   "id": "7d7a62f4"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Step 3: Breakpoints and Stepping in VS Code\n\n- Set a breakpoint **before** the suspected line\n- **Step Over** (`F10`) — execute one line, staying in current method\n- **Step Into** (`F11`) — follow execution into a called method\n- **Step Out** (`Shift+F11`) — exit the current method\n\nSet the breakpoint on the `total += scores[i]` line and watch `total` after each iteration.",
   "id": "644d3973"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Step 4: Watch and Call Stack\n\nInspect:\n- key variables before and after transformations\n- the call stack to confirm control flow assumptions\n- branch conditions at decision points (`if`, loop bounds)\n\nAdd a **Watch** expression for `scores[i]` and `total` to see each step.",
   "id": "cf60d8ba"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Step 5: Typical Bug Patterns\n\n| Pattern | Example |\n|---------|--------|\n| Off-by-one | `i < Length - 1` instead of `i < Length` |\n| Stale loop state | reusing a variable without resetting it |\n| Null/empty assumption | skipping `IsNullOrWhiteSpace` check |\n| Parsing without validation | `int.Parse` on unvalidated input |",
   "id": "c85d226f"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Step 6: Validate the Fix\n\nAfter patching:\n1. Re-run the **original** failing input\n2. Run edge cases (empty array, single element, max values)\n3. Run unit tests if they exist",
   "id": "6399fc71"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "// Edge case: single element\nint[] single = { 42 };\nint t = 0;\nfor (int i = 0; i < single.Length; i++) t += single[i];\nConsole.WriteLine(t);  // 42\n\n// Edge case: empty array\nint[] empty = {};\nint t2 = 0;\nfor (int i = 0; i < empty.Length; i++) t2 += empty[i];\nConsole.WriteLine(t2);  // 0",
   "id": "91f02fe0"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Practice\n\nTake a method that parses a CSV line:\n\n1. Add one intentional bug (wrong field index, missing `Trim`)\n2. Set a breakpoint at the parse start\n3. Add a watch on `parts.Length` and each field\n4. Step through `TryParse` calls\n5. Identify the bug and fix it\n6. Re-run with both valid and malformed input",
   "id": "644e282d"
  },
  {
   "cell_type": "markdown",
   "id": "footnotes",
   "metadata": {},
   "source": "```{rubric} Footnotes\n```\n"
  }
 ]
}
