So now we saw, this set of re-factoring's. They're nice, but
how can we actually perform re-factoring's? In some cases you'll have to
do it by hand. And you'll do it in that case in
small steps, so that you can check at every step that you
didn't introduce any area. But there's also many cases in which at
least for the more standard re-factoring's, you can just apply, you can
just use a tool that actually supports re-factoring. I'm going to show
you how that works, into a specific ID, Eclipse through a demo.
To show you how Eclipse, can help in performing
re-factoring, in an automated way, I just opened the Eclipse
editor and I maximized it. So that we can look
at the code more clearly. And as you can see
here, I have this class. It's called Re-factorable, it's a
pretty indicative name. And what we're going to do, we're going to
try to apply the extract method re-factoring to this class.
And in particular, to parts of this print owing method.
So this is a matter than will print owing's,
as the name says. And it will do several things
such as, for example, printing a banner first, then
calculating the outstanding debts, and then printing some details. So
the starting point for an extract method re-fractoring, is
the identification of some cohesive code fragment. And here, for
instance, we can see that, if we can see there,
these three print statements. They are basically printing some banner,
for the method. And I also put a comment here
just to make that even more explicit. So this is a
perfect case in which we might want to just extract
this part, create an independent method, so that we can make
the code more readable and maintainable. So I select, the
part of the code, that I want to put in my
method. I invoke the contextual menu, and as you can see
there is a re-factor entry here. Here are some re-factoring's [UNKNOWN],
re-factoring's that I can apply, and I'm going to select extract
method. When I do that, Eclipse is going to ask me to
specify a method name. I'll just call this one print
banner. And as you can see, as soon as I do
that, Eclipse will show me the preview, for the method
that will be generated. I'm going to leave the access modifier. To
public and I'm not going to change anything else. So,
now when I click Ok. As you can see Eclipse modified
my code so that now I have the Print Banner method
down here that does exactly what that piece of code was doing
before. And I just have an invocation of the Print Banner
method, up here, where the code was before. And of course, this
is something that we could have done by hand. It's pretty
easy to do, but it's even easier, to do it using Eclipse's
capabilities. And this will become even more apparent, when we consider slightly
more complex case. So here, if we look at this piece of
code for instance, we can that see this code
prints some details, about the always. And the reason
why this case is likely more complicated, is because
this code needs to know about the value of outstanding.
And whereas that underscore name, is a member of
the class, and therefore will be available to the
method. Outstanding is a locker variable, so a method
different from print, oh it wouldn't know anything about outstanding.
So let's see what happens when we try to apply
a re-factoring for this code. So we go again here
to the re-factor menu, we select extract method, we will
pick a name again. So let's call it [SOUND] print details,
since this is what the code does. And as you
can see here, Eclipse was able to figure out, that outstanding
has to be a parameter, of this method. So if
you look at the signature here, this will be very clear.
So outstanding has to be passed to the matter because
it's a locker variable of the print owing method. so
it will not be visible to the other methods otherwise.
So since eclipse figured it out, all I have to do,
is to press Ok. And at this point what I
will have here is my new method, for in details
that takes outstanding as a parameter. And does exactly what
the code was doing before. And here, where the code was,
I will have my print details invocation, with outstanding
as a parameter. So now, let's continue to extract methods.
And let's look at a even more complex case. which
is, the one involving this piece of code. So this
piece of code, as you can see, will calculate
the value of the outstanding debt. Will calculate the owing's,
and the way in which it does that, is by
considering all the orders, that are part of this enumeration.
That is the declared here, and it will
compute for each one, of these orders, the amount,
and then added to outstanding. So what is the
additional complication here? Well, the additional complication here is
that this code needs to know, not only
about outstanding. It also needs to know, about this
enumeration, because this one is also a local variable.
And in addition to that, this code also has
some side effects. So outstanding, is modified as a
consequence of the execution of this code. So how can
we do that in the extracted method? Well lets
see what the clips will do and what the clips
will suggest. It will try to again re-factor this
code and extract the method. In this case as you
can see. The clips does two things. First of all,
it figures out as before, that there are some parameters,
that are needed for this method to operate correctly. The
enumeration e, as we said, and the outstanding variable. In
addition, if you look at the method signature Eclipse will
also figure out that this method has to return, a
double value. So what does this value correspond to? This
value corresponds to a new value of outstanding. So if
we, give a name to this method, so we just
use the name, [SOUND] that I put in the comment
over there. We click Ok, and this will create
a method by extracting the code. And here, where the
method used to be, we will have that the
value of outstanding is updated. Based on the return value
of calculate outstanding. So in the end if we
look at this code, you can see that if we
just focus, on this code it's very easy to
understand what it does. It prints the banner, it calculates
an outstanding value, and then it prints some details. And
in case we don't care, as I said before, about the
details of what these methods do, we're done. And if we
care about the details we can look at each matter individually.
And get exactly the same information that we got before,
in a sort of a separation of concerns kind of way,
by focusing on one problem at a time. So now let
me do one last thing. So let me modify the code,
slightly. So i'm going to go back, to the version of
the code before re-factoring. So this is what we had.
And I'm going to add, an additional variable here, [SOUND] called count,
which I initialize to zero. Here I'm going to increase, [SOUND]
the value of count at every iteration. And
finally, here I'm going to print out the value
of count. Okay, now that I have this code up. So let's imagine that I
want to, again as I did before, extract this matter. So, I'm going to
give you a second. Have a look at this and see, if you see
any problem with that. Feel free to stop the video, if you need more time.
So the problem here is that I have two side effects.
Both outstanding and count are modified. And therefore it's not really
possible to extract this method, and preserve the semantics of this
code. Let's see if Eclipse will be able to figure that out.
And it does. If we try to extract the matter here,
you'll tell us that's an ambiguous return value. The selected block
contains more than one assignment to look at variables. And the
affected variables are outstanding, just a Double and Count which is
an integer. So it will refuse to extract the method. So
at this point if we wanted to do that we have
we'll have to do the re-factoring a different way, but I
don't really want to get there. I want to conclude here,
and I hope this this little demo helped you
realize how useful it can be to use an
id that supports re-factoring that can automate this important
task. And I also encourage you to try to play
with this, and try to use different re-factoring's, on
your code. So as to get familiar with the kind
of re-factoring's that are supported by the ID. And
also with the re-factoring's themselves and how should be used.