Writing Clean JavaScript
Code ရေးတယ်ဆိုတာ ကွန်ပျူတာ နားလည်ဖို့တင် မဟုတ်ပါဘူး။ လူ (ကိုယ့်ကိုယ်တိုင်နဲ့ တခြား Developer တွေ) နားလည်ဖို့လည်း အရေးကြီးပါတယ်။
1. Naming Conventions (နာမည်ပေးခြင်း)
Section titled “1. Naming Conventions (နာမည်ပေးခြင်း)”နာမည်ပေးတဲ့အခါ အဓိပ္ပာယ်ရှိတဲ့ နာမည် ပေးရပါမယ်။ CamelCase ကို သုံးတာ Standard ပါ။
- ❌
let x = 10;(ဘာ ၁၀ လဲ မသိရဘူး) - ✅
let studentAge = 10;(ရှင်းလင်းတယ်) - ❌
function abc() {} - ✅
function calculateTax() {}
2. Comments (မှတ်ချက် ရေးခြင်း)
Section titled “2. Comments (မှတ်ချက် ရေးခြင်း)”Code က ဘာလုပ်တာလဲ ဆိုတာကို ရှင်းပြထားသင့်ပါတယ်။ ဒါပေမဲ့ လိုအပ်မှ ရေးပါ။ Code က သူ့ဟာသူ ရှင်းနေရင် မလိုပါဘူး။
// Good Comment: ဘာကြောင့် ဒီလိုရေးရလဲ ရှင်းပြတာ// We use 3.14 because simpler precision is enough hereconst pi = 3.14;
// Bad Comment: ကုဒ်က ဘာလုပ်လဲ ဆိုတာ လိုက်ပြောနေတာ (Code ဖတ်ရင် သိတာကို)let age = 20; // set age to 20 (မလိုပါဘူး)3. Formatting (သပ်ရပ်မှု)
Section titled “3. Formatting (သပ်ရပ်မှု)”{} တွေ၊ နေရာခြားတာ (Indentation) တွေကို သပ်ရပ်အောင် ထားပါ။
Prettier လို Extension မျိုး သုံးရင် အလိုအလျောက် လုပ်ပေးသွားပါလိမ့်မယ်။
4. Don’t Repeat Yourself (DRY)
Section titled “4. Don’t Repeat Yourself (DRY)”Code တူတွေ ခဏခဏ ရေးနေရပြီဆိုရင် Function ခွဲထုတ်လိုက်ပါ။ ပြင်ရခက်တာ၊ မှားလွယ်တာတွေ သက်သာသွားပါလိမ့်မယ်။