Constants & Magic Values
Magic numbers named constants se kaise badlein. Status strings, limits aur keys ko const objects aur union types mein rakhna, TypeScript examples ke saath.
On this page
1. Yeh practice kya hai?
Magic value woh literal hai jo code mein achanak aata hai aur apna matlab nahi batata: if (attempts > 5), status === "pnd", setTimeout(fn, 86400000). Parhne wala sochta hai: 5 kyun? "pnd" kya hai? Yeh number kitna hai?
| Qisam | Misaal | Behtar |
|---|---|---|
| Limit | 5 | MAX_LOGIN_ATTEMPTS |
| Duration | 86400000 | ONE_DAY_MS |
| Status string | "pending" har file mein | ORDER_STATUS.pending ya union type |
| Storage key | localStorage.getItem("tkn") | STORAGE_KEYS.token |
| Route | "/dashboard/settings" | ROUTES.settings |
2. Kyun zaroori hai?
Magic values ke do nuqsaan hain: matlab chhupta hai, aur badalna mushkil hai. Limit 5 se 10 karni hai, to har 5 dhoondo aur socho kaunsa wala login ka hai aur kaunsa kisi aur cheez ka.
| Magic values | Named constants |
|---|---|
| pending ka typo pendng, koi error nahi, bug chupke se | Typo par TypeScript foran error deta hai |
| 5 ka matlab andaaza | MAX_LOGIN_ATTEMPTS khud batata hai |
| Limit badalni ho to poore repo mein search | Ek constant badlo |
| Do files mein alag value, UI aur API ka jawab alag | Ek source, ek jawab |
| Naya status add kiya, kuch jagah handle nahi hua | Record type batata hai kahan missing hai |
3. Kab follow karein?
| Level | Kitna zaroori | Kyun |
|---|---|---|
| Learning project | Zaroori | Aasaan aadat, aur typos ke bugs se bachati hai |
| Production code | Status, limits aur keys hamesha named | Repeated literals sab se aam drift bug hain |
| Senior level | Constants kahan rehte hain, yeh structure banao | Ek module ke constants us ke types ke saath, shared wale ek jagah |
4. Bura vs acha code
async function login(email: string, password: string) {
const user = await users.byEmail(email);
if (user.failedAttempts >= 5) {
throw new Error("locked");
}
if (user.status === "suspnded") {
throw new Error("suspended");
}
localStorage.setItem("tkn", await auth.token(user, password));
setTimeout(logout, 1800000);
}export const MAX_LOGIN_ATTEMPTS = 5;
export const SESSION_TIMEOUT_MS = 30 * 60 * 1000;
export const USER_STATUS = {
active: "active",
suspended: "suspended",
} as const;
export type UserStatus = (typeof USER_STATUS)[keyof typeof USER_STATUS];
export const STORAGE_KEYS = {
token: "auth_token",
} as const;async function login(email: string, password: string) {
const user = await users.byEmail(email);
if (user.failedAttempts >= MAX_LOGIN_ATTEMPTS) {
throw new LockedError(user.id);
}
if (user.status === USER_STATUS.suspended) {
throw new SuspendedError(user.id);
}
localStorage.setItem(STORAGE_KEYS.token, await auth.token(user, password));
setTimeout(logout, SESSION_TIMEOUT_MS);
}| Farq kya hai? | Bura | Acha |
|---|---|---|
5 | Kya hai? | MAX_LOGIN_ATTEMPTS |
"suspnded" | Typo, kabhi match nahi hoga, suspended user login kar lega | USER_STATUS.suspended, typo compile error |
1800000 | Ginti karo | 30 * 60 * 1000, parhne mein 30 minute |
"tkn" | Har file mein alag likha ja sakta hai | Ek key, ek jagah |
Pehle version ka typo ek security bug hai: suspended user login kar sakta hai, aur koi error nahi aata.
Typed accessor, magic key nahi
const theme = store.get("thme");const theme = settings.theme();Jab key ek typed function ke peeche ho, to galat key likhna mumkin hi nahi.
5. Kaise apply karein?
6. Exceptions aur common mistakes
Har literal magic nahi. 0, 1, khaali string, index + 1, ya array.length === 0 sab ka matlab saaf hai. Test files mein example values ("test@example.com") bhi theek hain.
User ko dikhne wale text ko bhi ek jagah rakhna achha hai (translation ke liye zaroori), lekin har button label ke liye constant banana chhote project mein zaroori nahi.
Galati: Constant ka naam value jaisa
Why? FIVE = 5 ya SECONDS_60 matlab nahi batata, kal value badli to naam jhoot bolega.
Behtar: Naam kaam bataye, jaise MAX_LOGIN_ATTEMPTS, value nahi.
Galati: Ek constants.ts jis mein poori app ke constants
Why? Har module ki values ek file mein, file bohat bari aur har koi isay chhedta hai.
Behtar: Har constant us module ke paas rakho jo use karta hai, sirf asli shared wale ek shared file mein.
Galati: TypeScript enum aur string union mix
Why? Ek jagah enum, ek jagah string, comparison aur imports confusing ho jate hain.
Behtar: Repo mein ek pattern chuno, jaise as const object plus derived union type.
Galati: Status ko bina type ke string rakhna
Why? status: string kisi bhi typo ko qabool kar leta hai.
Behtar: status ko union type do taake sirf valid values chalein.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | Variables, Self-Documenting Code |
| Saath mein | Naming Things, DRY Without Tight Coupling |
| Baad mein | Strict Typing, Types Beside Logic, Design Tokens & UI Consistency |
Final quick revision
| Check | Sawal |
|---|---|
| Matlab | Kya har number aur string ka matlab saaf hai? |
| Naam | Kya constant ka naam kaam batata hai, value nahi? |
| Set | Kya statuses as const object ya union type hain? |
| Ek jagah | Kya repeated value ek hi jagah defined hai? |
| Keys | Kya storage aur state keys typed accessor ke peeche hain? |