ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Debugging ဆိုတာ စုံထောက်အလုပ်နဲ့ တူပါတယ်။ "Total က 35 ဖြစ်ရမှာ 10520 ဖြစ်နေတယ်" ဆိုတာ ရာဇဝတ်မှု ဖြစ်ပွားရာနေရာပါ။ စုံထောက်ကောင်းတစ်ယောက်က ပထမဆုံးတွေ့တဲ့လူကို ဖမ်းမယ့်အစား သဲလွန်စ (evidence) တွေ စုပြီး ဘာဖြစ်ခဲ့လဲ အဆင့်လိုက် ပြန်တည်ဆောက်ပါတယ်။
JavaScript မှာ သဲလွန်စ စုတဲ့ ကိရိယာတွေက console.log() နဲ့ console.table() လို print လုပ်တဲ့ method တွေ၊ code ကို လမ်းတစ်ဝက်မှာ ရပ်ပေးတဲ့ debugger statement နဲ့ browser DevTools ရဲ့ breakpoint တွေပါ။ ရပ်ထားတဲ့အချိန်မှာ variable တိုင်းရဲ့ value ကို တိုက်ရိုက် ကြည့်လို့ ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: symptom ကို ဖုံးလိုက်ရုံနဲ့ bug မပျောက်
Shopping cart total တစ်ခုက 35 ဖြစ်ရမှာ "10520" လို့ ပြနေတယ် ဆိုပါစို့။ အမြန်ပြင်ချင်တဲ့ developer တစ်ယောက်က ပြတဲ့ line မှာ parseInt() ထည့်လိုက်ပါတယ်၊ ရလဒ်က 10520 ပဲ ဆက်ဖြစ်နေပြီး ကိုယ်ဘာပြင်လိုက်လဲ နားမလည်တော့ပါဘူး။ နောက်တစ်ယောက်ကတော့ "Pen" item ကို hard-code စစ်ပြီး ကျော်လိုက်ပါတယ် — ဒီနေ့ အလုပ်ဖြစ်ပေမယ့် မနက်ဖြန် text price နဲ့ item အသစ် ရောက်လာတာနဲ့ ပြန်ပျက်ပါတယ်။
// Symptom: the page shows "Total: 10520" instead of 35
function total(items) {
let sum = 0;
for (const item of items) sum += item.price;
return sum;
}
// "Fix" 1: patch the display line (still 10520)
console.log("Total:", parseInt(total(cart)));
// "Fix" 2: skip the item that looked strange (breaks on the next one)
const safeCart = cart.filter((item) => item.name !== "Pen");ဘာကြောင့် ကံစမ်းပြင်တာ မအောင်မြင်တာလဲ
Symptom ပေါ်တဲ့နေရာနဲ့ bug ရှိတဲ့နေရာက မတူတတ်ပါဘူး။ ဒီမှာ ပြဿနာက display line မဟုတ်ဘဲ form ကနေ "5" ဆိုတဲ့ string ဝင်လာပြီး + operator က ပေါင်းမယ့်အစား စာသားဆက်လိုက်တာပါ။ Evidence မစုဘဲ ပြင်ရင် အမှားကို နေရာရွှေ့ရုံပဲ ဖြစ်ပါတယ်။ မှားတဲ့ value ကို နောက်ပြန် လိုက်ရှာပြီး မူလနေရာမှာ ပြင်မှ bug တကယ်ပျောက်ပါတယ်။
Core concept: debugging က process တစ်ခုပါ
အတွေ့အကြုံရှိတဲ့ developer တွေ bug ကို မြန်မြန်ရှာနိုင်တာ ကံကောင်းလို့ မဟုတ်ပါဘူး၊ တူညီတဲ့ အဆင့်တွေကို အမြဲ လိုက်လုပ်လို့ပါ။ အဆင့်တစ်ခုချင်းက ရှာရမယ့် နေရာကို ကျဉ်းသွားစေပါတယ်။
1. Reproduce
Bug ကို အမြဲ ပြန်ဖြစ်အောင် လုပ်ပါ။ ဘယ် input၊ ဘယ် click အစဉ်နဲ့ ဖြစ်လဲ ချရေးထားပါ။ ပြန်မဖြစ်နိုင်တဲ့ bug ကို ပြင်ပြီးပြီလို့ ဘယ်တော့မှ သေချာမပြောနိုင်ပါဘူး။
2. Isolate
Input ကို တဖြည်းဖြည်း ဖြုတ်ပြီး bug ဖြစ်နေသေးတဲ့ အသေးဆုံး case (minimal reproducible case) ကို ရှာပါ။ Item သုံးခုနဲ့ ဖြစ်နေရင် တစ်ခုချင်း ဖြုတ်ကြည့်ပါ။
3. Hypothesis နဲ့ evidence
"price တစ်ခုက string ဖြစ်နေတယ်" လို ခန့်မှန်းချက် တစ်ခု ချပြီး အဲဒါကို အတည်ပြု/ငြင်းပယ်နိုင်မယ့် value တွေကို log ထုတ်ပါ ဒါမှမဟုတ် breakpoint နဲ့ ရပ်ကြည့်ပါ။
4. Fix နဲ့ regression check
အကြောင်းရင်းရှိတဲ့ နေရာကို ပြင်ပြီး bug ပြန်မလာအောင် စစ်တဲ့ check သို့မဟုတ် test တစ်ခု ထည့်ပါ။ ယာယီ log တွေကိုလည်း ပြန်ဖျက်ပါ။
Evidence စုတဲ့အခါ log တစ်ကြောင်းမှာ label၊ value နဲ့ typeof သုံးမျိုးလုံး ပါရင် အသုံးဝင်ဆုံးပါ။ console.log(sum) က 105 ကိုပဲ ပြပေမယ့် console.log("after Pen:", sum, typeof sum) က "after Pen: 105 string" ဆိုပြီး bug ကို ချက်ချင်း ဖော်ပြပါတယ်။ Object array တွေကို console.table() နဲ့ ဇယားအဖြစ် ကြည့်ရင် ပိုမြင်သာပြီး console.trace() က ဒီ function ကို ဘယ်က ခေါ်လဲဆိုတဲ့ call stack ကို print ထုတ်ပေးပါတယ်။
Log တွေ များလာရင် breakpoint က ပိုသန့်ပါတယ်။ Code ထဲမှာ debugger; လို့ ရေးထားရင် DevTools ဖွင့်ထားချိန်မှာ execution အဲဒီ line မှာ ရပ်ပါတယ် (DevTools ပိတ်ထားရင် ဘာမှ မလုပ်ပါဘူး)။ ရပ်ထားချိန် Scope panel မှာ local variable တွေ၊ Call Stack panel မှာ ခေါ်လာတဲ့ လမ်းကြောင်း၊ Watch မှာ ကိုယ်ကြည့်ချင်တဲ့ expression တွေကို မြင်ရပြီး step over / step into နဲ့ တစ်ကြောင်းချင်း ရှေ့ဆက်လို့ ရပါတယ်။ Node.js မှာဆိုရင် node --inspect နဲ့ run ပြီး Chrome DevTools ကို ချိတ်လို့ ရပါတယ်။
- Bug ကို အမြဲ ပြန်ဖြစ်အောင်လုပ်
- အသေးဆုံး failing input အထိ ချုံ့
- Value + typeof ကို log / pause
- မှားတဲ့ value စဝင်တဲ့နေရာကို ပြင်
- Regression check ထည့်
Symptom ပေါ်တဲ့နေရာက စပြီး value ကို နောက်ပြန်လိုက်ပါ — ပထမဆုံး မှားသွားတဲ့ နေရာက bug ရှိတဲ့နေရာပါ။
အထွေထွေ ပုံစံ
console.log("label:", value, typeof value); // value + its type
console.table(arrayOfObjects); // rows and columns
console.trace("how did we get here?"); // call stack (stderr)
console.error("something failed:", error); // error stream
function suspect(input) {
debugger; // pauses here while DevTools is open
return transform(input);
}Log တိုင်းမှာ label ထည့်ပါ၊ ဒါမှ output များလာရင် ဘယ်ဟာ ဘာလဲ ခွဲလို့ရပါမယ်။ console.trace() နဲ့ console.error() က stderr ကို ရေးတာမို့ Node.js မှာ ပုံမှန် stdout output နဲ့ သီးသန့် stream ကို ထွက်ပါတယ်။ debugger statement က DevTools (ဒါမှမဟုတ် node --inspect) ချိတ်ထားမှပဲ အလုပ်လုပ်ပါတယ်။
Example: 10520 ဖြစ်နေတဲ့ cart total ကို debug လုပ်ခြင်း
ဒီ example က process တစ်ခုလုံးကို Node.js မှာ run လို့ရအောင် ပြထားပါတယ်။ ပထမဆုံး bug ကို reproduce လုပ်ပြီး၊ နောက် loop ရဲ့ အဆင့်တိုင်းမှာ value နဲ့ type ကို log ထုတ်ကာ ဘယ်အဆင့်မှာ number ကနေ string ဖြစ်သွားလဲ ရှာပါတယ်။ နောက်ဆုံးမှာ အကြောင်းရင်းကို ပြင်ပြီး check တစ်ခုနဲ့ အတည်ပြုပါတယ်။
// The bug report: "the cart total is 10520, but it should be 35"
const cart = [
{ name: "Notebook", price: 10 },
{ name: "Pen", price: "5" }, // this price came from a form field as text
{ name: "Bag", price: 20 },
];
function total(items) {
let sum = 0;
for (const item of items) {
sum += item.price;
}
return sum;
}
console.log("Total:", total(cart));
// Gather evidence: log every intermediate value together with its type
const DEBUG = true;
function debugLog(label, value) {
if (DEBUG) console.log(`[debug] ${label}:`, value, `(${typeof value})`);
}
function totalTraced(items) {
let sum = 0;
for (const item of items) {
sum += item.price;
debugLog(`after ${item.name}`, sum);
}
return sum;
}
totalTraced(cart);
// Fix the cause (text prices), then prove it with a check
function fixedTotal(items) {
return items.reduce((sum, item) => sum + Number(item.price), 0);
}
console.log("Fixed total:", fixedTotal(cart));
console.log("Regression check:", fixedTotal(cart) === 35 ? "PASS" : "FAIL");Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Reproduce
console.log("Total:", total(cart));Bug ကို ပြင်မယ့်အစား အရင်ဆုံး အမြဲ ပြန်ဖြစ်အောင် လုပ်ပါတယ်။ Output က 10520 ဖြစ်လို့ bug report နဲ့ ကိုက်ပါတယ်။
- Conditional logging
if (DEBUG) console.log(`[debug] ${label}:`, value, `(${typeof value})`);DEBUG flag တစ်ခုနဲ့ debug log တွေကို တစ်နေရာထဲက ဖွင့်/ပိတ်လို့ရပါတယ်။ typeof ကိုပါ print ထုတ်တာက ဒီ bug ကို ဖမ်းဖို့ အဓိကသော့ပါ။
- Evidence inside the loop
debugLog(`after ${item.name}`, sum);Item တစ်ခုချင်းအပြီး sum ကို log ထုတ်ပါတယ်။ Notebook အပြီး 10 (number) ဖြစ်ပြီး Pen အပြီး 105 (string) ဖြစ်သွားလို့ ပြဿနာက Pen ရဲ့ price ဖြစ်ကြောင်း သိရပါတယ်။
- Root cause
{ name: "Pen", price: "5" }, // this price came from a form field as text10 + "5" မှာ operand တစ်ခုက string ဖြစ်လို့ + က ပေါင်းခြင်းမဟုတ်ဘဲ စာသားဆက်ခြင်း လုပ်ပြီး "105" ရပါတယ်။ အဲဒီနောက် "105" + 20 က "10520" ဖြစ်ပါတယ်။
- Fix at the source
return items.reduce((sum, item) => sum + Number(item.price), 0);Price ကို ပေါင်းခါနီး Number() နဲ့ ပြောင်းလိုက်တော့ total က 35 ဖြစ်ပါတယ်။ Real app မှာဆိုရင် form data ဝင်လာတဲ့ နေရာမှာတင် ပြောင်းထားတာ ပိုကောင်းပါတယ်။
- Regression check
console.log("Regression check:", fixedTotal(cart) === 35 ? "PASS" : "FAIL");Bug ဖြစ်စေတဲ့ input နဲ့ အဖြေမှန်ကို check တစ်ခုအဖြစ် သိမ်းထားပါတယ်။ Real project မှာ ဒါက unit test တစ်ခု ဖြစ်လာပါတယ်။
Total: 10520
[debug] after Notebook: 10 (number)
[debug] after Pen: 105 (string)
[debug] after Bag: 10520 (string)
Fixed total: 35
Regression check: PASSဘယ် tool ကို ဘယ်အချိန် သုံးမလဲ
| Tool | အသုံးဝင်တဲ့အချိန် |
|---|---|
| console.log(label, value) | Value တစ်ခု၊ နှစ်ခုကို အမြန် ကြည့်ချင်ချိန် |
| console.table(rows) | Object array ကို ဇယားလို နှိုင်းယှဉ်ချင်ချိန် |
| console.trace() | Function ကို ဘယ်က ခေါ်လဲ မသိချိန် |
| debugger / breakpoint | Variable များစွာကို တစ်ကြောင်းချင်း စောင့်ကြည့်ချင်ချိန် |
| Network panel | API request နဲ့ response ကို စစ်ချင်ချိန် |
Conditional breakpoint
Loop က အကြိမ် ၁၀၀၀ ပတ်ပြီး item တစ်ခုမှာပဲ မှားနေရင် DevTools မှာ line number ကို right-click နှိပ်ပြီး typeof item.price === "string" လို condition ထည့်ထားလို့ ရပါတယ်။ Condition မှန်တဲ့အခါမှပဲ ရပ်ပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Real project မှာ bug report တွေက "order page က total မှားနေတယ်" လို မရှင်းလင်းတဲ့ ပုံစံနဲ့ ရောက်လာတတ်ပါတယ်။ ပထမအလုပ်က ဘယ် user၊ ဘယ် cart နဲ့ ဖြစ်လဲ မေးပြီး local မှာ reproduce လုပ်တာပါ။ နောက်တော့ Network panel မှာ API ပြန်ပို့တဲ့ JSON ကို ကြည့်ပြီး price တွေက number လား string လား စစ်ပါတယ် — server ဘက်က ပြဿနာလား browser ဘက်က ပြဿနာလား ဒီအဆင့်မှာတင် ခွဲလို့ရပါတယ်။
Team တွေမှာ အလေ့အကျင့်ကောင်းတစ်ခုက fix မရေးခင် failing test တစ်ခု အရင်ရေးတာပါ — bug ကို code ပုံစံနဲ့ မှတ်တမ်းတင်ပြီး fix ပြီးရင် test အစိမ်းရောင် ဖြစ်သွားပါတယ်။ Production မှာ breakpoint ချလို့ မရတဲ့အတွက် structured log (request id၊ user id ပါတဲ့ log) နဲ့ error monitoring tool တွေကနေ stack trace တွေကို စုပါတယ်။ Debug အတွက် ထည့်ခဲ့တဲ့ console.log တွေကိုတော့ commit မလုပ်ခင် ဖျက်ပါ၊ ESLint ရဲ့ no-console နဲ့ no-debugger rule တွေက မေ့ကျန်ခဲ့တာကို ဖမ်းပေးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Label မပါတဲ့ log တွေ ပြန့်ကျဲထားတာ
console.log(x) ဆယ်ကြောင်းလောက် ထည့်ထားရင် output မှာ 10, 105, undefined တွေ တန်းစီနေပြီး ဘယ်ဟာ ဘယ်နေရာက လာလဲ မသိနိုင်တော့ပါဘူး။ Log တိုင်းမှာ "after Pen:" လို label နဲ့ typeof ကို ထည့်ပါ။
2. Object ကို log ထုတ်ပြီး နောက်မှ ပြောင်းသွားတာ
Browser console မှာ object ကို log ထုတ်ပြီး နောက်မှ ချဲ့ကြည့်ရင် log ထုတ်ခဲ့တဲ့အချိန်က value မဟုတ်ဘဲ လက်ရှိ value ကို ပြတတ်ပါတယ်။ အဲဒီအချိန်က snapshot လိုရင် console.log(JSON.stringify(obj)) သို့မဟုတ် structuredClone(obj) ကို log ထုတ်ပါ။
3. Sample တစ်ခု အလုပ်ဖြစ်တာနဲ့ ပြီးပြီ ထင်တာ
Bug ဖြစ်စေတဲ့ input တစ်ခု ပြေသွားရုံနဲ့ မလုံလောက်ပါဘူး။ Array အလွတ်၊ price က "abc" ဖြစ်နေတာ (Number() က NaN ပြန်ပါတယ်) လို boundary နဲ့ invalid input တွေကိုပါ စစ်ပါ၊ မဟုတ်ရင် bug အသစ်တစ်ခုကို ဖုံးထားမိနိုင်ပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- တစ်ကြိမ်မှာ hypothesis တစ်ခုပဲ စမ်းပါ၊ ပြောင်းလဲမှု အများကြီး တစ်ပြိုင်နက် လုပ်ရင် ဘယ်ဟာက ပြင်ပေးလိုက်လဲ မသိနိုင်တော့ပါဘူး။
- ဘာစမ်းခဲ့လဲ၊ ဘာတွေ့ခဲ့လဲ မှတ်စု တိုတိုလေး ရေးထားပါ၊ ရှာရတာ ကြာလာရင် လမ်းပျောက်ပြီး တစ်နေရာထဲ ပတ်မနေအောင် ကူညီပါတယ်။
- Fix တိုင်းနဲ့အတူ အဲဒီ bug ကို ဖမ်းနိုင်မယ့် test တစ်ခုကိုပါ commit လုပ်ပါ။
- Error message နဲ့ stack trace ကို အပေါ်ဆုံး line တင်မကဘဲ အပြည့်ဖတ်ပါ၊ ကိုယ့် code ထဲက ပထမဆုံး frame က စရှာရမယ့်နေရာပါ။
Mini challenge
Mini challenge: NaN ပြန်နေတဲ့ average ကို debug လုပ်ပါ
averageScore([80, 90, 70]) က 80 ပြန်ရမှာ NaN ပြန်နေပါတယ်။ Process ကို လိုက်ပြီး bug ကို ရှာ၊ ပြင်ပြီး regression check သုံးခု ရေးပါ။
- Loop ထဲမှာ i နဲ့ scores[i] ကို label ပါတဲ့ log နဲ့ ကြည့်ပြီး bug ကို ရှာပါ
- Loop condition ကို ပြင်ပါ
- Array အလွတ်အတွက် NaN မဟုတ်ဘဲ 0 ပြန်ပါ
- check(label, actual, expected) helper နဲ့ PASS/FAIL သုံးကြောင်း print ထုတ်ပါ
// Reported: averageScore([80, 90, 70]) returns NaN instead of 80
function averageScore(scores) {
let sum = 0;
for (let i = 0; i <= scores.length; i++) {
// TODO: log a labelled i, scores[i] and typeof sum to gather evidence
sum += scores[i];
}
return sum / scores.length;
}
function check(label, actual, expected) {
// TODO: print "PASS label: ..." or "FAIL label: ..."
}
// TODO: after fixing, check [80, 90, 70] -> 80, [55] -> 55, [] -> 0ရလဒ်: Check သုံးခုလုံး PASS ဖြစ်ရပါမယ်: PASS three scores: got 80, expected 80 PASS one score: got 55, expected 55 PASS empty list: got 0, expected 0
အရိပ်အမြွက် ကြည့်ရန်
Array မှာ item သုံးခုရှိရင် index တွေက 0, 1, 2 ပဲ ရှိပါတယ်။ i က 3 ဖြစ်တဲ့အခါ scores[3] က ဘာလဲ၊ 240 + အဲဒီ value က ဘာဖြစ်မလဲ စဉ်းစားပါ။ Array အလွတ်မှာ 0 / 0 ကလည်း NaN ပါ။
အဖြေ ကြည့်ရန်
// Evidence: with i <= scores.length the loop read scores[3], which is
// undefined, and 240 + undefined is NaN. An empty array gave 0 / 0 = NaN.
function averageScore(scores) {
if (scores.length === 0) return 0;
let sum = 0;
for (let i = 0; i < scores.length; i++) {
sum += scores[i];
}
return sum / scores.length;
}
function check(label, actual, expected) {
const status = actual === expected ? "PASS" : "FAIL";
console.log(`${status} ${label}: got ${actual}, expected ${expected}`);
}
check("three scores", averageScore([80, 90, 70]), 80);
check("one score", averageScore([55]), 55);
check("empty list", averageScore([]), 0);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Debugging က process ပါ: reproduce → isolate → hypothesis → evidence → fix → regression check။
- Log တိုင်းမှာ label၊ value နဲ့ typeof ပါအောင် ရေးပါ — type မှားတဲ့ bug တွေကို ချက်ချင်း မြင်ရပါတယ်။
- console.table က object array ကို ဇယားပြ၊ console.trace က call stack ပြ၊ debugger နဲ့ breakpoint က code ကို ရပ်ပြီး variable တွေကို ကြည့်ခွင့်ပေးပါတယ်။
- Symptom ပေါ်တဲ့ line ကို မဟုတ်ဘဲ မှားတဲ့ value ဝင်လာတဲ့နေရာကို ပြင်ပြီး ယာယီ log တွေကို ဖျက်ပါ။
MDN — JavaScript Guide — MDN Web Docs