ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Code ကို ကွန်ပျူတာ တစ်ကြိမ် run ပါတယ်၊ ဒါပေမယ့် လူတွေက အကြိမ်ပေါင်းများစွာ ဖတ်ကြပါတယ် — ခြောက်လကြာရင် ကိုယ်တိုင်တောင် ပြန်ဖတ်ရမှာပါ။ Best practice တွေဆိုတာ စည်းကမ်းတင်းကျပ်ဖို့ မဟုတ်ပါဘူး၊ developer အများကြီး အမှားလုပ်ခဲ့ဖူးတဲ့ နေရာတွေမှာ ထောင်ချောက်မမိအောင် ချထားတဲ့ လမ်းညွှန်ဆိုင်းဘုတ်တွေပါ။
ဥပမာ var အစား const ကို သုံးတာက "ဒီ value ကို ဘယ်တော့မှ ပြန်မ assign ဘူး" လို့ ဖတ်တဲ့သူကို ကတိပေးတာပါ။ == အစား === သုံးတာက JavaScript ရဲ့ အလိုအလျောက် type ပြောင်းခြင်းကြောင့် ဖြစ်တဲ့ အံ့ဩစရာ ရလဒ်တွေကို ဖယ်ရှားပေးပါတယ်။ အလေ့အကျင့် တစ်ခုချင်းစီက bug အမျိုးအစား တစ်ခုခုကို ကာကွယ်ပေးပါတယ်။
Problem → Why → Solution
ပြဿနာ: ပထမတစ်ကြိမ် မှန်ပြီး ဒုတိယတစ်ကြိမ် မှားတဲ့ code
အောက်က code က ပေးချေပြီးသား invoice တွေရဲ့ total ကို တွက်ပါတယ်။ ပထမ ခေါ်တဲ့အခါ 150 ဆိုတဲ့ အဖြေမှန် ရပါတယ်၊ ဒုတိယ ခေါ်တဲ့အခါ 300 ဖြစ်သွားပါတယ်။ Function နာမည် calc နဲ့ variable နာမည် t, d, s, a တွေကနေ ဘာလုပ်လဲ ခန့်မှန်းဖို့ ခက်ပြီး == ကလည်း နောက်ထပ် bug တစ်ခု ပုန်းနေနိုင်တဲ့ နေရာပါ။
var t = 0; // global: every call keeps adding to it
function calc(d) {
for (var i = 0; i < d.length; i++) {
if (d[i].s == "paid") t = t + d[i].a;
}
return t;
}
console.log(calc(invoices)); // 150
console.log(calc(invoices)); // 300 <- same input, different answerဘာကြောင့် ဒီလို ဖြစ်တာလဲ
t က function အပြင်မှာ ရှိတဲ့ global variable ဖြစ်လို့ ခေါ်တိုင်း ယခင် total ပေါ်မှာ ဆက်ပေါင်းပါတယ်။ Function တစ်ခုရဲ့ ရလဒ်က argument တွေအပြင် အပြင်က mutable state ပေါ်မှာပါ မူတည်နေရင် input တူတာနဲ့ output တူမယ်လို့ အာမခံချက် မရှိတော့ပါဘူး။ sum ကို function ထဲမှာ const/let နဲ့ ကြေညာပြီး === နဲ့ နာမည်ကောင်းတွေ သုံးလိုက်ရင် bug ရော နားလည်ရခက်တာပါ ပျောက်သွားပါတယ်။
Core concept: အလေ့အကျင့်တစ်ခုစီက bug တစ်မျိုးကို ကာကွယ်တယ်
Best practice စာရင်းကို အလွတ်ကျက်တာထက် "ဒါက ဘယ် bug ကို တားလဲ" လို့ မေးကြည့်တာ ပိုအသုံးဝင်ပါတယ်။ အကြောင်းရင်းကို နားလည်ရင် စည်းမျဉ်းမရှိတဲ့ အခြေအနေအသစ်မှာလည်း ကိုယ်တိုင် ဆုံးဖြတ်နိုင်ပါတယ်။
| အလေ့အကျင့် | ကာကွယ်ပေးတဲ့ bug |
|---|---|
| var အစား const / let | Block အပြင် ပေါက်ထွက်တဲ့ variable နဲ့ မရည်ရွယ်ဘဲ ပြန် assign လုပ်မိတာ |
| == အစား === | 0 == "" နဲ့ "0" == false လို coercion ကြောင့် true ဖြစ်သွားတာ |
| Strict mode | Variable ကြေညာဖို့ မေ့ပြီး global အသစ် ဖြစ်သွားတာ |
| Global mutable state ရှောင် | ခေါ်တဲ့ အစဉ်ပေါ် မူတည်ပြီး ရလဒ်ပြောင်းတာ |
| Boundary မှာ validate | မှားတဲ့ data က program အတွင်းထဲ နက်နက် ရောက်ပြီးမှ crash တာ |
const ကို default အဖြစ် သုံးပြီး တကယ် ပြန် assign လုပ်ရမယ့် counter လိုမျိုးမှာပဲ let သုံးပါ။ const က object ထဲက property တွေကို ပြောင်းလို့ မရအောင် မလုပ်ပါဘူး၊ variable ကို တခြား value ဆီ ပြန်မညွှန်ဖို့ပဲ တားတာပါ။ var ကတော့ function scope ဖြစ်လို့ if သို့မဟုတ် for block ထဲမှာ ကြေညာထားရင်တောင် block အပြင်ကနေ မြင်ရနေပါတယ်။
== က operand နှစ်ခုရဲ့ type မတူရင် comparison မလုပ်ခင် type ပြောင်းပါတယ်၊ ဒါကြောင့် 0 == ""၊ "0" == false နဲ့ null == undefined အားလုံး true ဖြစ်ပါတယ်။ === ကတော့ type မတူရင် false ချက်ချင်း ပြန်ပါတယ်။ 'use strict' ကို file ထိပ်မှာ ရေးရင် ကြေညာမထားတဲ့ variable ကို assign လုပ်တာက global အသစ် မဖန်တီးတော့ဘဲ ReferenceError ဖြစ်ပါတယ်။ ES module (import/export ပါတဲ့ file) တွေနဲ့ class body တွေက strict mode အလိုအလျောက် ဖြစ်ပြီးသားပါ။
နာမည်ကောင်းနဲ့ function သေးသေးလေးတွေက တစ်ခုနဲ့တစ်ခု ဆက်စပ်ပါတယ်။ Function တစ်ခုက အလုပ်တစ်ခုပဲ လုပ်ရင် subtotal()၊ assertValidItem() လို တိကျတဲ့ နာမည် ပေးလို့ ရပြီး test ရေးရတာလည်း လွယ်ပါတယ်။ Comment တွေကတော့ code က ဘာလုပ်လဲ ထပ်ပြောဖို့ မဟုတ်ဘဲ ဘာကြောင့် ဒီလို ဆုံးဖြတ်ခဲ့လဲ ရှင်းပြဖို့ ရေးပါ။
- Input က boundary ကို ဝင်လာ
- Type နဲ့ value ကို validate လုပ်
- Pure function သေးသေးလေးတွေ တွက်
- ရလဒ်ကို return (global မပြောင်း)
Validate လုပ်ပြီးသား data ကို အတွင်းက function တွေ ယုံလို့ရတာကြောင့် function တိုင်းမှာ စစ်ဆေးချက်တွေ ထပ်ခါထပ်ခါ ရေးစရာ မလိုပါဘူး။
အထွေထွေ ပုံစံ
'use strict';
const NAMED_CONSTANT = 0.05; // no magic numbers
function doOneThing(input, { option = DEFAULT } = {}) {
if (!isValid(input)) throw new TypeError("why it was rejected");
const result = helper(input); // const unless reassigned
return result; // return, do not mutate globals
}
if (actual === expected) { /* strict equality */ }Function တစ်ခုက input ကို အရင်စစ်ပြီး မှားရင် ဘာကြောင့်လဲ ပြောတဲ့ error ကို throw လုပ်ပါတယ်။ Option တွေကို default ပါတဲ့ destructured object အဖြစ် လက်ခံရင် ခေါ်တဲ့နေရာမှာ ဖတ်ရလွယ်ပါတယ်။ ရလဒ်ကို return ပြန်ပြီး အပြင်က state ကို မပြောင်းပါဘူး။
Example: သန့်ရှင်းတဲ့ order total
ဒီ example က lesson ထဲက အလေ့အကျင့်တွေကို တစ်နေရာထဲမှာ စုပြထားပါတယ်: နာမည်ပေးထားတဲ့ constant၊ အလုပ်တစ်ခုစီ လုပ်တဲ့ function တွေ၊ options object နဲ့ default value၊ input ကို ဝင်ပေါက်မှာ စစ်တာနဲ့ error ကို ဖမ်းပြီး ရှင်းရှင်းလင်းလင်း ပြတာပါ။ အပေါ်က legacy calc() နဲ့ နှိုင်းယှဉ်ကြည့်ပါ — အလုပ်အမျိုးအစား တူပေမယ့် အခု နာမည်တိုင်းက သူ့အလုပ်ကို ပြောပြနေပါပြီ။
'use strict';
const TAX_RATE = 0.05; // a named constant instead of a "magic number"
function assertValidItem(item) {
if (typeof item.price !== "number" || item.price < 0) {
throw new TypeError(`Invalid price for ${item.name}: ${item.price}`);
}
}
function subtotal(items) {
return items.reduce((sum, item) => sum + item.price * item.qty, 0);
}
function orderTotal(items, { taxRate = TAX_RATE } = {}) {
if (!Array.isArray(items)) {
throw new TypeError("items must be an array");
}
items.forEach(assertValidItem);
const net = subtotal(items);
return Math.round(net * (1 + taxRate) * 100) / 100;
}
const cart = [
{ name: "Tea", price: 2.5, qty: 2 },
{ name: "Cake", price: 4, qty: 1 },
];
console.log(`Subtotal: ${subtotal(cart)}`);
console.log(`Total: ${orderTotal(cart)}`);
console.log(`Total without tax: ${orderTotal(cart, { taxRate: 0 })}`);
for (const badInput of [null, [{ name: "Gift", price: "free", qty: 1 }]]) {
try {
orderTotal(badInput);
} catch (error) {
console.log(`Rejected: ${error.message}`);
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Named constant
const TAX_RATE = 0.05; // a named constant instead of a "magic number"Code ထဲ ဟိုနေရာဒီနေရာမှာ 0.05 ရေးမယ့်အစား နာမည်ပေးထားလို့ ဆိုလိုရင်းက ရှင်းပြီး rate ပြောင်းရင် တစ်နေရာထဲ ပြင်ရုံပါပဲ။
- One job per function
function subtotal(items) {subtotal() က ပေါင်းတာ တစ်ခုပဲ လုပ်ပြီး assertValidItem() က စစ်တာ တစ်ခုပဲ လုပ်ပါတယ်။ နာမည်ကို ဖတ်ရုံနဲ့ ဘာလုပ်လဲ သိပြီး တစ်ခုချင်း test လုပ်လို့ရပါတယ်။
- Options object with a default
function orderTotal(items, { taxRate = TAX_RATE } = {}) {orderTotal(cart, { taxRate: 0 }) လို ခေါ်တဲ့အခါ 0 က ဘာလဲ ချက်ချင်း နားလည်ပါတယ်။ = {} က options လုံးဝမပါရင် destructuring error မဖြစ်အောင် ကာကွယ်ပါတယ်။
- Validate at the boundary
items.forEach(assertValidItem);တွက်ချက်မှု မစခင် item တိုင်းကို စစ်ပါတယ်။ "free" ဆိုတဲ့ price က NaN total အဖြစ် အနောက်ထိ ရောက်မသွားဘဲ ဘာမှားလဲ ပြောတဲ့ TypeError ဖြစ်ပါတယ်။
- Handle errors deliberately
console.log(`Rejected: ${error.message}`);Error ကို catch လုပ်ပြီး ဘာဖြစ်လဲ ပြတာက error ကို မျိုချ (swallow) လိုက်တာနဲ့ မတူပါဘူး။ Input နှစ်ခုလုံး ဘာကြောင့် ငြင်းပယ်ခံရလဲ output မှာ မြင်ရပါတယ်။
Subtotal: 9
Total: 9.45
Total without tax: 9
Rejected: items must be an array
Rejected: Invalid price for Gift: freeသတိထားရမယ့် ထောင်ချောက် လေးခု
ဒီ tutorial ရဲ့ နောက်ပိုင်း lesson တွေမှာ အသေးစိတ် လေ့လာရမယ့် ထောင်ချောက်တွေ ရှိပါတယ်။ အခုကတည်းက နာမည်လောက်တော့ သိထားရင် code review မှာ တွေ့တဲ့အခါ မှတ်မိပါလိမ့်မယ်။
| ထောင်ချောက် | ရှောင်နည်း |
|---|---|
| Callback ထဲမှာ this ပျောက်သွားတာ | Method ကို callback အဖြစ် ပို့ရင် arrow function သို့မဟုတ် bind() သုံးပါ |
| Callback တွေ အထပ်ထပ် (callback hell) | Promise နဲ့ async/await သုံးပြီး အဆင့်တွေကို အပေါ်ကနေ အောက် ဖတ်လို့ရအောင် ရေးပါ |
| Async error ကို မဖမ်းတာ | await ကို try/catch နဲ့ အုပ်ပါ ဒါမှမဟုတ် .catch() ထည့်ပါ |
| Memory leak | မလိုတော့တဲ့ event listener၊ timer နဲ့ ကြီးမားတဲ့ cache တွေကို ဖယ်ရှားပါ |
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Team project တွေမှာ ဒီအလေ့အကျင့်တွေကို လူက မှတ်ထားဖို့ မမျှော်လင့်ပါဘူး၊ tool တွေက စစ်ပေးပါတယ်။ ESLint က no-var၊ eqeqeq (=== မသုံးတာကို ဖမ်း)၊ no-unused-vars လို rule တွေနဲ့ ထောင်ချောက်တွေကို editor ထဲမှာတင် ပြပေးပြီး Prettier လို formatter က indentation နဲ့ quote ပုံစံ ငြင်းခုံမှုတွေကို အပြီးသတ်ပေးပါတယ်။ CI pipeline က lint နဲ့ test မအောင်ရင် merge လုပ်ခွင့် မပေးပါဘူး။
Express route handler ရှည်ကြီးတစ်ခုကို refactor လုပ်တဲ့အခါ parse → validate → compute → save → respond ဆိုပြီး function သေးသေးလေးတွေ ခွဲတာ အဖြစ်များပါတယ်။ ဒီလိုခွဲလိုက်ရင် compute အပိုင်းကို database မလိုဘဲ unit test လုပ်လို့ရပြီး validation error တွေကလည်း တစ်နေရာထဲက တူညီတဲ့ ပုံစံနဲ့ ထွက်ပါတယ်။ Performance အတွက် code ကို ရှုပ်ထွေးအောင် မရေးခင် profiler နဲ့ အရင် တိုင်းတာပါ၊ များသောအားဖြင့် ဖတ်ရလွယ်တဲ့ version က လုံလောက်အောင် မြန်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. const ဆိုရင် object ပြောင်းလို့မရဘူး ထင်တာ
const settings = { theme: "dark" }; ပြီးရင် settings.theme = "light" က အလုပ်ဖြစ်ပါတယ်၊ settings = {} ကိုပဲ TypeError နဲ့ တားတာပါ။ Object ကို လုံးဝ မပြောင်းစေချင်ရင် Object.freeze() ကို သုံးပါ (shallow ပဲ freeze လုပ်ပါတယ်)။
2. Error ကို catch လုပ်ပြီး ဘာမှမလုပ်တာ
try { save(); } catch (e) {} လို အလွတ် catch block က bug ကို ဖျောက်ထားပါတယ် — save မအောင်မြင်ပေမယ့် user ကို ဘာမှ မပြောဘဲ app က ဆက်လုပ်နေပါတယ်။ Log ထုတ်ပါ၊ user ကို ပြပါ ဒါမှမဟုတ် context ထည့်ပြီး ပြန် throw လုပ်ပါ။
3. Code ကို ပြန်ပြောတဲ့ comment တွေ
i++; // add 1 to i လို comment က အသစ် ဘာမှ မပြောသလို code ပြောင်းသွားရင် မှားတဲ့ comment ဖြစ်ကျန်ခဲ့ပါတယ်။ "Price တွေကို cents နဲ့ သိမ်းထားတယ်၊ float rounding error ရှောင်ဖို့" လို ဘာကြောင့်လဲ ကို ရှင်းပြတဲ့ comment ကပဲ တန်ဖိုးရှိပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Project အစကတည်းက ESLint နဲ့ formatter ကို ထည့်ပြီး save လုပ်တိုင်း run အောင် editor ကို set လုပ်ပါ။
- Function တစ်ခုက screen တစ်မျက်နှာထက် ရှည်လာရင် ဒါမှမဟုတ် နာမည်မှာ "and" ပါလာရင် ခွဲဖို့ အချိန်ရောက်ပြီပါ။
- Argument အဖြစ် ရလာတဲ့ array သို့မဟုတ် object ကို ပြင်မယ့်အစား value အသစ်တစ်ခု return ပြန်ပါ၊ ဒါမှ ခေါ်တဲ့သူရဲ့ data က မထင်မှတ်ဘဲ မပြောင်းသွားမှာပါ။
Mini challenge
Mini challenge: legacy invoice code ကို refactor လုပ်ပါ
Problem section က calc() ကို lesson ထဲက အလေ့အကျင့်တွေနဲ့ ပြန်ရေးပါ။ အကြိမ်ကြိမ် ခေါ်လည်း ရလဒ် မပြောင်းရပါဘူး။
- Function ကို totalPaid(invoices) လို့ နာမည်ပေးပြီး global variable မသုံးရ
- "paid" ကို named constant အဖြစ် ထားပြီး === နဲ့ နှိုင်းယှဉ်ရ
- Array မဟုတ်တဲ့ input ကို TypeError နဲ့ ငြင်းရ
- Function ကို နှစ်ကြိမ် ခေါ်ပြီး ရလဒ်တူကြောင်း၊ error message ကိုပါ print ထုတ်ရ
// Legacy version (do not keep it):
// var t = 0;
// function calc(d) {
// for (var i = 0; i < d.length; i++) {
// if (d[i].s == "paid") t = t + d[i].a;
// }
// return t;
// }
// TODO: sample data with id, status ("paid" or "draft") and amount fields
function totalPaid(invoices) {
// TODO: throw a TypeError when invoices is not an array
// TODO: keep paid invoices (compare with ===) and add up their amounts
}
// TODO: print the total twice, then print the error for totalPaid("oops")ရလဒ်: Paid amount 120 နဲ့ 30 ဆိုရင်: First call: 150 Second call: 150 Rejected: TypeError: invoices must be an array
အရိပ်အမြွက် ကြည့်ရန်
filter() နဲ့ paid invoice တွေကို ရွေးပြီး reduce((sum, invoice) => sum + invoice.amount, 0) နဲ့ ပေါင်းပါ။ Running total က reduce ထဲမှာပဲ ရှိလို့ global မလိုတော့ပါဘူး။
အဖြေ ကြည့်ရန်
const STATUS_PAID = "paid";
const invoices = [
{ id: "A1", status: "paid", amount: 120 },
{ id: "A2", status: "draft", amount: 80 },
{ id: "A3", status: "paid", amount: 30 },
];
function totalPaid(list) {
if (!Array.isArray(list)) {
throw new TypeError("invoices must be an array");
}
return list
.filter((invoice) => invoice.status === STATUS_PAID)
.reduce((sum, invoice) => sum + invoice.amount, 0);
}
console.log(`First call: ${totalPaid(invoices)}`);
console.log(`Second call: ${totalPaid(invoices)}`);
try {
totalPaid("oops");
} catch (error) {
console.log(`Rejected: ${error.name}: ${error.message}`);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Best practice တစ်ခုချင်းစီက bug အမျိုးအစား တစ်ခုကို ကာကွယ်ပါတယ် — စည်းမျဉ်းကို အကြောင်းရင်းနဲ့ တွဲမှတ်ပါ။
- const ကို default အဖြစ်၊ let ကို ပြန် assign လုပ်မှသာ သုံးပြီး var နဲ့ global mutable state ကို ရှောင်ပါ။
- === ကို အမြဲ သုံးပါ၊ == ရဲ့ type coercion က 0 == "" လို အံ့ဩစရာ ရလဒ်တွေ ပေးပါတယ်။
- Input ကို boundary မှာ validate လုပ်ပြီး function သေးသေးလေးတွေ၊ နာမည်ကောင်းတွေ၊ "ဘာကြောင့်" ကို ရှင်းပြတဲ့ comment တွေ သုံးပါ၊ ESLint နဲ့ formatter က ကျန်တာ စစ်ပေးပါမယ်။
MDN — JavaScript Guide — MDN Web Docs