Better and more refactorings for Pharo-Part 2

EVELYN CUSI LOPEZ
5 min readMay 20, 2019

This post is aimed to give a small introduction of what refactoring is and to present the refactoring tool that Pharo currently has, in such a way that it can show the starting point that we will have before carrying out this project. So let’s get started…

1. What is refactoring?

Refactoring is to make a transformation to the software preserving its behavior, modifying only its internal structure to improve it.

2. Why should we do refactoring?

One of the reasons for refactoring is to help the code to stay in “good shape”, since over time the changes in the software cause it to lose its structure, and this makes it difficult to see and preserve the design. Refactoring helps to avoid typical problems that appear over time, such as, a greater number of lines to do the same things or duplicate code.

3. Pharo’s refactoring tool

The Pharo’s refactoring tool has the following automatic refactoring capabilities:

  • Rename
  • Extract
  • Inline
  • Push down and Pull up

3.1. Rename

It allows to change the name of classes, local variables and methods. It is useful when you want to safely change the name of something without having to search all the instances and copy and paste the new name.

Figure 1 — Window of rename

To access this option, select the item you want to rename and press Ctrl + R, Pharo will show you a window to modify the name (Figure 1).

3.2. Extract

  • Extract Method

The Extract Method refactoring lets take a code fragment that can be grouped together, move it into a separated method and replace the old code with a call to the method.

Figure 2 — Selecting a refactoring option

You can access this option by selecting the code fragment you want to extract, right click >> Suggestions >> Extract method (Figure 2), this will open a window to name the method you are extracting, once named the method, will appear the window “Changes Browser” (Figure 3) showing how your code will look like after the extraction, you can select which changes you want to make and deselect which not.

Figure 3 — Changes browser

Below you will see an example of extract method

— Before refactoring

Example >> method
| a b c d|
a := 1.
b := 2.
c := a + b.
d := a + c.

— After refactoring

Example >> method
| a b c d|
a := 1.
b := 2.
c := self add: a to: b.
d := self add: a to: c.
Example >> add: a to: b
^ a + b
  • Extract local

If you come across an expression that is hard to understand or it is duplicated in several places throughout you code, the Extract Local Variable refactoring can help you deal with those problems placing the result of such expression or its part into a separate variable that is less complex and easier to understand. Plus, it reduces the code duplication.

To access this option you must follow the same steps as to extract a method, but instead of selecting the option Extract method you must select Extract local, this will show a view (Figure 4) where you must define the name of the local variable with which you will extract the fragment of code

Figure 4 — Window for name a local variable that will be extracted

Once you name the local variable, and press Ok, you will get the “Changes Browser” window as before, where you can see your changes in the code.

Below you will see an example of extract local

— Before refactoring

Example >> method
| a b c|
a := 1.
b := a + list size.
c := b + list size.

— After refactoring

Example >> method
| a b c d|
a := 1.
d := list size.
b := a + d.
c := b + d.

3.3. Inline

Figure 5 — Selecting Inline temp option
  • Inline temp refactoring replaces redundant temporal variable usage with its initializer.

You can access this option by selecting the variable that you want put inline and then right click >> Source code>> Inline temp (Figure 5)

Below you will see an example of inline temp

— Before refactoring

Example >> method
| number b |
number := anotherClass value.
b := 3 + number.

— After refactoring

Example >> method
| b |
b := 3 + anotherClass value.
  • Inline method results in placing method’s body into the body of its caller(s). This option can be accessed like extract method, but instead of selecting the option Extract method you must select Inline method.

Below you will see an example of inline method

— Before refactoring

Example >> method
| a b c d|
a := 1.
b := 2.
c := self add: a to: b.
d := self add: a to: c.
Example >> add: a to: b
^ a + b

— After refactoring

Example >> method
| a b c d|
a := 1.
b := 2.
c := a + b.
d := a + c.

3.4. Push down and Pull up

  • Push down improves class coherence. A method is located where you expect to see it. For example if you see that a method is needed by more than one subclass, but not all of them, it may be useful to create an intermediate subclass and move the method to it. This allows avoiding the code duplication that would result from pushing a method down to all subclasses.
Figure 6 — Push down example (Image extracted of website: https://refactoring.guru/push-down-method)

This option can be accessed selecting the method of superclass that you want push down then right click >> Push down.

  • Pull up gets rid of duplicate code. If you need to make changes to a method, it’s better to do so in a single place than have to search for all duplicates of the method in subclasses; this refactoring technique can also be used if, for some reason, a subclass redefines a superclass method but performs what’s essentially the same work.
Figure 7— Pull up example (Image extracted of website: https://refactoring.guru/pull-up-method)

This option can be accessed selecting the method of subclass that you want pull up then right click >> Pull up.

I hope that this post has served to improve their knowledge of refactoring, and to see how to use them with the automatic options of refactoring that has Pharo.

That’s it for today’s post, bye and happy coding :D

--

--