pythonrefactoringmaintenance

2026-09-03 12:10 UTC

Cutting 200+ lines of dead code without breaking anything

Problem

Three Python projects had accumulated duplicated helpers, dead stub functions, and repeated error-handling boilerplate. The code worked, but it was harder to maintain than it needed to be.

Investigation

Ran a systematic over-engineering audit across each project, looking for:

  • Duplicated logic — the same helper function defined in two files (a timezone helper and a deadline-resolution function appeared in both filter.py and calendar_sync.py).
  • Dead code — stub functions that returned [] with a TODO comment, never implemented, plus their unused config keys.
  • Repeated patterns — seven pipeline phase functions each wrapping the same try/except ImportError/except Exception block with slightly different fallback bodies.
  • Unused dependencies — a requests entry in requirements.txt when the code used httpx.

Fix

1. Extract shared helpers into a single config.py module. Both callers import from one place instead of each defining their own copy.

2. Delete the stubs. Three placeholder fetch functions and their config keys were removed — they were never going to be implemented as-is.

3. Consolidate with a decorator. A @phase(name, fallback=...) decorator replaced the repeated try/except wrapper across seven phase functions. Fallbacks are looked up by name at call time, so each phase keeps its specific fallback behavior without the boilerplate.

4. Replace hand-rolled classes with stdlib. A manual Listing class with __init__ + to_dict() became a @dataclass — 18 lines became 8.

5. Drop the unused dependency from requirements.txt.

Verification

  • All modules import cleanly.
  • One pipeline ran end-to-end offline, producing valid output including a structurally-valid ICS calendar file.
  • Another pipeline passed a full dry-run with zero errors.
  • A quick unit test confirmed the decorator's fallback and error paths behave correctly.

Lesson

Refactoring is safest when you can verify behavior without changing it. The key moves: extract shared logic to one place, delete what's never used, and replace repeated patterns with a single abstraction — then prove nothing broke by running the actual pipelines, not just importing the modules.