بالطبع، يمكننا تحسين بنية الشيفرة البرمجية الخاصة بك لتجعلها أكثر فعالية وقراءة. فيما يلي إعادة صياغة لشيفرة البرمجة الخاصة بك بطريقة أكثر إتقانًا:
مقالات ذات صلة
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
لجعل الشرط أكثر وضوحًا. يساعد ذلك في جعل الشيفرة البرمجية أكثر قراءة وفهماً.
المزيد من المعلومات
بالتأكيد، يُمكننا استكمال التحسينات على الشيفرة البرمجية لتحسين قابلية الصيانة وفهم الشيفرة. فيما يلي إضافة بعض التحسينات:
- تجنب استخدام التكرار:
يمكن دمج التنبيهات المكررة في دالة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!!");
}
}
تمثل هذه التحسينات محاولة لتحسين قابلية صيانة الشيفرة وجعلها أكثر وضوحًا وقراءة.