البرمجة
تحسين بنية الشيفرة: أفضل ممارسات لبرمجة JavaScript
07/02/2024آخر تحديث: 07/02/2024
6 دقيقة واحدة
بالطبع، يمكننا تحسين بنية الشيفرة البرمجية الخاصة بك لتجعلها أكثر فعالية وقراءة. فيما يلي إعادة صياغة لشيفرة البرمجة الخاصة بك بطريقة أكثر إتقانًا:
javascriptif (!chineseName) {
showAlert("chineseName is not correct");
} else if (!IDN) {
showAlert("IDN is not correct");
} else if (!mobileNumber) {
showAlert("number is not correct");
} else if (hasAccount) {
if (!isAccountNumberValid) {
showAlert("account number is not correct");
} else {
check();
}
} else {
check();
}
function check() {
if (!check1 && !check2) {
showAlert("Please read NOTE and check the read box.");
} else {
showAlert("Everything is good to go!!");
}
}
function showAlert(message) {
alert(message);
}
في هذا التحسين، تم إنشاء دالة جديدة showAlert
لتقليل تكرار رمز الإنذار. تم استخدام المشغل اللوجيكي &&
بدلاً من &&
للتحقق من الشروط في دالة check
. يتم أيضًا استخدام هيكل التحكم else if
لجعل الشرط أكثر وضوحًا. يساعد ذلك في جعل الشيفرة البرمجية أكثر قراءة وفهماً.
مقالات ذات صلة
-
تطبيق بناء مصفوفة الجوار في C++17/03/2024
-
تحليل البيانات في Excel باستخدام VBA15/03/2024
المزيد من المعلومات
بالتأكيد، يُمكننا استكمال التحسينات على الشيفرة البرمجية لتحسين قابلية الصيانة وفهم الشيفرة. فيما يلي إضافة بعض التحسينات:
- تجنب استخدام التكرار:
يمكن دمج التنبيهات المكررة في دالةshowAlert
لتجنب تكرار الكود.
javascriptfunction showAlert(message) {
alert(message);
}
if (!chineseName) {
showAlert("chineseName is not correct");
} else if (!IDN) {
showAlert("IDN is not correct");
} else if (!mobileNumber) {
showAlert("number is not correct");
} else if (hasAccount && !isAccountNumberValid) {
showAlert("account number is not correct");
} else {
check();
}
function check() {
if (!check1 && !check2) {
showAlert("Please read NOTE and check the read box.");
} else {
showAlert("Everything is good to go!!");
}
}
- استخدام المتغيرات التوضيحية:
استخدام متغيرات توضيحية يسهل قراءة الشيفرة. على سبيل المثال:
javascriptconst isChineseNameValid = chineseName;
const isIDNValid = IDN;
const isMobileNumberValid = mobileNumber;
const hasValidAccount = hasAccount && isAccountNumberValid;
if (!isChineseNameValid) {
showAlert("chineseName is not correct");
} else if (!isIDNValid) {
showAlert("IDN is not correct");
} else if (!isMobileNumberValid) {
showAlert("number is not correct");
} else if (hasValidAccount) {
check();
} else {
check();
}
function check() {
if (!check1 && !check2) {
showAlert("Please read NOTE and check the read box.");
} else {
showAlert("Everything is good to go!!");
}
}
تمثل هذه التحسينات محاولة لتحسين قابلية صيانة الشيفرة وجعلها أكثر وضوحًا وقراءة.