Download presentation
Presentation is loading. Please wait.
Published byJessie Shepherd Modified over 9 years ago
1
Toward an Implementation of the “Form Template Method” Refactoring Nicolas Juillerat University of Fribourg Switzerland
2
27/09/07SCAM'072 1.Problem definition public void rotateAt(Point center, double amount) { translate(mult(center, -1 + 5)); rotate(amount, center); translate(center); normalize(amount); } public void skewAt(Point center, double amount) { translate(mult(center, -1 - 4)); amount = skew(amount); translate(center); normalize(amount); } public void templateMethod( Point center, double amount) { translate(mult(center, d1())); amount = d2(amount, center); translate(center); normalize(amount); }
3
27/09/07SCAM'073 2. Challenges How to compare two code snippets? –Use clone detection techniques? –Yes, but… small differences The matching order is important Renamed variables are more difficult to handle
4
27/09/07SCAM'074 2. Challenges ABCDEABCDE ABDCEABDCE ABCDEABCDE Matching order Clone detection 4 clones
5
27/09/07SCAM'075 2. Challenges ABCDEABCDE ABDCEABDCE ABCDEABCDE ABDCEABDCE ABCDEABCDE ABDCEABDCE Matching order Template method (2 solutions)
6
27/09/07SCAM'076 2. Challenges Renamed variables int x = z * 2; print(x); store(x); int y = x - 4; return y; int y = z * 2 print(y); store(y); y++; return y; int x = z * 2; print(x); store(x); int y = x - 4; return y; int y = z * 2 print(y); store(y); y++; return y; x --- x, y --- y, z --- z 2 matches x --- y, y --- x, z --- z 3 matches
7
27/09/07SCAM'077 2. Challenges Renamed variables int x = z * 2; print(x); store(x); int y = x - 4; return y; int y = z * 2 print(y); store(y); y++; return y; int x = z * 2; print(x); store(x); int y = x - 4; return y; int y = z * 2 print(y); store(y); y++; return y; We need to know what variables are matched before we can match statements. But we need to know what statements are matched before we can match variables Reason: unlike with clone detection, variable matching must be the same for multiple matched statements
8
27/09/07SCAM'078 2. Challenges Method extraction –Known problem, but the template method can only be formed if all differences can be extracted AXDFHKLAXDFHKL BXCFMKPBXCFMKP e1 X e2 F e3 K e4 e1 X e2 F H/M K e4 Successfully extracted Failed to extract. No template method can be formed
9
27/09/07SCAM'079 2. Challenges Method extraction –Furthermore, we have to extract pairs of different methods AXDFHKLAXDFHKL BXCFMKPBXCFMKP e1 X e2 F e3 K e4 void D(int, String); float C(int, boolean); float e2(int, String, boolean); Merging of arguments / results
10
27/09/07SCAM'0710 3. Proposed solutions
11
27/09/07SCAM'0711 3. Proposed solutions Variable matching (before matched statements are known) –Based on Maximum Matching of Bipartite Graphs (Heuristic, used to supply a proposal) Not in paper (ongoing work) Statement comparison –Post-order AST traversal ( Tokens list) –Longest Common Subsequence (LCM) Or modified LZ77 (data compression algorithm) –Common ancestor (tree) And various other tree algorithms
12
27/09/07SCAM'0712 3. Proposed solutions Method Extraction –If impossible, various tricks to make the extraction possible Divide the method to extract into multiple methods Extend a method with matched statements –Some duplications remains Control flow to dataflow conversion Etc –More freedom than with method extraction alone
13
27/09/07SCAM'0713 4. Discussion/Conclusion
14
27/09/07SCAM'0714 4. Discussion/Conclusion Algorithms: AST Traversal List Longest Common SubsequenceLongest Common Subsequence Common Ancestor (tree)Common Ancestor (tree) Maximum matching of bipartite graphsMaximum matching of bipartite graphs Control flow to data flow conversionControl flow to data flow conversion Data/Control flow analysis LZ77LZ77 … Existing techniques / abstractions: Graph transformations Graph rewriting languages AST Traversals/Rewriting Scripting languages Code queries … Questions: Are the existing techniques suitable for really complex refactorings? Isn’t a general purpose language (+ suitable library) more adequate?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.