-Hi. In this video, we are going to look at the refactorings
and how we can use them in Pharo at different levels.
Refactorings were introduced in Smalltalk in 1995.
They are extremely handy because they allow you to refactor pieces of code,
change the relation between your classes and methods, etc., extremely quickly.
I have taken back the counter we made in the redo of week 2.
It still has the four methods we implemented:
count, count:, decrement, increment, and then the tests.
You can check that they are still green. Cool.
The first level of refactoring you can have is within the class.
You can right-click on the class, go to...
You will have Rename, Copy, Jump to test class, Remove, etc.
Then, you have got Refactorings
that will allow you to insert subclass, superclass,
and/or change this class from a package to another.
We will look at Rename. Let's click on it.
We will just rename it "Counter2".
Ok.
You can see that our counter is now renamed "Counter2".
If you go to the CounterTest,
we can see that every occurrence of "Counter"
has been replaced by "Counter2".
You can run the tests, check that they are still green.
It is the case. Great.
Now, we will look into another level of refactoring that is methods.
If you right-click on a method, for example "increment",
you still have Rename because it is extremely useful.
So it has been moved to another level. But you can go to the Refactorings tab
and you can see that you have Push up,
so you can bring this method to its superclass or subclass.
Then you have Move to class side or Move to another class,
Move to another package. You can Add argument, etc.
We are going to look into the Rename for now.
We press Rename,
and we would like to rename our "increment" method to "increment2".
Press Rename.
You can see that this will modify all 9 implementors.
But we only have one.
This might mean that we are modifying things that we would not like to.
So, let's press No.
Just to remind you, the shortcut for implementors is Cmd+M.
So, let's press that.
You can see that there are many implementors of increment.
Some of them will rather not change, such as JobProgressBarMorph.
In order to contain our actions to the actual package,
we will set up a scoped view.
You can either right-click and select Set up scope
or you can simply, since we only have one package,
press the button Scoped View.
We will do that for now. Scoped View. Now, if we right-click
and try to rename it again, "increment2", Rename,
this time we have only modified this method,
and you can see that this would be the changes that would apply.
It will add a method called "increment2" in the class Counter2. That is great.
That will change the test path using increment,
changing "increment" to "increment2",
and then remove the actual increment method.
That is perfect, that is exactly what we want.
Let's just press Apply.
You can see that we now have a method increment2.
If we go to the testCounterIncrement, you can see that it changed.
Now, we are going to look at another level of refactoring which is protocols.
You can right-click on them, you can rename them,
you can remove empty protocols.
Basically, this happens when you create new methods,
tag them into protocols, and then remove them without changing the protocol.
This might happen. You can simply press this button
or you can categorize all methods that are uncategorized.
The last level of refactoring that you can use is from within the source code.
Let's go, for example, to testCounterStartingAt5.
We will just select that, highlight it, and you can right-click
and go to Source code, and there you will have all the possible refactorings,
Rename, etc., and you can extract methods.
This is interesting because... Let's click on it.
We can say we will call it "startingAt5".
Rename.
You can see that we now have a method there
that is doing exactly what we have highlighted earlier on.
You can see that it is doing "Counter2 startingAt: 5",
and then, in the test, you can see that it is calling "self startingAt5".
So, it has really extracted what we highlighted in the method.
This might not be the best idea here
because we are talking about a test class.
But it is just to give you the idea that you can simply highlight a piece of code
to extract its method.
I really encourage you to learn refactorings and how you can use them
because you will really be faster in your writing.
You will simply develop faster and maybe better
because this will help you to not forget some references you might have written
to a method when you rename it or it will save you time
when you remove a method then write it back again.
You can simply refactor, push up, push down, move to class side.
Please, use refactorings, they will really help you.