Problem Statement
When software grows, code often becomes tangled and hard to change. Without clear guidelines, developers add features in ways that cause bugs, slow down development, and make the system fragile.
┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │ Untidy Code │──────▶│ Apply Principles│──────▶│ Clean Code │ │ (Spaghetti) │ │ (SOLID, DRY, │ │ (Modular, │ │ │ │ KISS, etc.) │ │ Maintainable)│ └───────────────┘ └───────────────┘ └───────────────┘
This diagram shows how messy code becomes clean and maintainable by applying additional design principles.
### Before applying principles (no clear structure, duplication) class Order: def __init__(self, items): self.items = items def total_price(self): total = 0 for item in self.items: if item['type'] == 'book': total += item['price'] * 0.9 # 10% discount else: total += item['price'] return total def print_order(self): for item in self.items: print(f"{item['name']}: ${item['price']}") ### After applying SOLID and DRY principles from abc import ABC, abstractmethod class Item(ABC): def __init__(self, name, price): self.name = name self.price = price @abstractmethod def get_price(self): pass class Book(Item): def get_price(self): return self.price * 0.9 # 10% discount class OtherItem(Item): def get_price(self): return self.price class Order: def __init__(self, items): self.items = items def total_price(self): return sum(item.get_price() for item in self.items) def print_order(self): for item in self.items: print(f"{item.name}: ${item.get_price():.2f}")