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

KI-gestützte Lerntools freischalten

Registriere dich, um leistungsstarke Tools zu nutzen, die dir helfen, schneller aus jedem Video zu lernen.

Szenen-Erklärer Phrasen-Jäger Karteikarten-Review Nachsprechübung Sprachausgabe
Kostenlos registrieren
Englisch 15:24 Science & Tech

I Hate Long If-Elif Chains: This Design Pattern Solved It Once and For All

Late Night with Seth Meyers · 352,262 Aufrufe · Hinzugefügt vor 2 Monaten

Untertitel (373 Segmente)

00:00

If your code looks like this, you're in

00:04

trouble. You wanted something clean and

00:07

extensible, a system where you could

00:08

just, you know, plug in a new exporter

00:11

or an algorithm, but it turned into a

00:14

hot mess of if else statements. Today,

00:18

I'll show you a simple design pattern

00:20

that solves exactly this problem, the

00:22

registry. It's easy to implement, and

00:25

once you know how it works, you want to

00:27

use this everywhere. Let's dive in. If

00:29

you want to learn more about how to

00:30

design a piece of software from scratch,

00:32

I have a free guide for you. You can get

00:34

it at iron.co/designguide.

00:36

This contains the seven steps I take

00:38

when I design new software. Hopefully,

00:41

it helps you avoid some of the mistakes

00:42

I made in the past. Iron.code/design

00:45

guide. Link is also in the video

00:47

description. Now, what is the registry

00:49

pattern? This pattern is a way to avoid

00:52

hard- coding logic like those if else

00:54

statements by storing that behavior in a

00:58

sort of central registry. In Python,

01:01

that can be a dictionary for example.

01:03

And then instead of calling functions

01:05

directly, you register them under a key

01:08

like a string or an enum and later call

01:11

them dynamically. It's like a sort of

01:14

named plug-in map, one that's easy to

01:16

extend without changing the core logic

01:19

of your application. If you look at this

01:21

example, we have an export data function

01:23

that picks the exporter based on the

01:26

format, which is a string. Now, you

01:28

could use an enum for that. I kept it to

01:30

a string also because that maps better

01:32

to the example that I'll show you after

01:33

this one. But the problem here is that

01:36

this is like this chain of if else

01:38

statements. Uh depending on the format,

01:40

you call a different function. And if

01:42

you want to add more exporters, you're

01:43

going to add more lines of code here.

01:45

This function is going to get really

01:46

long. It's not really a nice design,

01:50

right? And by the way, these exporter

01:52

functions in this particular example,

01:53

they don't really do anything. They just

01:55

print a message cuz it's not really

01:57

about exporting. It's more about how you

02:00

uh patch everything up. And then the

02:02

main function, I call export data with

02:04

various formats. Now, you could say,

02:06

hey, why don't just call export PDF and

02:09

export CSV? But that kind of defeats the

02:11

purpose because maybe you want to be

02:13

able to select the exporter in a

02:16

dropdown for example and then you want

02:18

to call that function directly. Now if

02:20

we run this example then this is what we

02:22

get. You see we get some export results.

02:25

So how do we change this so that here we

02:27

no longer have to deal with this if else

02:30

statement? Well, we're going to use the

02:32

registry. Like I mentioned in the

02:34

beginning, a registry is a sort of

02:36

central place where you keep track of

02:38

all of these different things. So let's

02:42

create something called exporters and

02:44

that's a dictionary of strings to export

02:47

functions. That's a type that I defined

02:49

here which is a collable that takes data

02:53

and doesn't return any results. It's

Vollständige Untertitel im Videoplayer verfügbar

Mit Übungen trainieren

Erstelle Vokabel-, Grammatik- und Verständnisübungen aus diesem Video

Vokabeln & Grammatik Verständnisquiz IELTS-Prüfung Schreib-Übung
Registrieren zum Üben
Noch keine Kommentare. Sei der Erste, der seine Gedanken teilt!

Registriere dich, um alle Features freizuschalten

Verfolge deinen Fortschritt, speichere Vokabeln und übe mit Übungen

Kostenlos Sprachen lernen