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.

Scene Explainer Phrase Hunter Flashcard Review Shadowing Practice Talk Back
Sign Up Free
English 23:23 Science & Tech

How to Refactor Insanely Complicated Business Logic (Step by Step)

Late Night with Seth Meyers · 117,219 views · Added 1 month ago

Subtitles (542 segments)

00:00

Take a look at this mess. This is a

00:02

function that approves an order for a

00:05

particular user. As you can see, there's

00:08

like a bunch of if else statements here,

00:11

uh, orders, currencies, different types.

00:17

In this video, I'm going to fix this

00:19

code step by step and show you how to

00:21

take impossibly complex logic like this

00:24

and turn this monster into Python code

00:27

that is actually clean and readable.

00:30

Before we start, if you want to learn

00:31

how to design software from scratch,

00:34

take a look at my free design guide at

00:36

r.code/design

00:38

guide. This takes you through the seven

00:40

steps that I always take whenever I

00:42

design new piece of code. /design guide

00:45

link is also in the video description.

00:48

Before I start working on this code, one

00:50

thing that's important to note is that

00:51

this type of code doesn't start out this

00:54

way. It grows this way. You know, you

00:56

start very simple where you just have

00:59

nonpremium users. So, approving an order

01:02

is pretty basic, but then you know, you

01:05

add premium users, you add discounts,

01:07

you add amount limits, and then there's

01:10

more and more rules and you don't want

01:12

to like refactor things. So you just add

01:15

an extra if statement. Just do it

01:16

quickly, right? And before you know it,

01:18

you have like 12 levels of indentation

01:20

and no idea what any of this means

01:22

anymore. And the result of that is that

01:24

you're going to have these confusing

01:26

checks and duplication and all sorts of

01:30

stuff that's really going to complicate

01:33

working on this code in the future. Now

01:35

the good news is that you can actually

01:37

refactor and clean up code like this. Uh

01:40

but you have to follow a couple of

01:42

specific steps that I'm going to show

01:44

you in this video. So first let's take a

01:46

look at the code in total. So uh apart

01:48

from this function, there's a couple of

01:49

data classes. We have an item which has

01:51

a name and a price. I have an order that

01:54

has an amount, discount as a region.

01:56

There's a currency. There's a type like

01:59

bulk, normal, and there's items in the

02:01

order which are these things basically.

02:04

And we have a user class which is uh

02:08

premium, admin, trial, and the user also

02:10

has a region. Then we have this horrible

02:13

approve order function. And then there's

02:16

a main function that creates a sample

02:18

user. And then it creates an order with

02:21

an amount and a couple of other things

02:23

as well. And then it checks whether the

02:25

order is approved or not.

02:29

So when I run this, we can see that the

02:31

order has been approved. So how do you

02:33

start? You could just randomly start

02:35

moving around code and seeing if that

02:38

improve things, but that's not really a

02:39

good way to do this. Now, boringly,

02:43

unfortunately, we're going to have to

02:44

spend some time understanding what is

02:47

actually happening before you can start

02:49

working on the code. And this is not

02:51

good news because we're going to have to

02:53

spend some time trying to drill down

02:55

what this logic actually does. But one

02:57

thing you can do to make this a bit

03:00

easier to also externalize kind of what

03:02

you learn is to write tests while you're

03:06

understanding the logic of the function.

03:08

Like for example, if we take a look

03:10

here, we see that there's this uh large

03:12

if statement about premium users and

03:16

then uh as you can see there is here an

03:18

else where if the user is an admin. What

03:21

you can basically grasp from this is

03:22

that if a user is not premium but it's

03:24

an admin then basically the order is

03:27

always going to be approved and all

03:29

other cases that's going to be rejected.

03:31

Also you can see that there's a

03:34

difference which makes a big difference

03:36

between order amount. So if it's less

03:37

than a th00and then if it's bulk and the

03:41

user is not trial then we do approve and

03:43

otherwise it's rejected. So you can get

03:45

a bit more clarity already by trying to

03:48

drill through this logic to understand

03:50

what is actually working and know it's

03:52

probably not what you want to hear but

03:53

if you want to work on code you have to

03:55

at least spend some time trying to

03:56

understand it and writing tests allows

03:59

you to specify what this code is

04:02

supposed to do. So these are not

04:04

correctness tests. These just lock in

04:06

what the current behavior is. So what I

04:08

did is already create a couple of these

04:10

tests. This is the test file where I

04:13

have a helper function to make an order,

04:15

make a user, and then we have the

04:17

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

Vocabulary & Grammar Comprehension Quiz IELTS Exam Writing Practice
Sign up to practice

Comments (0)

Login to Comment
No comments yet. Be the first to share your thoughts!

Sign up to unlock full features

Track progress, save vocabulary, and practice exercises

Start learning languages for free