Web appOpen in Telegram

Postیکی از ساده‌ترین تکنیک‌های Refactoring برای تمیزتر شدن کد جاوااسکریپت، استفاده از الگوی Early Return است ✨

28 August 2026
|
| کانال توسعه‌دهندگان جاوااسکریپت |
یکی از ساده‌ترین تکنیک‌های Refactoring برای تمیزتر شدن کد جاوااسکریپت، استفاده از الگوی Early Return است ✨ در این الگو به‌جای اینکه یک تابع را با if تو در تو و بلاک‌های بزرگ بپیچیم، شرط‌های نامعتبر را زودتر برمی‌گردانیم و مسیر «عادی» تابع را خوانا و سرراست نگه می‌کنیم. نمونه کد شلوغ بدون Early Return 👇 function createOrder(user, items) { if (user) { if (user.isActive) { if (Array.isArray(items) && items.length > 0) { const total = items.reduce((sum, it) => sum + it.price, 0); if (total > 0) { return { userId: user.id, total, status: "CREATED" }; } else { throw new Error("Total must be positive"); } } else { throw new Error("Items required"); } } else { throw new Error("User not active"); } } else { throw new Error("User required"); } } خواندن این تابع سخت است؛ منطق اصلی بین ifهای تودرتو گم شده. همان منطق با Early Return 🧼 function createOrder(user, items) { if (!user) { throw new Error("User required"); } if (!user.isActive) { throw new Error("User not active"); } if (!Array.isArray(items) || items.length === 0) { throw new Error("Items required"); } const total = items.reduce((sum, it) => sum + it.price, 0); if (total <= 0) { throw new Error("Total must be positive"); } return { userId: user.id, total, status: "CREATED" }; } حالا: • تمام شرط‌های نامعتبر بالا جمع شده‌اند و سریع تابع را ترک می‌کنند ✅ • «مسیر خوشحال» وسط تابع مثل یک داستان خطی و قابل‌خواندن است ✅ • دیباگ و لاگ‌گرفتن ساده‌تر شده است ✅ نکته کاربردی در پروژه‌های React / Node.js در هندلرهای API و کنترلرهای Express هم همین الگو کمک زیادی می‌کند تا از callback hell جدید یعنی if hell فرار شود: export const handler = async (req, res) => { const { user } = req; if (!user) { return res.status(401).json({ message: "Unauthorized" }); } if (!user.isActive) { return res.status(403).json({ message: "Inactive user" }); } const data = await loadDashboardData(user.id); return res.json(data); }; در مستندات MDN درباره return و جریان کنترل توضیحات بیشتری آمده که مطالعه‌اش دید خوبی می‌دهد. هرجا تابعی دیدۀ می‌شود که تو در تو و سخت‌خوان است، یک بار با Early Return بازنویسی‌اش، هم برای خودتان هم برای تیم، خوانایی کد را چند درجه بالا می‌برد 🚀 🔖 #Javascript #JS #جاوااسکریپت #JavaScript #Clean_Code #Refactoring #Early_Return #Node_js #Express #Readability 👤 Developix 💎 Channel: @DevelopixJavascript
7 · 965 ·

Nearby in the feed

|| کانال توسعه‌دهندگان جاوااسکریپت |💠 نسخه جدید سوپرست تایپ‌اسکریپت (Typescript) منتشر شده است 🔸 ارتقا از نسخه 6.0.3 به 7.0.2 🔹 لیست تغییرات این نسخه: 🔖 #typescript, #web, #js, #react, #vue, #rele|| کانال توسعه‌دهندگان جاوااسکریپت |💠 نسخه جدید ران‌تایم نود جی‌اس (NodeJS) منتشر شده است 🔸 ارتقا از نسخه 26.7.0 به 26.8.1 🔹 لیست تغییرات این نسخه: - رفع مشکل نمایش اشتباه نسخه در خروجی node --ver
this message
|| کانال توسعه‌دهندگان جاوااسکریپت |بررسی نوع داده Symbol در جاوا اسکریپت در این مطلب به معرفی نوع داده Primitive و یکتای Symbol، تفاوت آن با سایر انواع داده، نحوه استفاده از Global Symbol Registr|| کانال توسعه‌دهندگان جاوااسکریپت |🔥 پروژه: ‎دانلودر TikTok و Douyin ابزار متن‌باز TikTokDownloader برای دانلود و جمع‌آوری داده از ویدئوهای TikTok و نسخه چینی آن Douyin طراحی شده 🛰️ به‌درد جاوااس
|| کانال توسعه‌دهندگان جاوااسکریپت || کانال توسعه‌دهندگان جاوااسکریپت |@DevelopixJavascript · channel
3 879subscribers681average post reach
Venue feed Open in Telegram

An open public feed from the search index ChatCrawler — “Google for public Telegram”; refreshed as the venue is crawled. Times are UTC.

Public content only, official Telegram API. About · FAQ · What we do not do · Remove a page · Catalog · Search · How we count