Tired of if-elif chains? Here’s a cleaner way to write your code | Issue #75


Hi there,

When I see an if-statement with multiple elifs in my code, it almost always triggers an investigation. Is there some way to rewrite this to make the logic simpler? Is there a chance that in the future I'll need to add even more complexity? If so, I'll try to redesign the code so that I don't need that chain of if-elif statements.

In this week’s video, I cover a design pattern that helped me eliminate many of those chains from a project + at the same time made the code easier to test, extend, and read.

It’s called the Registry Pattern. The basic version of it is simple to implement, and I show a quick example of that in the video, using exporters.

But the cool thing is that there's another level to this pattern that leads to incredible flexibility. I've used the pattern in this way in many of my previous projects and it always delivers.

This is the next video in my brand new Design Patterns series. There's a lot more where this is coming from - I'm really excited about the next batch of videos coming to you soon.

Hope you enjoy this video and wish you an awesome weekend!

Cheers,

Arjan


Do you enjoy my content on YouTube and would you like to dive in deeper?

🚀 Check out my online courses

My courses have helped thousands of developers take the next step in their careers. Check out these courses to help grow your skills and become a senior developer:

👕 Clean code and clean clothes

The ArjanCodes merch store features T-shirts, hoodies, hats, and more for the clean-code-obsessed. Careful though: you'll look dangerously professional while reviewing PRs.

👉 Check out the store here and grab something before your stand-up call starts.

🔧 Need help with a complex codebase?

If your team is struggling with tech debt, unclear architecture, or cloud costs that keep creeping up, my team and I can help. We don’t just review, we get in and fix things. We’re only working with a few companies at a time to stay focused, so if it sounds like a fit:

Unsubscribe | Send by ArjanCodes

Wolvenplein 25, Utrecht, UT 3512 CK

The Friday Loop

Every Friday, you'll get a recap of the most important and exciting Python and coding news. The Friday Loop also keeps everyone posted on new ArjanCodes courses and any limited offers coming up.

Read more from The Friday Loop

Have you ever written a Python script that felt slow, even though the logic was simple? Sometimes it’s not the efficiency of your code that's the problem, it’s when your code does its work. This week’s video is all about the Lazy Loading design pattern, and I think many developers underestimate how powerful (and practical) it is. I start with a real example: loading a huge CSV file that freezes your program for 10 seconds before doing anything useful. Then, step by step, we improve it using:...

If your code is hard to test, hard to reuse, and hard to change, you’re probably hardcoding your dependencies. It’s one of the most common architectural problems I see in Python code, even in production systems. In this week's video, I take a small but realistic example, a data pipeline that loads, transforms, and exports some records, and show you how Dependency Injection can turn it from a rigid mess into something clean, testable, and modular. No frameworks required. You’ll learn: Why...

Hi there, Most apps only store the final result: the current balance, the current inventory, the current status. But what if you could track every change that led to that result? That’s exactly what the event sourcing pattern allows you to do. Instead of overwriting state, you store a sequence of immutable events and derive the current state by replaying them. It’s like Git (kinda), but for your domain logic. In this week’s video, I show you how to implement event sourcing in Python from...