Duplication vs. Coupling: When do you actually refactor duplicate code in Python?

2026-10-07来源 dev.to ProgrammingEnglish

When building and maintaining larger Python codebases, one of the classic debates between senior engineers is balancing the DRY (Don't Repeat Yourself) principle against premature coupling.

Consider a situation where two or three separate modules have identical 10-line boilerplate (for example, database connection lifecycle handling, recurring input validation checks, or retry logic):

 python # Seen across module_a.py and module_b.py: managed: bool = conn is None if managed: conn = init_db(DB_PATH) try: yield conn finally: if managed: conn.close() 

原文:https://www.a8q9.cn/en/article/duplication-vs-coupling-when-do-you-actually-refactor-duplicate-code-in-python-119101.html

相关文章