The article criticizes a Python function for its excessive use of boolean flags (flag1
, flag2
, flag3
) to control its behavior. This makes the function's logic hard to follow and understand, particularly when determining which flag combinations produce specific results.
The core issue is the abundance of flags. Each flag controls a separate data processing step (digit checking, uppercase conversion, sorting). The author demonstrates how calling the function with different flag combinations (e.g., process_data(my_data, True, False, True)
) becomes difficult to interpret without careful examination.
While not explicitly provided, the article implies that a better approach would involve refactoring the function to handle these data processing steps in a clearer manner. This could involve using different function parameters, function overloading, or conditional logic, with better naming and clarity.
Last week, I was reviewing someone else’s code. I just stumbled upon a Python function that made me feel whether I must cry or laugh.
Then I decided to write about that incident…so that you didn’t make the same mistake.
Not a medium member, yet? Read it FREE here.
This was the code that just made me feel like this:
def process_data(data, flag1, flag2, flag3): if flag1: data = [item for item in data if item.isdigit()] if flag2: data = [item.upper() for item in data] if flag3: data = sorted(data) result = "Processed: " + ", ".join(data) return result
This code doesn’t seem that much bad at first glance. But let me tell you why I am saying this to be terrible:
At first glance, it doesn’t seem too bad, right? But let’s understand what’s wrong with this code.
It’s using too many flags (flag1
, flag2
, flag3
) to control behavior, which is making the function a little bit confusing.
In this, every time we are calling a function, we have to figure out which combination of flags is producing the desired result.
Let me explain this with the help of this example
res = process_data(my_data, True, False, True)
Did you understand what does True, False, True
even means? This is the same case with the above function.
Skip the extension — just come straight here.
We’ve built a fast, permanent tool you can bookmark and use anytime.
Go To Paywall Unblock Tool