Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Member-only story

Refactor Long Method with a Lightweight DAG

Tame complex workflows and simplify testing with the Command design pattern

--

Press enter or click to view image in full size
My girl

Not a member? You can still check out this article through here.

Reviewing some legacy code lately has given me a headache.

The main issue is one massive function crammed with every piece of logic spread out flat inside it. I have to say, this is what I consider the worst kind of “code smell.”

You might ask, “But this function works fine, so what’s wrong with it?”

If it were a static function with no further iterations needed, that’d be fine. But if this function still requires iterations, or worse, isn’t even stable yet with a bunch of process bugs still needing fixing, then it’s absolutely terrible.

Modifying the flow of a massive function is challenging, whether adding conditional checks or introducing new steps. First, developers must understand every existing process to grasp the context and make changes.

After spending tremendous effort to understand the function and complete the modification, the next problem is how to test it.

If it’s a massive function, I believe its unit tests are likely filled with various mocks. Once part of the logic is modified, the…

--

--

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Chunting Wu
Chunting Wu

Written by Chunting Wu

Architect at Netskope. Experienced in system design, backend development, and data engineering.