text stringlengths 0 2.46k |
|---|
**Jerod Santo:** Right. |
**Yehonathan Sharvit:** So the first thing that we do, we separate. Data can live on its own, and code can live on its own. Like in functional programming. |
**Jerod Santo:** Okay. |
**Yehonathan Sharvit:** So the first step of data-oriented programming is exactly the same as functional programming. The second step is that instead of using specific structure to represent our data, we prefer to use generic data structures, like HashMaps, like we have in Ruby, and in JavaScript, of dictionaries in Py... |
\[10:19\] And the number three, which is similar to functional programming, is that we never mutate data. We use immutable data structures. And there are very, very advanced - or very performant, sorry - immutable data structures for generic data structures. So in all languages, we have super-efficient immutable HashMa... |
And principle number four is - okay, if you don't have types for your data, how do you prevent, how do you avoid the big mess that you will be into? If all the pieces of data that you're manipulating in your program are HashMaps, how do you know if in the HashMaps you expect a field that is called email, and user, and ... |
**Jerod Santo:** So these four principles of data-oriented programming. The first one separating the code from the data; the second one, representing data with generic data structures, like maps and lists. The third one treating data as immutable. And the fourth one, separating the schema from the data representation. ... |
So this first one, separating code from data... As you said, this is kind of like against traditional object-oriented programming, which is kind of defined objects as code plus data coexisting in the same entity. Data-oriented programming says separate those two. And so the question to that which comes to my mind is li... |
**Yehonathan Sharvit:** Because almost every developer that has worked in a production-ready object-oriented system has suffered from huge class hierarchies, and you inherit from something that inherited from something, and when you want to make a little change, you influence so many things that it's a nightmare of com... |
**Jerod Santo:** \[14:05\] I see. So if I have an object which is a person, and inside of that object there's the data of the person's first name and last name, and there's the code that says, "Here's how I represent that as their full name", I've implemented that inside of this little object, and it's stuck inside of ... |
**Yehonathan Sharvit:** Yeah. |
**Jerod Santo:** Okay. |
**Yehonathan Sharvit:** And I think object-oriented is fine where the data that you encapsulate in the object is not information. We have different kinds of data. Sometimes we have data, for example, the internals of the data structure. The left child and the right child, and the number of children, and is visited, and... |
**Jerod Santo:** Okay, so there's kind of like internal data and external data, is kinda what you're saying, but you're saying one's information and one's not. And it's okay to encapsulate internal things, because they are uninteresting to the outside world. But if you encapsulate things that are eventually interesting... |
Okay, so there's the why, for principle one. What about principle two? Representing data with generic data structures. Why use a map, or a list - those are kind of the two main ones, right? List of things, and then like dictionaries of things, or maps, or hashes, or whatever your language calls them... When you could m... |
**Yehonathan Sharvit:** Okay, that's the toughest question, and that's the question that comes up again and again; that's the strongest critique against Clojure and data-oriented programming... But also, that's the most interesting one. So let's say that we have a way to do data validation. We will talk about it when w... |
\[17:50\] First of all, we lose the ability to refer to fields by their name, at runtime. Because a struct, when it's compiled, it becomes just an array, and the field names become offsets inside the array... Meaning that, for example, it's very hard to be dynamic and to receive, let's say from the user, the name of th... |
**Jerod Santo:** I think so. But you said absent reflection, and lots of languages have reflection abilities... So you can get at the names of things pretty reliably, right? |
**Yehonathan Sharvit:** Yeah. But I think that if you write a program and you rely too much on reflection, you will be rejected by a code reviewer. They will say, "Hey, what are you doing here?" And in a sense, if you use reflection -- and anyway, when you use reflection, you bypass the type checker. So you do data-ori... |
Let me give you an example. Let's say you fetch from the database information about a book, with the title and number of pages, and you want to rename a field. Because in your API, "title" should be called "the title", and "number of pages" should be called "pages". In object-oriented programming, what you need to do i... |
**Jerod Santo:** Okay. So from the static typing side though, aren't you throwing away a lot of upside? You're throwing away a lot of tooling, inference, you're throwing away a lot of refactoring abilities... I know you said set validations aside for this part of it, but obviously, that does play a role in decision-mak... |
**Yehonathan Sharvit:** Yeah, I can hear in the way you ask the question. You just pretend that you ask the question. |
**Jerod Santo:** Well, I'm representing what a lot of people think. I work in small teams, small code bases; I don't have a lot of the problems that static type solves, personally. I've seen them and I've heard them from a lot of people, and so I represent them... But yes, I am not going to be the hardest sell on this,... |
**Yehonathan Sharvit:** Yeah. I think that most of the concern of static type people is based on fear. Like, "Oh, I need to know what I have." It's like you're a control freak. And tooling. |
**Jerod Santo:** Yes, tooling is the big one. |
**Yehonathan Sharvit:** But if you put those two aside, and you are interested about what really happens when the program runs, after you have written it... Let's say you want to debug a program in production. So there, your tooling will not really help you. And you want something that, when it runs, you want the-- eve... |
\[22:06\] For example, the API for Google Docs, right? You want to modify the title of the Google document. If you are using a generic data structure, you pass the JSON, with the title, and document, and body, and the author name, and first paragraph... And that's what goes on the wire anyway. But if you are a static -... |
I think one way to measure the readability or the goodness of the code is to see how easy it is to write tests for it. And when you use generic data structures, it's very easy to write tests for your code. You just create a map with the field that your function expects, and you call the function. And it could be that a... |
**Jerod Santo:** Really valuable. But does that flexibility scale? So one of the things that I've found over time as I've written many Ruby programs, and done it in such a way that it's flexible, and I could just pass maps around, and I could just test the parts of the maps that I'm interested in, is that I ended up wr... |
**Yehonathan Sharvit:** Yeah, so let's keep to principle number four, and then we'll go back to principle number three. |
**Jerod Santo:** Okay. Alright, so let's set that one aside... Let's go -- yeah, straight to principle four. And we'll go back to three. Separating data scheme from data representation. Go ahead. |
**Yehonathan Sharvit:** So how do we do data validation in a dynamically-typed world, right? So I'm writing an HTTP server, an API, with lots of endpoints, and each endpoints receive a payload, and each payload has an expected shape for the data. So until I think four years, the way I would validate that the data is va... |
**Jerod Santo:** Right. You can't call this method on this nil thing. |
**Yehonathan Sharvit:** \[25:43\] So that works well when you're a startup, and you don't really care about safety, and you need to move fast. And that's what I did until four years ago. But four years ago I discovered that there is a way to express programmatically the expected data shape. It started from something ca... |
So in JSON schema, you have the map that describes the expected field in your map. For example, you could say - the schema for a book, I have a field called Title, a field called Pages, Title should be a string, Pages should be a number. And there are functions for validating and saying, "Okay, here is a piece of data ... |
**Jerod Santo:** Sure. |
**Yehonathan Sharvit:** So instead of writing the class, that says, "Hey, that's the class of a book, and let me try to JSON parse this string into my book", I have a schema, which is super-flexible, I can express many, many conditions, there is no limit... I can even pass function as predicate, or numbers in a range, ... |
So I think for that, for API, for validating user input, dynamic programming is even better than static programming. It's not only as good as. I claim it's better for this kind of data validation. There is another kind where it's worse, but this first kind, it's better. |
**Jerod Santo:** Okay. It's better because -- say why it's better again. Why is it better? |
**Yehonathan Sharvit:** Okay. First of all, because you can express conditions that are not expressible as static types. Like number in a range. Number of pages should be between 0 and 10,000. You cannot express that with static types. That's number one. And number two, because you can, for example, very easily generat... |
**Jerod Santo:** \[29:40\] So conceptually, what you're saying is you get to defer the typing, or you get a -- you're basically saying, "Well, you're going to have a schema, you're going to have types, or validations, and like requirements of their data, but it's going to be separated out from your application code." A... |
**Yehonathan Sharvit:** Are you convinced? Or half-convinced, or...? |
**Jerod Santo:** Um, I'm interested. I understand why you used JSON schema as an example, because it's broad-sweeping, versus a specific implementation or toolchain inside of Clojure... But I wonder how accessible this setup is to different developers in different circumstances. Oftentimes what we find is inside of an ... |
**Yehonathan Sharvit:** Yeah. So here, you need a little bit of discipline, because nobody is going to force you to write a schema for your endpoint, for the payload and for the response. While in statically typed languages, you are forced to type. So yes, that's maybe a little downside. But once you get to it, you do ... |
Where it's more challenging is when we talk about another kind of validation, which is - okay, I've passed my endpoint, and the endpoint calls a function that calls a function that calls a function that calls a function. So I'm going down the stack. And now I call the function foo, that receives the book. But inside th... |
I don't know, maybe you have seen in VS Code, which relies on JSON schema, when you want to edit some configuration files, VS Code knows the JSON schema of the file. And if you mistype the name of a field into your configuration, on the fly VS Code will tell you "Hey, you have an error." Because actually, there is a re... |
So we can have something similar in our code; not only for configuration data, but also for function arguments. So here, we are not empowered with static typing. In terms of tooling and internal functions, when the data flows, it's not as good as in static typing, I admit. But what we do have is when you decide to type... |
Let me tell you -- again, let's take the function foo, that receives a book, and it's supposed to return whether it is a good book or not. |
**Jerod Santo:** \[34:16\] Yeah, thumbs up or thumbs down. |
**Yehonathan Sharvit:** Yeah, thumbs up or thumbs down. Let's say thumbs up is more than three stars, something like that, and less than 1000 pages. If you have a schema for your book, what you can do is use JSON schema library that -- so the first library that we discussed would validate data against schema, but there... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.