Professional Documents
Culture Documents
Assignment 1 - SER566: 1 Refactoring
Assignment 1 - SER566: 1 Refactoring
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 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].
• Method can now be read as a series of statements with the same level of detail/abstraction.
• 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.
• Helps in avoiding unnecessary data classes that only contain data, getters, and setters.
• 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.
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.
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/.