أفضل ممارسات البرمجة ومراجعة الكود الفعّالة
عرض العرض التقديميالمعيار الذهبي. عشر فئات لتقييم كل طلب سحب تراجعه.
x، tmp، data، calc، process؛ منطق متداخل بعمق؛ دوال كبيرة غير واضحة> 0؛ لا اختبارات لمدخلات غير صالحة؛ لا اختبارات للمنطق المتغيرNone بدون توضيح؛ استثناءات مبتلعة؛ أنماط فشل مخفيةصياغات احترافية لسيناريوهات المراجعة الشائعة. استخدمها كنقاط انطلاق.
طلب سحب به أخطاء متعمدة للتدريب. هل يمكنك اكتشاف المشكلات؟
def calc(items, user, d=False):
t = 0
for i in items:
if i["type"] == "book":
t += i["price"] * 0.9
else:
t += i["price"]
if d:
t = t - t * 0.1
if user is not None:
if "is_premium" in user:
if user["is_premium"] == True:
t = t - t * 0.05
return t
def process_cart(items, user):
total = calc(items, user, True)
print("cart processed")
return totalfrom src.cart import calc
def test_calc():
items = [{"type": "book", "price": 100}]
user = {"is_premium": True}
assert calc(items, user, True) > 0# Cart Project
cart logiccalc عام جداً - يجب أن يكون calculate_cart_totald غير واضح - يجب أن يكون discount_enabledt وi غير وصفية0.9، 0.1، 0.05 يجب أن تكون ثوابت مسماةif متداخلة بعمق لفحص العضوية المميزة - استخدم .get()process_cart له أثر جانبي (print) مختلط مع منطق الأعمالd=False يُمرر موضعياً كـ True وهو مربك> 0 لا يتحقق من الصحةprocess_cartBOOK_DISCOUNT_RATE = 0.10
GENERAL_DISCOUNT_RATE = 0.10
PREMIUM_DISCOUNT_RATE = 0.05
def calculate_item_price(item):
item_type = item["type"]
price = item["price"]
if item_type == "book":
return price * (1 - BOOK_DISCOUNT_RATE)
return price
def apply_general_discount(total_price, discount_enabled):
if not discount_enabled:
return total_price
return total_price * (1 - GENERAL_DISCOUNT_RATE)
def apply_premium_discount(total_price, user):
if not user or not user.get("is_premium", False):
return total_price
return total_price * (1 - PREMIUM_DISCOUNT_RATE)
def calculate_cart_total(items, user, discount_enabled=False):
total_price = 0
for item in items:
total_price += calculate_item_price(item)
total_price = apply_general_discount(total_price, discount_enabled)
total_price = apply_premium_discount(total_price, user)
return total_price
def process_cart(items, user):
total_price = calculate_cart_total(items, user, discount_enabled=True)
return total_priceواجب مراجعة طلب السحب: راجع كمهندس محترف
يجب أن يتضمن تقديمك:
File: src/orders.py Line/Area: calc function Comment: The function name is too generic. Could we rename it to calculate_order_total for clarity? File: tests/test_orders.py Line/Area: test_calc Comment: This test only checks that the result is greater than zero. Could we assert the exact expected value instead?
راجع الملفات التالية:
def calc(items, vip=False):
total = 0
for i in items:
if i["type"] == "book":
total += i["price"] * 0.9
elif i["type"] == "electronics":
total += i["price"]
else:
total += i["price"]
if vip == True:
total = total - total * 0.05
return total
def checkout(items, user):
total = calc(items, user["vip"])
print("checking out order...")
print("total is", total)
return {"ok": True, "total": total}def f(x):
if x == None:
return False
return Truefrom src.orders import calc
def test_calc():
items = [{"type": "book", "price": 100}]
assert calc(items, False) > 0# Orders
Run it with python.المهندسون العظماء لا يجعلون الكود يعمل فحسب. بل يجعلونه مفهوماً وقابلاً للاختبار والمراجعة وجديراً بالثقة.