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.
This Design Pattern Scares Me To Death
AI Summary
This video provides a deep dive into the **Specification Pattern**, a design technique from Domain-Driven Design used to encapsulate complex business rules into reusable, composable blocks. While the instructor warns that this approach can lead to overengineering, they demonstrate how to move beyond basic, tangled `if` statements. You will learn to use Python’s functional features—such as decorators, dunder methods, and generics—to create a flexible "predicate" framework. By the end, you will understand how to separate the definition of business logic from its execution, allowing you to treat complex rules as manageable, modular data components.
Learning Stats
CEFR Level
Total Words
Unique Words
Difficulty
Subtitles (688 segments)
This is a video about the specification
design pattern with a big warning. It's
extreme overengineering. I wasn't even
sure whether I should actually post this
video. The reason I'm doing it is
because, well, there are a couple of
really interesting design ideas that I
just don't want to keep from you. But
frankly, when I look at the final
version of the code, it scares the
out of me. It's love craftsion. Is that
a word? That should be a word. When you
first look at it, you go like, but then
it's also kind of interesting and then
it sucks you in and then bam, you're
done. We're going deep today. Higher
order functions, decorators, thunder
method, overrides, generics, the whole
shebang. It's going to be insane, but
really good. This video is sponsored by
SER API. More about them later. Now,
what is the specification? But let me
first show you a code example. I have a
user class with a bunch of different
settings. And there's a function that
checks whether this user can access some
API. As you can see, the logic is kind
of convoluted. There is an age, whether
the user is active, is it banned, what
country the user is in. Now, you can
maybe refactor this a bit to turn a lot
of these things into guard clauses,
right? But it's still kind of
complicated business logic. And you can
imagine that this is maybe something
that regularly changes, maybe because
the company changes uh the policy or
something like that. And perhaps you
have very similar kind of business logic
in other places as well like can you
access some kind of report which you
know looks kind of like it but is
slightly different or maybe you have
another function that checks whether you
can access the CLI which again is
slightly different from the first
version. Now maybe this is intended
maybe not who knows. The problem with
this type of business logic is that well
here this example is of course very
basic but this would normally be spread
out over your entire codebase and mixed
with other types of business logic and
whenever you want those rules to change
it means you have to update this in many
different places and you don't have a
clear overview of what those rules
actually are so today I'm going to show
you the specification pattern which
fixes this by turning these complicated
if conditions into reusable building
blocks and ultimately this will let you
turn all of this code into data and I'll
show you an example of how that works at
the end of this video. Now this
specification pattern doesn't come from
the gang of four book it's from domain
driven design and at the core the idea
is to encapsulate business rules into
small specifications and then you can
combine these specifications using
boolean logic and then you can reuse
that everywhere. Now if you have a
classical object-oriented language, you
would then basically implement this with
lots of small classes and each class
represents one of these business rules.
You can find a bit more information
about this pattern on Wikipedia. Uh so
you can see there is like a UML diagram
where we have various types of
specifications.
Uh there are some code example which is
extremely abstract. So, and when I read
this the first time, I had a hard time
understanding why you would actually
need this because what's the advantage,
right? You can just write the same logic
in an if condition. So, why would you
have all of these classes to wrap around
that? So, that's what I'm trying to
address in today's video. There's even a
Python example in Wikipedia that adds
all of these classes without really
showing why you would want to do that.
The only actual example that's given on
the Wikipedia page is in my opinion
something pretty basic that you can also
just capture in a regular if condition.
So in this video I hope I can take this
a few steps further. Now the goal that
we have is that we want to have rules
that are reusable and composable. And
obviously like I already said the
example here is pretty basic. uh but I
do think in Python we can do much better
than this objectoriented example from
Wikipedia. In Python we can use
functions in much better way. So we can
build a small functional framework for
these rules and we can rely on other
features like uh decorators and generics
and cool stuff like that that we have
access to in Python. So the first step
that I want to take is to move these
conditions out of this if statement into
a sort of uh let's call that a
predicate. Basically a predicate is a
function that returns true or false and
then we can later on combine these
functions so that we get more
complicated logic. So for example here
you see a bit of code that could be
turned into a predicate. So that would
be then a simple function called let's
say is admin.
That's a user.
It returns a boolean value and this
returns whether the user is an admin. So
that's a function returning true or
false. That could be a predicate. That
could be a rule. And we can do this for
other types of rules as well. For
example, is active
like so. or this check whether the
account is over 30 days.
So these are some basic example of rules
that we can pull out of these functions.
Full subtitles available in the video player
Key Vocabulary (49)
Used to refer to the person or people that the speaker is addressing. It is the second-person pronoun used for both singular and plural subjects and objects.
To make someone or something different or to become different. It can also mean to stop using one thing and start using another, such as putting on different clothes.
To come or go back to a place after being away. It also means to give, send, or put something back to its original owner or location.
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
REVERSE ENGINEERING: LASER PRINTER TONER CHIP
#TheAndroidShow in 60 seconds: agents in Android Studio, the first Android XR device and more!
Why Humans Only Need To Vaccinate A Few Bats
Learning Python Feels Easy. Until It Isn’t.
Now in Android: 118 –What’s new in Android development at Google I/O 2025 (part 2)
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