All right so at the end of the last video we read our program and we saw that we got the same applause
as we've had for quite a few videos now.
But we defined our parameters.
We can say those write down on Line 23 can't quite see them also describe that up a bit.
There's four parameters that we've defined and you can see the arguments where we're calling the method
online eleven.
Second it's a recap what's happening here with is game either score level completed and bonus is that
Java is actually creating variables with those types with those names for us automatically and it's
assigning whenever value we've passed in the parentheses when we're calling the calculate score method.
So true eight hundred level completed and bonus.
The other thing that it is is looking in order that we need to make sure that when we're calling this
method we match the same parameter.
So we've got these came over has to be first we put that in another position.
Finally get it error.
All right.
So far so good.
We're running out okay.
Let's see if we can clean up this second bit of code with this code.
Here is well above us and just have one line defined.
We're going to call that method again the same method names that calculate score and in parentheses.
What we're gonna pass this time is true again.
Yes this game is over and that's how that we get the relevant output showing that we gonna pass the
values we're going to hardcoded them say ten thousand four score from line 14 common level completed.
I'm going to top eight their comma and then I'm gonna finish up with bonus which is the final line sixteen
to be two hundred and there's a valid corner.
So now that I've done that we can clean up this code completely
and you can see our method our main method is now starting to look pretty good we've cleaned up a lot
of it so far.
Notice up here now with score now that we're no longer using the score very well that are deleted if
we hover over that as well as his came over we can say that neither of those variables are being used.
The thing to remember and we'll talk more about this when we start talking about scope but these variables
that we've created in our main method are only for the main method and don't work here.
So in other words we can quite happily Define variables of the same name outside of our calculate score
method.
This is not telling us it's no longer being used in the main method.
That's because we deleted that code so I could happily delete those now but note that level completed
and bonus we're currently still using on line 9 so for delete those will actually get an error a complaint
here from Intel I try that but so we cannot resolve the simple level completed and bonus which of course
is correct because we just deleted them.
So what I'm meant to do is take the opportunity to undo that change briefly so I can see that level
completed was five and bonus was one hundred.
Let's change those out change bonus to be one hundred and then we've done that.
You can say that time we got the same everything about them being unused.
I'm cleaning that up as well.
So consequently our entire main method now consists of just two calls to calculate score and you can
see that we've got some different values being passed for most of the variables other than ours came
over which is set to true in both cases and that's so that we can actually output the code or get the
code executing and putting the lines 15 through 17.
Now firstly before I talk more about our final score if we run this code now remembering the pellets
that we've had for quite a few videos now if we run it.
Notice that we've got some different values appearing on the screen now.
The final scores you've got now two thousand three hundred and twelve thousand six hundred and if you
recall a few days ago I talked about the fact that we made a change in one place and that was the code
adding 1000 to the final score and our first output was twenty three hundred but the second was eleven
thousand six hundred.
But now that we've swapped over and we're using calculate score for both calls you can say that final
score is now being adjusted for each of those calls we've made the change in one place and we've got
the output showing proposed that this is what we've done here is we've removed our code duplication
and we've made it easier to edit because we can make a change in one place.
Now the code in there calculate score method and that will change any time we call the calculate score
method.
It's a lot easier to maintain.
We don't have to worry about missing making a change if we've got code duplication all over the place.
And just to demonstrate that now I can change this 1000 to 2000.
We can run it remembering that the values we had previously on screen two thousand three hundred and
twelve thousand six hundred we've now got the final score values of three thousand three hundred and
thirteen thousand six hundred.
Clearly both have been updated by an additional 1000 points.
I think you'd agree.
Looking at the code we've got now it's a lot easier to read it's less code less code is usually a good
sign because it means there's less to understand and so not only is there less code to understand and
to maintain which is what you do as a developer making sure the code is to updated and it's going to
be easier to maintain.
But we've also removed that code duplication.
It's basically a win win.
So in essence one of the great features of methods is that it makes life a lot easier for you to maintain
your code because you've literally only got one place now where the code is modified.
This one method by the why there wasn't a problem with using variables.
So before I deleted those variables out of the main method.
You can certainly do that and that is certainly valid.
And there was a lot of scenarios where it would be valid but it's really just starting our discussion
of method.
So we've got to start from somewhere.
So it's easy to show you how to do that.
And in general what's a good idea to reduce the amount of code that you're typing if any using a variable
purely to pass to a method and not using that for anything else then it probably makes sense just a
hard code the values at least in this simple test.
But if you're running some production code probably there'd be an argument there to make it easier is
to define a variable name but we'll talk more about the sort of rules and best practices a little bit
later for methods.
We're just making a start for this for now.
All right.
So basically you're still free to use hard coded numbers as we're using here on line seven to eight
using those literal values we can define variables if you like and use the variable name which you saw
earlier in the video.
And again what happens is if you're using variables and you're calling a method the value was passes
this of argument and Java automatically creates a variable name as we talked about the variable top
and name is based on what's defined in the parameters.
In this case on lines Eleven and 12 and it assigns and puts the values in that being passed as arguments
in those method calls the line seven through eight.
Well it's I think agree that its methods are pretty cool in their own right.
And the fact that we can actually call a method and get it to do something to make our code look a lot
cleaner is a cool thing but it does actually get even colder than that because what we can do is we
can get our method to do some calculations much like it's doing now but then we can send back the result
of that calculation or that processing whatever we're doing back to the code that was executing that
it actually called the method in the first place perhaps for some future processing.
So that's the topic of the next video.
I'll see you in that next video.