Chapter 9 - Refactoring Flashcards
Lehman’s Laws
a set of empirical laws concerning aging, internal quality, and the evolution of software systems
refactoring
code transformations that improve a system’s maintainability without affecting its behavior
Extract Method
aims to extract a piece of code from method f and move it to a new method g.
Inline Method
This refactoring operates in the opposite direction of method extraction. Consider a method with only one or two lines of code that is rarely called. Thus, the benefits offered by this method, both in terms of reuse and organization of the code, are minimal. Consequently, it can be removed from the system, and its body can be incorporated into the call sites.
Move Method
This occurs when a method f is located in a class A, but it uses more services from class B. For instance, it may have more dependencies on elements of B than on elements of its original class. In such cases, moving f to B should be considered. This refactoring can improve the cohesion of A, reduce the coupling between A and B, and ultimately make both classes easier to understand and modify.
Pull Up Method
when we move a method from a subclass to a superclass
Push Down Method
when a method is moved down the class hierarchy
Renaming
giving a more appropriate and meaningful name to a code element.
Deprecation
a mechanism provided by programming languages to indicate that a code element is outdated and, therefore, should not be used anymore. When the compiler detects a piece of code using a deprecated element, it issues a warning.
Extract Variable
used to simplify expressions and make them easier to read and understand
Remove Flag
a refactoring that recommends using statements like break or return, instead of control variables (or flags).
Remove Dead Code
recommends deleting methods, classes, variables, or attributes that are no longer used
Opportunistic refactorings
carried out during a programming task when we discover that a piece of code is not well implemented and can be improved
planned refactorings
typically involve time-consuming and complex changes that cannot be accommodated within a typical development task. Instead, they should be carried out in scheduled and dedicated sessions.
Automated Refactorings
most IDEs provide support to automate refactorings in the following manner: developers select the block of code they intend to refactor and choose the refactoring operation they wish to perform. The IDE then performs this operation automatically.
preconditions
whether the refactoring will not result in a compilation error or change the program’s behavior
bad smells
indicators of low-quality code, that is, code that is difficult to maintain, understand, modify, or test.
Code duplication
a common code smell and one of those with the highest potential to harm the evolution of a system. Duplicated code increases maintenance effort, as changes need to be applied to more than one part of the code.
clones
Blocks of duplicated code
Long Method
considered a code smell, as they make the code harder to understand and maintain.
Large Class
classes shouldn’t assume multiple responsibilities or provide services that aren’t cohesive. Considered a smell because they make the code harder to understand and maintain. These classes are also more challenging to reuse in another package or system. Large classes are characterized by a large number of attributes with low cohesion among them.
God Class or a Blob
When a class grows so much that it starts to monopolize most of the system’s intelligence
Feature Envy
This smell corresponds to a method that seems to envy the data and methods of another class. In other words, it accesses more attributes and methods of class B than of its current class A.
Long Parameters List
In addition to being small, methods should, whenever possible, have few parameters. That is, a Long Parameters List is a smell that can be eliminated in two main ways. First, we should check whether one of the parameters can be obtained directly by the called method. Another possibility is to create a class to group some of the parameters
Global Variables
should be avoided, as they result in a poor type of coupling. For this reason, they are considered a code smell. Essentially, global variables make it more difficult to understand a function without looking at other parts of the system.
Primitive Obsession
This code smell occurs when primitive types (that is, int, float, String, etc.) are used instead of specific classes.
Mutable Objects
one whose state can be modified after creation
Data Classes
These classes contain only attributes and no methods, or at most, they have getters and setters.
Comments
the idea is that comments should not be used to explain bad code. Instead, we should refactor the code, thereby improving readability. After doing that, there’s a good chance that the comments will become redundant
Technical deb
technical problems that can hinder the maintenance and evolution of a system. These problems include, among others, lack of tests, architectural issues, systems with many code smells, and systems without any documentation at all.