The video owner has disabled playback on external websites.
This video is no longer available on YouTube.
This video cannot be played right now.
Watch on YouTube
Unlock AI-Powered Learning Tools
Sign up to access powerful tools that help you learn faster from every video.
How to Refactor Insanely Complicated Business Logic (Step by Step)
Subtitles (542 segments)
Take a look at this mess. This is a
function that approves an order for a
particular user. As you can see, there's
like a bunch of if else statements here,
uh, orders, currencies, different types.
In this video, I'm going to fix this
code step by step and show you how to
take impossibly complex logic like this
and turn this monster into Python code
that is actually clean and readable.
Before we start, if you want to learn
how to design software from scratch,
take a look at my free design guide at
r.code/design
guide. This takes you through the seven
steps that I always take whenever I
design new piece of code. /design guide
link is also in the video description.
Before I start working on this code, one
thing that's important to note is that
this type of code doesn't start out this
way. It grows this way. You know, you
start very simple where you just have
nonpremium users. So, approving an order
is pretty basic, but then you know, you
add premium users, you add discounts,
you add amount limits, and then there's
more and more rules and you don't want
to like refactor things. So you just add
an extra if statement. Just do it
quickly, right? And before you know it,
you have like 12 levels of indentation
and no idea what any of this means
anymore. And the result of that is that
you're going to have these confusing
checks and duplication and all sorts of
stuff that's really going to complicate
working on this code in the future. Now
the good news is that you can actually
refactor and clean up code like this. Uh
but you have to follow a couple of
specific steps that I'm going to show
you in this video. So first let's take a
look at the code in total. So uh apart
from this function, there's a couple of
data classes. We have an item which has
a name and a price. I have an order that
has an amount, discount as a region.
There's a currency. There's a type like
bulk, normal, and there's items in the
order which are these things basically.
And we have a user class which is uh
premium, admin, trial, and the user also
has a region. Then we have this horrible
approve order function. And then there's
a main function that creates a sample
user. And then it creates an order with
an amount and a couple of other things
as well. And then it checks whether the
order is approved or not.
So when I run this, we can see that the
order has been approved. So how do you
start? You could just randomly start
moving around code and seeing if that
improve things, but that's not really a
good way to do this. Now, boringly,
unfortunately, we're going to have to
spend some time understanding what is
actually happening before you can start
working on the code. And this is not
good news because we're going to have to
spend some time trying to drill down
what this logic actually does. But one
thing you can do to make this a bit
easier to also externalize kind of what
you learn is to write tests while you're
understanding the logic of the function.
Like for example, if we take a look
here, we see that there's this uh large
if statement about premium users and
then uh as you can see there is here an
else where if the user is an admin. What
you can basically grasp from this is
that if a user is not premium but it's
an admin then basically the order is
always going to be approved and all
other cases that's going to be rejected.
Also you can see that there's a
difference which makes a big difference
between order amount. So if it's less
than a th00and then if it's bulk and the
user is not trial then we do approve and
otherwise it's rejected. So you can get
a bit more clarity already by trying to
drill through this logic to understand
what is actually working and know it's
probably not what you want to hear but
if you want to work on code you have to
at least spend some time trying to
understand it and writing tests allows
you to specify what this code is
supposed to do. So these are not
correctness tests. These just lock in
what the current behavior is. So what I
did is already create a couple of these
tests. This is the test file where I
have a helper function to make an order,
make a user, and then we have the
various scenarios, right? So there is uh
Full subtitles available in the video player
Practice with Exercises
Generate vocabulary, grammar, and comprehension exercises from this video
Comments (0)
Login to CommentSign up to unlock full features
Track progress, save vocabulary, and practice exercises
Interactive Mode
Quiz
Correct answer:
Related Videos
Late Night with Seth Meyers
Quiz
Correct answer:
Quizzes appear as you watch the video
Memory Tip
From this video
Start learning languages for free