Michael WyCliff Williams’ Post

When init isn't enough: The Python mistake that cost me 3 hours Found a junior dev hardcoding database connections inside every class method. Here's what we fixed: ❌ Before: Python class UserService:   def get_user(self, id):     db = connect_to_database() # 🔥 New connection every call     return db.query(f"SELECT * FROM users WHERE id={id}") ✅ After: Python class UserService:   def __init__(self, db_connection):     self.db = db_connection # ♻️ Reuse connection       def get_user(self, id):     return self.db.query("SELECT * FROM users WHERE id=?", [id]) Dependency injection isn't just fancy talk - it's how you avoid 500 database connections crashing prod at 3am. What OOP concept made you facepalm when it finally clicked? #Python #SoftwareEngineering #CodingBestPractices #TechEducation #Programming

It not always bad, sometimes you prefer many connections.

Like
Reply

Easy to blame the junior developer when no code review mechanism was put in place

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories