Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Assignment 1 - SER566

Karandeep Singh, Grewal - 1216985692


January 2021

1 Refactoring
Refactoring code is a systematic approach to improving the internal structure without changing the external
behaviour of the code [1]. Code should behave in the same manner, before and after the refactoring. It
helps to improve the maintainability and readability of the software system. Refactoring has a significant
improvement in metrics and defects like: [3]:

1. Maintainability Index

2. Average Cyclomatic Complexity

3. Average Class Coupling

2 Refactoring Patterns
2.1 Extract Method
According to [2], functions should do only one thing and they should do it well. When a function/method
has more than one responsibility, we need to decompose it.
Extract method is one of the most common techniques used to decompose such methods. Extract method
tries to find a part of the method that can be put as a separate method. Ideally, all the statements of a
function/method with single responsibility should be at the same level of abstraction [2].

2.1.1 Advantages [1]


• It increases the chance of method reuse.

• Method can now be read as a series of statements with the same level of detail/abstraction.

• Overriding methods also becomes easy.

• Minimize the use of comments.

2.1.2 Things to consider while extracting method [1]


• Name of the extracted method should correspond to its responsibility.

• If any of the local variables are used in the extracted methods, pass those variables in the method call
from the original method.

• Any local variables that are used only in the extracted methods should be local to extracted methods
only. There is no need to pass them from the original method.
2.2 Introducing Explaining Variable
Sometimes we find very complicated expressions directly used in function calls, conditional statements, loop
statements, and other similar places. They can be hard to read. This pattern suggests to put a complicated
expression in a temporary variable that explains the result of that complicated expression. It helps to easily
manage the result of the complicated expression. Sometimes, it is not easy to use ’Extract Method’ pattern.
So, an explanatory variable is used in those situations. [1]

2.2.1 Advantages
• Makes to the code easy to read and understand.

• Result of the same complicated expressions can be used at multiple places.

2.3 Move Method


Sometimes, we have classes that have too many responsibilities or too much collaboration, resulting in high
coupling. This technique suggests moving the methods from one class to another, to minimize the interaction
and giving the most relevant responsibilities to respective classes. [1]

2.3.1 Advantages [1]


• Classes become loosely coupled. It reduces the dependencies between the classes.

• Classes have the most relevant responsibilities.

• Helps in avoiding unnecessary data classes that only contain data, getters, and setters.

2.3.2 Things to consider while moving method [1]


• Check all the sub-class and super-classes of the class whose method is being moved to make sure there
are no other declarations of the same method.

• Sometimes, a set of method is easy to move than moving a single method. Check for all the features
used by the method being used to find out what other features or methods need to be moved along
that method.

• You may have to turn the method in the source class to a delegating method so that no method calls
result in MethodNotFound exception.

• You may have to find out a way to refer to the source class if moved methods use features from the
source class.

3 Refracting Tool - JetBrains IntelliJ [4]


JetBrains develops a series of integrated development environments for different programming languages.
IntelliJ is developed for JVM (Java Virtual Machine) languages. It provides various code analysis, code-
completion and refactoring tools. It has a built-in marketplace for additional plugins. The refactoring
capabilities of IntelliJ are discussed in the following sections.

3.1 Supported Refactoring Options in JetBrains IntelliJ


1. Safe Delete: Search for all the usages of the element before deleting it to make sure it doesn’t break
the code.

2. Copy/Move: Copies/Moves the elements from one location to another, and make changes in the code
to keep the external behavior of the code intact.

3. Extract Method: Shows duplicate code and support ’extract method’.


4. Encapsulate Fields: suggests getter and setter methods for class data fields. This prevents the need
for direct access to data fields.
5. Find duplicates: helps to find code duplication and suggests relevant refactoring options for each
duplication.
6. Inline: Inline replacements are suggested when it is redundant putting the content inside a separate
element. Inline has support for variables, methods, constructor, superclass, and anonymous class.
7. Pull members up or down: enables you to move the fields or methods of the multiple sub-classes to
the super class when the methods are identical. Also, it enables you to move a sub-class specific fields
or methods from superclass to the subclass.
8. Remove middleman: enables to remove a delegate method by replacing all its calls to the actual
method.
9. Renaming: enables to rename any element of the code without checking all the other occurrences or
usages of the element. This increase the overall refactoring speed.
10. Undo: This is not a direct refactoring option but helps to revert back changes, when refactoring
impacts the behaviour of the code.

4 How does IntelliJ addresses the refactoring patterns mentioned


in Section 2?
4.1 Addressing Extract Method:
1. It enables you to select a fragment of code and turn it into a separate method.
2. IntelliJ can’t take the decision on what section of the code needs to be turned into a separate method.
But it does warns you when there is a long method. This helps to identify a potential candidate for
Extract method.

4.2 Addressing Explaining Variables


1. IntelliJ doesn’t directly addresses the issue of introducing explaining variable.
2. But IntelliJ suggests meaningful (to some extent) names for variables which does supports this pattern.

4.3 Addressing Move Method


1. IntelliJ supports moving methods from one part of the code to another by checking its usages and
occurrence. It automatically replaces the method calls according to how we move the method.
2. IntelliJ doesn’t automatically suggests the methods that need to be moved but other analysis tools of
the IDE can be used to find the potential candidates.

References
[1] Refactoring: Improving the Design of Existing Code. USA: Addison-Wesley Longman Publishing Co.,
Inc., 1999, isbn: 0201485672.
[2] R. C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship, 1st ed. USA: Prentice Hall
PTR, 2008, isbn: 0132350882.
[3] C. Dibble and P. Gestwicki, “Refactoring code to increase readability and maintainability: A case study,”
J. Comput. Sci. Coll., vol. 30, no. 1, pp. 41–51, Oct. 2014, issn: 1937-4771.
[4] Code refactoring - help: Intellij idea. [Online]. Available: https://www.jetbrains.com/help/idea/.

You might also like