İçeriğe geç

Uzun metot (ARCH2180)

Anahtar python.quality.long-method · Önem info · Kategori quality

Çok uzun fonksiyonlar birden fazla sorumluluk taşır, okunması ve test edilmesi zorlaşır. Mantığı küçük, adlandırılmış yardımcı fonksiyonlara bölün.

def process_order(order):
total = 0
for item in order.items:
total += item.price * item.qty
discount = total * 0.1 if total > 100 else 0
total -= discount
tax = total * 0.18
total += tax
send_email(order.user, total)
log.info("processed")
return total
def process_order(order):
total = _subtotal(order) - _discount(order)
total += total * 0.18
_notify(order.user, total)
return total
def _subtotal(order):
return sum(i.price * i.qty for i in order.items)

Fonksiyonu mantıksal adımlara göre tek sorumluluklu küçük yardımcı fonksiyonlara ayırın.