1 / 15

On the separation of queries from modifiers

On the separation of queries from modifiers. Ran Ettinger, IBM Research – Haifa COST Action IC0701 9 th MC and WG Meeting Darmstadt, Germany 29 February 2012. Separate Query from Modifier (SQfM). A refactoring technique by Martin Fowler*

rogerramos
Download Presentation

On the separation of queries from modifiers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. On the separation of queries from modifiers Ran Ettinger, IBM Research – Haifa COST Action IC0701 9th MC and WG Meeting Darmstadt, Germany 29 February 2012

  2. Separate Query from Modifier (SQfM) • A refactoring technique by Martin Fowler* • “You have a method that returns a value but also changes the state of an object.” • “Create two methods, one for the query and one for the modification.” • Inspired by Bertrand Meyer’s Command Query Separation (CQS) • This talk: • Outline of a first algorithm to support the automation of this refactoring • Based on program slicing and sliding, with reference to other refactoring techniques • A prototype tool integrated into Eclipse • Open source implementation in WALA (http://wala.sourceforge.net) • Developed by Alex Libov, Eli Kfir and Daniel Lemel (Technion, Israel Institute of Technology) • Contributions by Dima Rabkin and Vlad Shumlin (Haifa University) • Further investigation in Hemachandran Kalaimani’s MSc thesis (Leicester University) • Based on a slicer for Java by Stephen J. Fink (IBM Research) and the WALA contributors * See http://www.refactoring.com/catalog/separateQueryFromModifier.html and http://sourcemaking.com/refactoring/separate-query-from-modifier

  3. Fowler’s Example (Before SQfM) String foundMiscreant(String[] people) { for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); return"Don"; } if (people[i].equals("John")) { sendAlert(); return"John"; } } return""; } void checkSecurity(String[] people) { String found = foundMiscreant(people); someLaterCode(found); }

  4. Fowler’s Example (After SQfM) String foundPerson(String[] people) { for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { return"Don"; } if (people[i].equals("John")) { return"John"; } } return""; } void sendAlert(String[] people) { for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); return; } if (people[i].equals("John")) { sendAlert(); return; } } } void checkSecurity(String[] people) { sendAlert(people); String found = foundPerson(people); someLaterCode(found); }

  5. Fowler’s Example (Beyond SQfM) String foundPerson(String[] people) { for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { return"Don"; } if (people[i].equals("John")) { return"John"; } } return""; } void sendAlert(String[] people) { if (!foundPerson(people).equals("")) sendAlert(); } void checkSecurity(String[] people) { sendAlert(people); String found = foundPerson(people); someLaterCode(found); }

  6. Proposed Mechanics for SQfM • Prepare the method for sliding by ensuring it has a single return statement. • Compile and test. • Perform sliding on the returned value. • Perform Extract Method on the slice (Q). • [Optional] Perform Inline Temp on the result of Q. • Compile and test. • Perform Extract Method on the co-slice (M). • Undo the preparatory step (in Q and M). • Compile and test. • Perform Inline Method on the refactored version of the selected method. • Compile and test.

  7. Step 1: Single Return String foundMiscreant(String[] people) { String result; body:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); result = "Don"; break body; } if (people[i].equals("John")) { sendAlert(); result = "John"; break body; } } result = ""; } return result; }

  8. Step 2: Sliding (of result) String foundMiscreant(String[] people) { String result; slice:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { result = "Don"; break slice; } if (people[i].equals("John")) { result = "John"; break slice; } } result = ""; } co_slice:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); break co_slice; } if (people[i].equals("John")) { sendAlert(); break co_slice; } } } return result; }

  9. Step 3: Extract Method (Q) String foundPerson(String[] people) { String result; slice:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { result = "Don"; break slice; } if (people[i].equals("John")) { result = "John"; break slice; } } result = ""; } return result; } String foundMiscreant(String[] people) { String result = foundPerson(people); co_slice:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); break co_slice; } if (people[i].equals("John")) { sendAlert(); break co_slice; } } } return result; }

  10. Step 4: Inline Temp (result) String foundPerson(String[] people) { String result; slice:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { result = "Don"; break slice; } if (people[i].equals("John")) { result = "John"; break slice; } } result = ""; } return result; } String foundMiscreant(String[] people) { co_slice:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); break co_slice; } if (people[i].equals("John")) { sendAlert(); break co_slice; } } } return foundPerson(people); }

  11. Step 5: Extract Method (M) String foundPerson(String[] people) { String result; slice:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { result = "Don"; break slice; } if (people[i].equals("John")) { result = "John"; break slice; } } result = ""; } return result; } void sendAlert(String[] people) { co_slice:{ for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); break co_slice; } if (people[i].equals("John")) { sendAlert(); break co_slice; } } } } String foundMiscreant(String[] people) { sendAlert(); return foundPerson(people); } void checkSecurity(String[] people) { String found = foundMiscreant(people); someLaterCode(found); }

  12. Step 6: Undo Step 1 (in Q and M) String foundPerson(String[] people) { for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { return"Don"; } if (people[i].equals("John")) { return"John"; } } return""; } void sendAlert(String[] people) { for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); return; } if (people[i].equals("John")) { sendAlert(); return; } } } String foundMiscreant(String[] people) { sendAlert(); return foundPerson(people); } void checkSecurity(String[] people) { String found = foundMiscreant(people); someLaterCode(found); }

  13. Step 7: Inline Method String foundPerson(String[] people) { for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { return"Don"; } if (people[i].equals("John")) { return"John"; } } return""; } void sendAlert(String[] people) { for (int i=0; i<people.length; i++) { if (people[i].equals("Don")) { sendAlert(); return; } if (people[i].equals("John")) { sendAlert(); return; } } } void checkSecurity(String[] people) { sendAlert(people); String found = foundPerson(people); someLaterCode(found); }

  14. Conditions for Behavior Preservation • The two new method names must be legal and cause no conflict • The code of Q must be free of side effects • Otherwise, can some measures be taken to prevent the effects? • Further SQfM of called methods might be needed, requiring further user interaction • Legal selection of a method • It should be non-void and with side effects (or M would be empty) • If it participates in overriding special treatment is needed • Example: A Java Iterator’s next() method

  15. Some Challenges • How not to fail when the Query has side effects • Idea: assuming Q will follow M, try to reuse some of M’s results in Q instead of re-computing them; so it is the slice of the side effects that will be extracted, instead of that of the returned value • How to minimize code duplication, correctly • which extraction technique (of Q or of M) should be preferred? • How not to fail in the final (Inline Method) step • When the call is inside a loop’s condition the Modifier’s invocation location is non-trivial • The Eclipse “Inline” treatment needs improvement

More Related