ምርጥ የኮዲንግ ልምዶች እና ውጤታማ የኮድ ግምገማ
ገለፃውን ይመልከቱወርቃማው መስፈርት። የሚገመግሙትን እያንዳንዱን pull request ለመገምገም አስር ምድቦች።
x፣ tmp፣ data፣ calc፣ process ያሉ ስሞች፤ በጥልቀት የተደረደረ ሎጂክ፤ ትልልቅ ግልፅ ያልሆኑ functions> 0 ያሉ ደካማ assertions፤ ልክ ያልሆነ input ጉዳዮች የሉም፤ ለተቀየረ ሎጂክ ሙከራ የለምNone መመለስ፤ የተዋጡ exceptions፤ የተደበቁ የውድቀት ሁነቶችለተለመዱ የግምገማ ሁኔታዎች ሙያዊ አገላለፆች። እነዚህን እንደ መነሻ ይጠቀሙ።
ለልምምድ ሆን ተብሎ የተበላሸ PR። ችግሮቹን ማግኘት ይችላሉ?
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_total መሆን አለበትd ግልፅ አይደለም - discount_enabled መሆን አለበትt እና i ገላጭ አይደሉም0.9፣ 0.1፣ 0.05 የተሰየሙ constants መሆን አለባቸውif blocks - .get() ይጠቀሙprocess_cart ከንግድ ሎጂክ ጋር የተቀላቀለ side effect (print) አለውd=False positionally እንደ True ማስተላለፍ ግራ የሚያጋባ ነው> 0 ትክክለኛነትን አያረጋግጥምprocess_cartን አይሞክርምBOOK_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የPR ግምገማ ምደባ: እንደ ሙያዊ ኢንጂነር ይገምግሙ
ማቅረቢያዎ የሚከተሉትን ማካተት አለበት:
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.ታላላቅ ኢንጂነሮች ኮድ እንዲሰራ ብቻ አያደርጉም። ኮዱ ሊረዳ፣ ሊሞከር፣ ሊገመገም እና ሊታመን የሚችል ያደርጋሉ።