In the last video, I said that our application was all wrapped up, but I can't help myself.
Just one last little thing.
I swear you will like it.
It's going to condense some of the code inside of our indexed file just a little bit.
So like I mentioned several times, having to create a new instance of summary right here and then pass
in new wins analysis and new HTML report.
Well, that's just a little bit verbose.
Chances are whenever we create a summary, we might always want to have this same analysis where we
might want to always print out an HTML report.
So having to always pass in those instances, every time we create a summary, well, there's just got
to be a better way.
So in this video, I want to very quickly show you how we can kind of add in a shortcut for this statement
right here.
We can also use this technique to get a shortcut for the previous use of composition that we had up
here as well.
Remember, this is doing the same thing up here using composition.
We first create an instance of CSV file reader and then pass that into match reader.
So I want to show you the shortcut that we can use in both these locations.
So to create a shortcut, I'm going to open up my summary file again.
Inside of summary.
We currently have a method inside of here.
Remember, methods can only be called once we have created an instance out of this class.
So in other words, to call, build and print report we have to say cons summary is new summary.
Pass some stuff in and then we can call summary, build and print report on the instance we created.
There's another type of method that we can add to a class called a static method.
Static methods can be called off the class itself.
So in other words, if I add a static method at the top of this thing, which I can do by writing out
static and then the name of the method.
So how about just like print?
Hello.
And inside of here I do a console log of print or just high.
I can now call this method because it is marked as static without creating an instance of summary.
So down at the bottom of the file I can say summary dot print.
Hello.
Like so no issue whatsoever.
However, if I try to call print hello or simply try to call a build and print report off the class
itself, I'll very quickly get an error message.
Because the class does not have a method called Build Imprint Report.
It's only on an instance of summary.
So how can we use these static methods to get a little shortcut here?
Well, essentially, we can create some static methods that give us back pre-configured instances of
summary.
So we might decide to make a static class method on here called summary.
Or how about like what?
It would be a good name here.
How about winds analysis with html report?
Really long, but I just want to give you kind of like an idea of what this method would do.
So maybe if we call this method, it would give us back a copy of summary or an instance of summary
that already has winds analysis and an HTML report loaded up into it.
So let's try that out.
So you actually put this together.
We need to make sure we import winds analysis and HTML report at the top.
So I'm going to do that right now.
I'm going to import.
Winds analysis.
From the.
Oop.
Analyzers directory.
And then I'll also import.
HTML report.
From the report.
Target's directory like so.
So now down inside of our static method right here, we can say return a new summary.
And we're going to preload this thing up with instances of winds, analysis and HTML reports.
So the first argument will be new wins analysis.
Second argument will be new HTML report.
And then remember, winds analysis needs to know what the team name is.
So we would probably need to pass through the team name from this method itself.
So we'd probably want to receive the team name as an argument to this class method.
So I'll say team is string like so and we can just pass through team.
And then finally I'm going to make sure I annotate the return type of this method as well.
It's going to return an instance of summary like so.
Okay.
So now any time that we want to create an instance of summary that already has these things loaded into
it, we can just call this class method.
So let's try that out.
So I'm going to save this file, flip back over to indexes.
And now, rather than writing out this really long statement right here.
We can just call summary dot winds analysis with HTML report and pass in our team name right there of
Man United.
Notice that when we use a class method, we don't have to use the new keyword.
We do eventually use the new keyword inside the class method.
So inside of here is where we actually use new.
So the one thing that's kind of bad about this approach is that it is not immediately evident that we
were returning an instance of summary as it is when we were previously using the new keyword here.
So yeah, kind of a downside there, but in general, kind of a little bit less code.
Now of course, I used a very long method name for this, but I only use the long method name just to
make sure there was really clear what we were doing with this method.
So now I can save this file.
Pull it back over to my terminal and oops looks like let me just restart here and hit a control C and
start again.
And you know what?
I think I must.
I must have missed a step in here.
So let's take a quick pause.
I'm going to hunt down that issue, and then we'll find out what is going wrong.