DEV Community

Cover image for 🧩 Design Principles of Software: Building Better Code with Python

🧩 Design Principles of Software: Building Better Code with Python

πŸš€ TL;DR

This article explains key software design principles (SOLID + DRY +
KISS)
with a real-world Python example.\
You'll also find step-by-step code, a GitHub repo with automation, and a
short guide to present this as a 5-minute video. πŸŽ₯


πŸ“Œ Outline

  1. Why software design principles matter πŸ€”\
  2. Key principles (SOLID, DRY, KISS, YAGNI) πŸ“š\
  3. Python example: Order Processing System 🐍\
  4. Refactoring with design principles applied ✨\
  5. GitHub repo structure & CI/CD automation βš™οΈ

1️⃣ Why software design principles matter πŸ€”

  • πŸ”§ Maintainability β†’ Easier to update and fix bugs.\
  • ♻️ Reusability β†’ Write once, use multiple times.\
  • πŸ“ˆ Scalability β†’ Design that grows with your app.\
  • πŸ‘©β€πŸ’» Team collaboration β†’ Principles help everyone understand and extend code.

2️⃣ Core Design Principles πŸ“š

  • SOLID β†’ Five core OOP principles:
    • S: Single Responsibility\
    • O: Open/Closed\
    • L: Liskov Substitution\
    • I: Interface Segregation\
    • D: Dependency Inversion
  • DRY β†’ Don't Repeat Yourself\
  • KISS β†’ Keep It Simple, Stupid\
  • YAGNI β†’ You Ain't Gonna Need It

3️⃣ Python Example: Order Processing System 🐍

❌ Bad code (violates SRP, DRY):

class Order: def __init__(self, items): self.items = items def calculate_total(self): return sum(item['price'] for item in self.items) def save_to_db(self): print("Saving order to database...") def send_email(self): print("Sending email to customer...") 
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Problem: One class is doing everything (violates SRP).


βœ… Refactored code with design principles:

class Order: def __init__(self, items): self.items = items def calculate_total(self): return sum(item['price'] for item in self.items) class OrderRepository: def save(self, order: Order): print(f"Saving order with total {order.calculate_total()} to database...") class EmailService: def send_order_confirmation(self, order: Order): print(f"Email sent for order total: {order.calculate_total()}") # Usage items = [{"name": "Pizza", "price": 12}, {"name": "Cola", "price": 3}] order = Order(items) repo = OrderRepository() repo.save(order) email = EmailService() email.send_order_confirmation(order) 
Enter fullscreen mode Exit fullscreen mode

βœ… SRP: Each class has a single responsibility.\
βœ… DRY: No duplicate calculation logic.\
βœ… KISS: Simple methods and classes.\
βœ… Extensible: Swap EmailService with SMS later (Open/Closed
Principle).


4️⃣ Repo Structure & Automation βš™οΈ

software-design-principles/ β”œβ”€ order.py β”œβ”€ tests/ β”‚ └─ test_order.py β”œβ”€ requirements.txt β”œβ”€ README.md └─ .github/ └─ workflows/ └─ ci.yml 
Enter fullscreen mode Exit fullscreen mode

requirements.txt

pytest==8.3.3 
Enter fullscreen mode Exit fullscreen mode

ci.yml (GitHub Actions)

name: Python CI on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-python@v4 with: python-version: '3.11' - run: pip install -r requirements.txt - run: pytest 
Enter fullscreen mode Exit fullscreen mode

πŸš€Repository link
πŸ”‘Repository to Practice

Top comments (0)