2 min read

Reduce Python Code Complexity With This Simple Trick

Python
Share:
Reduce Python Code Complexity With This Simple Trick
Ran Isenberg
Written by

Ran Isenberg

Builder

AWS Serverless Hero & Principal Cloud Architect at Palo Alto Networks

Passionate about AI, Serverless, Platform Engineering and helping organizations build reliable & scalable systems on AWS.

As engineers, we should always strive to write simple code.

One common pitfall which is typical to many programming languages and not only to Python is the horrible if-elif misusage.

If you want to tackle this issue and to improve your code readability and maintainability, stick around, this article is for you!

Request Handler Use Case

A customer management system receives requests.

Request action type

Each request contains an action and a customer name. Actions consist of creating a new customer, activating a customer, suspending a customer, and deleting a customer.

There are four types, and each type is handled differently.

Each request is a Python dictionary:

Here is a potential flow diagram that tackles this use case:

Image 3

And the equivalent Python code:

Can you spot the issue? There are many if and elifs

that make the code more complex, less readable (too long!), and harder to maintain.

In addition, in order to handle the actions, the code sometimes has to go through several ‘ifs’ that won’t match, which is not ideal as the number of actions gets bigger (complexity of O(n) where n is the number of actions).

Thankfully, there is a straightforward solution!

Say no to If-elif!

As you can see in the code below, the if-elif section is removed in favor of a dictionary that maps each action to the corresponding handler function — line 3, ACTION\_MAPPING.

Now, any action handler is selected immediately at line 18 from the dictionary (at average order of O(1)), and the handler is called at line 20.

For example, for an ‘activate’ action, the \_handle\_activate\_request will be selected.

Side note: since the input is validated at line 13, the action is always found in the ACTION\_MAPPING dictionary.

What is the Best Practice?

If-elif isn’t evil; they are an essential part of the language.

However, like with everything else in life, you shouldn’t overdo it.

My rule of thumb is that one if-elif is enough. If you require more, use the dictionary mapping mechanism.

And lastly, use a code complexity scanner tool such as Radon/Xenon. They can pinpoint problematic areas and help you refactor your code into a masterpiece.


Need help with AWS Serverless or Platform Engineering?

Book a consultation to discuss your architecture challenges.

Book Consultation

Share this article

How to Build Open Source Software Developers Actually Want to Use
Jan 8, 20269 min read

How to Build Open Source Software Developers Actually Want to Use

Building a successful open-source project goes beyond sharing code — it requires production-grade quality, clear documentation, strong DevEx, and active community engagement. In this guide, learn how to build secure, maintainable, and high-impact open-source projects using GitHub best practices, CI/CD pipelines, contribution templates, and real-world automation. Ideal for both companies and individual developers.

Secrets Manager vs. Parameter Store: Which One Should You Really Use?
Sep 8, 20255 min read

Secrets Manager vs. Parameter Store: Which One Should You Really Use?

Choosing between AWS Secrets Manager and SSM Parameter Store isn’t always straightforward. This guide breaks down the key differences in cost, secret rotation, versioning, encryption, IaC support, and more—helping you decide which service fits your use case. Whether you're managing sensitive credentials or sharing configuration across environments, you'll walk away with a clear, practical recommendation backed by real-world experience.

Serverless MCP on AWS: Lambda vs. Fargate for Agentic AI Workloads
Jul 16, 202516 min read

Serverless MCP on AWS: Lambda vs. Fargate for Agentic AI Workloads

Building agentic AI requires more than prompts—it needs secure, structured access to your systems. This post explores three ways to deploy a production-grade MCP server on AWS: Lambda with Web Adapter, Pure Lambda, and AWS Fargate. Compare performance, cost, observability, and DevEx tradeoffs, and access ready-to-use CDK templates. Ideal for teams bringing agentic AI into real-world applications on AWS.