This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
– The process of gradually improving the design of an existing software system by performing source code transformations that improve its quality in such a way that it becomes easier to maintain the system and reuse parts of it, while preserving the behavior of the original system
For example: Extract Method
void printOwing(double amount) {
printBanner();
// print details
print(“name:” + _name);
print(“amount:” + amount);
}
void printOwing(double amount) {
printBanner();
printDetails(amount);
}
void printDetails(double amount) {
print(“name:” + _name);
print(“amount:” + amount);
}Source: Martin Fowler’s online refactoring catalog
– Directly expresses program semantics in terms of data-flow and control-flow
Convenient for machine manipulation
– Naturally expresses semantic transformations
Rich, C. 1986. A formal representation for plans in the programmer's apprentice. In Readings in Artificial intelligence and Software Engineering, C. Rich and R. C. Waters, Eds. Morgan Kaufmann Publishers, San Francisco, CA, 491-506 .
Program Sliding A family of provably-correct code-motion
untangling transformations
– Automate slice extraction: Sequential composition of a selected slice with its complement (i.e. co-slice); Useful for refactoring, componentization, the move to SOA, obfuscation, etc.
– Combine statement reordering with code duplication, including duplication of assignments
– Benefit from the best of leading earlier solutions without suffering some of their respective deficiencies
Sliding is particularly strong in
– Preserving behavior
– Maximizing reuse (of extracted computation’s results, in the complement)
– Minimizing code duplication, i.e. yielding a smaller, more desirable complement;
– Improving applicability, i.e. less reasons to reject a request
Example source: Lakhotia and Deprez (rewritten in COBOL)
Towards a COBOL Refactoring Catalog Rename Paragraph
– This refactoring might look trivial, but as it is with the renaming of variables, it must be done with care: the new name must be valid, it must not conflict with existing names, and it must be replaced correctly in each call (PERFORM, GO TO, etc.), without violating any column restrictions
Split/Merge Paragraphs– When merging two consecutive paragraphs, one must check the second is not referenced, or if it is, its reference must
always follow a call to the first paragraph such that the two calls can be merged. Similarly, one must verify that any call to the first paragraph is either followed by a call to the second, or it must be a non-returning call that implies fall-through to the second paragraph
Extract/Inline Paragraph/Section/Program– Could support clone detection too, such that upon extraction, the tool will identify (at least exact) clones of the selected
code, and suggest to replace it too with a call to the newly introduced program Extract Slice (through Sliding)
– First support the extraction of the code for computing a set of variables in a selected compound statement (or sentence); later add support for extraction from internal program points, i.e., the slicing criteria involves pairs of program point and (sets of) variables of interest (at that particular point); and finally support arbitrary method extraction, i.e., the slicing criteria involves a set of statements (or sentences), not necessarily contiguous, for extraction
Swap Consecutive (Independent) Executable Program Entities– Such as compound statements, sentences, and even paragraphs or sections
Split/Merge (Consecutive) Conditionals– So long as two instances of the conditional’s predicate are guaranteed to evaluate similarly
Loop-Invariant Code Motion– Computation flavor: A loop-invariant computation is moved inside/outside that loop, as in optimizing compilers– Conditional flavor: Instead of a computation, it is a loop-invariant conditional being moved
• If moved out, the loop itself is duplicated, for each branch of the conditional, but with each branch simplified based on the known conditional’s result