Check out a free preview of the full Rapid Application Development with Code Generation course:
The "Dynamically Invoking Methods" Lesson is part of the full, Rapid Application Development with Code Generation course featured in this preview video. Here's what you'd learn in this lesson:

Lukas live codes dynamically invoking methods, including a single invocation, sequenced invocation, mapped invocation, and reduced invocation. The techniques used in this segment will be used as a foundation for the tools created in this course.

Get Unlimited Access Now

Transcript from the "Dynamically Invoking Methods" Lesson

[00:00:00]
>> The next example that I want to pull up in the primer is the ability to dynamically invoke a method. So I have four methods here, add, subtract, divide and multiply. And in this case we're taking a and b and we're doing something with this so, from here I have defined an action and it has two properties on it.

[00:00:32] So it's an action object. It has a funk property for function and params and so it's taking a parameters object. Now the one thing I will point out is that when you're doing dynamic method invocation, you may not always know what your parameter signature is going to be.

[00:00:54] As a result, I prefer to put in a single parameter object and then using JavaScript. I like to D structure the parameters that I want to use from the parameters itself. So in this case, I'm passing in a single object, and then I'm pulling a and b off of it performing some calculation and sending it back.

[00:01:18] Now we have a single transaction which is just a single action or we have a ledger. So imagine that you're working like a cashier stand or you're doing some kind of math and these different events happen that you need to either multiply, divide, add or subtract. And so the question is, how do we execute a single action.

[00:01:52] How can we accomplish that? So, I am going to just delete everything that was there and we will build this out. So, to execute a single action, we can create an execute method. And this takes in a single action property. And then from here, we're just going to call action, funk, action, params.

[00:02:27] And so we are able to store a reference to that function and then take a parameter object D structure it and then call it. So in this case if I wanted to execute the single transaction we'll go const result execute, transaction and then from here let's go ahead and realize it's looking for output.

[00:03:02] So let me just go ahead and define that real quick. So now, I have my result, and if I go down here we can see that the result of the single invocation was, well let's see here. I want to add 1 and 3, I could update this to be five and, 83, and you'll see here it's 88.

[00:03:32] So just this very, very simple idea of we are storing a reference to a function, and then we're storing a reference to the parameter map. And then we are able to essentially call this at some point. And so now I could take this transaction object it stored in a database, pull this out, and then execute it.

[00:03:55] So what this represents, and this is really, really important is simple and basic is this is that this represents something that can happen now or it anytime in the future. So now we're starting to play with space and time just a bit. And so the next question is, well, how can we execute an array of actions?

[00:04:25] So what would this look like? Well, what I can do is underneath this output, I can go and go ledger for each action and we will from here I'm going to go output. Within this we're going to capture the output of every one of these particular action objects.

[00:05:01] So, we're gonna go action. Well, before I do that, so real quick, I can go action.func(action.params) And let me update this from constant let, there we go. And if we go here, what we see is that it's just kind of bunching them together. So let's fix this. So within here, I'm gonna just enter a newline character output, who would have thought that that was case sensitive Now what I've done here and hopefully everybody kind of saw this is like, well, aren't you kind of doing that up here?

[00:05:53] And so I realized and this is important that when you are coding, have your spidey sense on so that if you type something you're like, wait a second, I feel like I just did this. Then this is an opportunity to stop and realize like can I optimize this so in this case.

[00:06:22] I was able to refer to the smaller function that I defined up here and so on by having fine grained single purpose functions that they do one thing, we're able to composite it together in a much more efficient way. Also something that is very, very important. Now, this is kinda cool, but the problem is that we actually have six different results or four different results in this case.

[00:06:56] We have unique distinct results whereas there is back to the clothesline a lot of circumstances where instead of just going through and just kind of outputting stuff Into these individual kind of function executions. We want to contain that into a single collection and ideally, preserve the previous collection.

[00:07:23] So I'm gonna go here, ledger and so, we'll just switch this up to map. We're going to go action and then From here, we are going to then go execute, action, okay. Now what we're gonna see here is that map returns a brand new array. So we were able to preserve an array or preserve this order and push it back into a new array which is important.

[00:08:10] Last one. Instead of having a collection of transformed objects, you may want to condense this into a single value that is returned. And so this is where reduce comes in. So from here ledger, reduce. And now, the typical parameter signature for reduce is something like ACC for accumulated plus CURR for current.

[00:08:45] I tend to deviate after that and so in my case, I'm gonna go with output and action. And I don't have a strong opinion on this if you wanna preserve the original API, I find that it is a lot easier for me when I update the parameters to be a little bit more self documenting.

[00:09:11] So we're going to return, Execute, action. And we're going to start with, An empty string, And this was actually a little more verbose than I needed to be. So, I need to go. And then I'm just going to return this We save this, [LAUGH] And now what we have is some sweet string concatenation.

[00:10:13] And execute action. This is so, why this is happening? Because guess what we're doing, math operations, and I put an empty string in there. So this is the real result, which is 13. And so, hopefully, question to the audience. On a scale of 1 to 10, how complex was the JavaScript that we just did?

[00:10:47] Hopefully, you're not super offended in the sense of like, I came here to learn how to do magic tricks or came here to learn how to do something really complex. And we start out by wax in the car, wax on, wax off painting the fence is that in my opinion that I would say in terms of complexity this is maybe a two or a three.

[00:11:14] We're doing some basic string concatenation. We are doing some basic higher order JavaScript functions for each map and reduce. So I'd give this maybe a two or three, so hopefully everybody we're looking at this and we're saying this is approachable, just some basic high order functions. And so, this is it.

[00:11:44] This is in terms of JavaScript, in JavaScript complexity. This is as far as we are going. And with that said we are going to take these very basic techniques, and we're going to start to assemble them into some very powerful tools