Transcript from the "Methods as Verbs" Lesson
>> We understand that we live in a world of objects. We also understand that we can do things with these objects, we can manipulate them that objects themselves can do things. And so whereas we were talking about nouns, let's talk about verbs. So time to wax the car.
[00:00:22] We have a function and what this does is it takes state and it takes an object and, so it takes really two objects, and then it just returns another object. And so we can think of a function is that it's just a unit that we give it something and we get something back.
[00:00:47] Now, what I want to show here is this is kind of your classic s5ish method and going forward, we're going to be using fat arrows. So, this is what this looks like, all we've done is we've just taken the function piece, dropped it off and put a fat arrow on the other side.
[00:01:09] That's the simplest way to convert it. Now what we have is the ability to not only call a method and how to do something and so in this case, select client is very simple. It's just taking two objects and returning a new object. And from here, we're able to call that method and it does something more importantly, is that we're able to call that method and have the result capture that, and store that somewhere else.
[00:01:48] And so, this is just a very simple, I think, almost obvious kind of tenant about methods, is that you can call them. But a lot of times you wanna structure your code in a way that you can call it and store the result. The reason being is that when you can store when you have a method that was turned something, you can test the return or you can write an assertion on the result of that function.
[00:02:21] And so this is why for me, typically I prefer methods that I pass something in, I get something out that I have a clear contract with that method versus methods that you just call in and something happens you don't know what it is. There are times like that, but more often than not Is when you can, not all the time but when you could have a method that actually does something and then returns a value.
[00:02:48] That is a much easier method to test. So what we can do here, is we can define a store, so think of this is just an object that is keeping track of the clients and the current client. And so you could imagine this would be maybe a place that our initial client state may exist at some point.
[00:03:21] And so what we can do now based on this, is we can find the client store and then we can start to manipulate it in the sense of we can say, I want to create a new client store. What is that? Well, it's an object, we are saying I want to create a new object that represents the store in which I'm storing my clients.
[00:03:48] Then I want this store to do things and so now we were going from data structures with nouns to nouns with verbs. And so we're saying I want to create this noun and then once I have this noun or this data structure, which is nothing more than when you instantiate a class, it returns an object, which is nothing more than a data structure.
[00:04:13] Some of those properties are methods that you can then invoke and so in this case, clientstore.load putting the clients in their clients store dot select or selecting a client. And in this case, we're also creating a new client, so going up to the clients section here class clients store.
[00:04:47] And what we can do is we can have clients we can have a current client and then what we can do and so this is just a transition step we're going to clean this up in a little bit is that it's going to take clients and we're just going to say this clients equals clients.
[00:05:19] And what I'm going to say here is this is also an array of clients and it doesn't like the, fact that I have clients here and then I'm setting it in here. So let me go new clients like so and I'm going to do a create method new client, which is also a client and then I'm going to do something that is not awesome, well.
[00:06:02] It'll be fine, so we're gonna go this clients equals this clients and so we'll talk about this particular operator a little bit later. But basically, I'm just saying, create a new array and put the old clients and then this new client in here. Did forget one thing, so load and we'll go select we'll, send in a client object, And notice everything I'm doing up to this point, I'm just defining essentially verbs, and I'm attaching it to this instance.
[00:06:41] And then from here, I'll just assign this current client to the new client, all right. Now let's instantiate this client store equals new clients store and then let's do this, now, so really drive this point home. I'm gonna set we'll save this And notice that I've instantiated the class, but I haven't done anything yet, so it's an empty object.
[00:07:28] But now when I go clients door and I say load, and then I pass in clients, let's save this, Notice what's happening to the instance of the client store that I've created. Well, it's an object and so I'm just adding stuff to it. Clients store select and let's go with Peter, all right, let's save this.
[00:08:09] And so now we have an object and we have methods that allows us to manipulate this. So I could do the create a new one but I think we are pretty solid on this.