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

Desbloqueie ferramentas de aprendizado com IA

Cadastre-se para acessar ferramentas poderosas que ajudam a aprender mais rápido com cada vídeo.

Explicador de cena Caça-frases Revisão com flashcards Prática de repetição Falar de Volta
Cadastrar grátis
Inglês 17:36 Science & Tech

Stop Building Ugly APIs: Use the Fluent Interface Pattern

Late Night with Seth Meyers · 34,057 visualizações · Adicionado há 1 mês

Legendas (410 segmentos)

00:00

Let me show you two ways to describe the

00:02

same animation. So here's the first

00:05

version. So this animation consists of a

00:07

number of steps. We rotate, we move, we

00:10

scale, we fade, we move again, and we

00:12

fade again. And then there's durations

00:14

for each of these steps. Second version

00:18

looks like this. So it's the same

00:20

behavior, but it has a completely

00:23

different feel. And the second one is

00:25

using a pattern called fluid interface.

00:28

In this video, we're going to start with

00:30

a small animation engine and make it

00:32

cleaner step by step and refactor it

00:35

into a fluent API that feels like you're

00:38

telling a story in Python. Now, if you

00:41

want to learn more about how to design a

00:43

piece of software from scratch, I have a

00:44

free guide for you. You can get this at

00:46

iron./design guide. This contains seven

00:49

steps I take when I design new software.

00:51

Hopefully, it helps you avoid some of

00:53

the mistakes I made in the past.

00:54

Iron/design guide. The link is also in

00:57

the video description. Now, what is a

00:59

fluid interface? A fluid interface is a

01:02

style where methods return self. So, you

01:06

can chain them. For example, in Python,

01:08

that means you could type something like

01:10

this,

01:18

etc., etc., etc. Now, of course, it's

01:20

not a valid code. I'm simply typing it

01:22

here, but that's the idea. And the main

01:25

goal for this type of interface is that

01:28

it's more readable and that it's more

01:29

like a flow. So it's not really about

01:32

performance or having a clever design or

01:34

something like that. You typically use

01:36

this interface to write code that

01:38

describes a sequence of things to do in

01:41

an order that they should happen. And

01:43

even if you've never heard of the name

01:45

fluid interface, this is probably a

01:47

pattern that you've seen before. If you

01:50

use pandas for example, uh it uses the

01:52

fluent interface on data frames or in

01:56

OMS like SQL alchemy, you use it to

01:59

construct SQL queries. And finally,

02:02

pipeline tools are also a really good

02:04

candidate for fluid interfaces. And

02:07

these APIs, they feel comfortable

02:10

because they match how we think about

02:12

transformations. And that's why I picked

02:14

animation as a domain to show you how

02:16

this pattern actually works. So the way

02:18

I set this up is that this is a simple

02:21

example application that animates shapes

02:23

on a TK inter canvas. So this is my main

02:27

file. So the first step is some basic TK

02:30

setup like creating a canvas. Uh then I

02:32

create a graphics render. That's

02:34

something that I built myself. I include

02:37

in the code example. Then I define an

02:40

animation for a square.

02:42

I create a shape of that square where I

02:46

pass the animation. So animations are

02:47

stored alongside shapes. I do the same

02:51

thing for a triangle. So it has some

02:52

steps and durations. I then create the

02:55

shape of the triangle. Uh then I have

02:58

these two shapes. And then whenever I

03:00

click a button or touch a key, it will

03:02

start the animation by playing a

03:04

particular scene. So that gets the

03:06

renderer and these shapes. When I run

03:08

the script, it shows the canvas. And I

03:10

can click on this and then we have these

03:12

two animated shapes that I just created.

03:15

So like I said, I already created a

03:17

simple graphics engine and animation

03:20

system for this. The graphic engine is

03:22

very basic. It has a convos and it has a

Legendas completas disponíveis no player de vídeo

Pratique com exercícios

Gere exercícios de vocabulário, gramática e compreensão deste vídeo

Vocabulário e gramática Quiz de compreensão Exame IELTS Prática de escrita
Cadastre-se pra praticar
Nenhum comentário ainda. Seja o primeiro a compartilhar suas ideias!

Cadastre-se para desbloquear todos os recursos

Acompanhe seu progresso, salve vocabulário e pratique com exercícios

Aprenda idiomas de grátis