Extract Slice Refactoring. Rani Ettinger, Programming Tools Group, May 19th, 2003

Similar documents
Static Slicing. Software Maintenance

Final Exam. COMP Summer I June 26, points

1. What does the following code fragment write to the monitor?

Automating Big Refactorings for Componentization and the Move to SOA

Methods. CSE 114, Computer Science 1 Stony Brook University

Testing Methods: White Box Testing II

Tour of common optimizations

CSE 143 SAMPLE MIDTERM

Source Code Rejuvenation is not Refactoring

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

On the separation of queries from modifiers

Principles of Compiler Design

Data-Flow Analysis Foundations

6. Operatoren. 7. Safe Programming: Assertions. Table of Operators. Table of Operators - Explanations. Tabular overview of all relevant operators

8/27/2016. ECE 120: Introduction to Computing. Graphical Illustration of Modular Arithmetic. Representations Must be Unambiguous

Lecture 11. Example 1. Introduction. Method definition and invocation. Parameter passing: value vs. reference parameters. Scope and Lifetime.

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Parallel Programming. Easy Cases: Data Parallelism

Lecture 5: Methods CS2301

Small changes to code to improve it

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

COMPUTER SCIENCE TRIPOS

CSE 143 SAMPLE MIDTERM SOLUTION

Parallel prefix scan

On the separation of queries from modifiers. Ran Ettinger, IBM Research Haifa CREST Open Workshop, University College London 24 January 2011

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Question: Total Points: Score:

1 Overview. 2 Basic Program Structure. 2.1 Required and Optional Parts of Sketch

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Class 6. Review; questions Assign (see Schedule for links) Slicing overview (cont d) Problem Set 3: due 9/8/09. Program Slicing

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

ELEC 876: Software Reengineering

CSC 1051 Arrays - Review questions

Lecture 5: Inheritance

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

PROPER TECHNIQUE OF SOFTWARE INSPECTION USING GUARDED COMMAND LANGUAGE

CS 12 Fall 2003 Solutions for mid-term exam #2

Lecture Notes on Static Single Assignment Form

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

CSE Qualifying Exam, Spring February 2, 2008

Formalizing Software Refactoring in the Distributed Environment by aednlc Graph Grammar

User Interface Programming OOP/Java Primer. Step 3 - documentation

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

slicing An example of slicing Consider the following example:

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

School of Computer Science. Scheme Flow Analysis Note 2 4/27/90. Useless-Variable Elimination. Olin Shivers.

Computer Science E-119 Practice Midterm

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

CS115 Principles of Computer Science

Fall 2015 CSE Qualifying Exam Core Subjects

Slicing. Rupesh Nasre. CS6843 Program Analysis IIT Madras Jan 2016

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Hoare Logic: Proving Programs Correct

Chapter 6 Methods. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

Automatically Generating Refactorings to Suppport API Evolution

Lecture 04 FUNCTIONS AND ARRAYS

Motivation. Both human- and computer-generated programs sometimes contain data-flow anomalies.

Programming Paradigms Written Exam (6 CPs)

An Assembler for the MSSP Distiller Eric Zimmerman University of Illinois, Urbana Champaign

6. Control Statements II

CS 2505 Computer Organization I

CSE 373: Data Structures and Algorithms. Asymptotic Analysis. Autumn Shrirang (Shri) Mare

Operational Semantics of Cool

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Prototype Environment for Refactoring Clean Programs

OO Design Principles

Living and Working with Aging Software. Ralph Johnson. University of Illinois at Urbana-Champaign

(Not Quite) Minijava

On Meaning Preservation of a Calculus of Records

True or False (14 Points)

Static rules of variable scoping in Erlang

Concurrency Abstractions in C#

Refactoring Framework for Instance Code Smell Detection

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Review of the syntax and use of Arduino functions, with special attention to the setup and loop functions.

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Refactoring. Section (JIA s) OTHER SOURCES

Lexical Considerations

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Chapter 5 Methods / Functions

COMP-520 GoLite Tutorial

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

COURSE 11 DESIGN PATTERNS

A Basis for AspectJ Refactoring

COMPILER DESIGN - CODE OPTIMIZATION

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Programming II. Modularity 2017/18

Transcription:

Extract Slice Refactoring Rani Ettinger, Programming Tools Group, May 19th, 2003

Outline Extract Slice Refactoring: Motivation Example Mechanics Correctness issues: Behaviour Preservation Limitations and Preconditions

Extract Slice You have a method that does too much. Among others, it computes some value using a computation that deserves a method of its own. Extract the computation into a method whose name explains the purpose of the computation.

Extract Slice Example (Before) void sum_and_prod() { int sum = 0; prod = 1; int i = 0; while (i<array.length) { sum += array[i]; prod *= array[i]; i++; System.out.println(sum); System.out.println(prod);

Extract Slice Example (After, Extracted Method) int computesum() { int result = 0; int i = 0; while (i<array.length) { result += array[i]; i++; return result;

Extract Slice Example (After, Source Method) void sum_and_prod() { int sum = computesum(); prod = 1; i = 0; while (i<array.length) { prod *= array[i]; i++; System.out.println(sum); System.out.println(prod);

Extract Slice - Motivation Decompose (de-fuse?) entangled computations Enhance code reusability Enhance code clarity Avoid Extract Method s Code Fragment restriction Introduce aspects

Extract Slice - Mechanics In a method, say f, look for a variable, say v of type T, that is assigned with the result of a computation If the computation s statements are clearly consecutive, Extract Method may be used instead Create a new method, and name it after the intention of the computation For example, computev - for the computation of v If v is defined locally in f, define the target method to return a value of type T

Extract Slice Mechanics (2) Identify all the statements that contribute to the computation of v, i.e. the backward slice with respect to the slicing criterion <s, v> where s uses the final value of v Copy the extracted code to the new target method again, if v is defined locally in f, and thus redefined locally in computev, add a return v statement at the end of the target method. Then you may substitute the name v in computev with its local meaning: result

Extract Slice Mechanics (3) Scan the extracted code for references to any variables that are parameters to f. These should be parameters to computev also Check preconditions

Extract Slice Mechanics (4) Look to see which of the extracted statements are no longer needed in f and delete those Do not delete a statement if it defines or assigns to a variable that is still needed for other purposes in f, excluding v itself, which is computed now by the computev method Do not delete a statement if it controls the execution of other statements, like if and while, and the corresponding break and continue statements, if at least one of the controlled statements (while body, then branch or else branch) was not deleted If deleting a statement will break the program (e.g. the only statement in the then branch of an if statement that also has an else branch), replace it with the empty statement

Extract Slice Mechanics (5) Replace the extracted code in f with a call to the target method Finally, if v is defined locally in the source method, add the method call in the initialisation part of its definition, and you can now consider using the Replace Temp with Query refactoring, to replace uses of v with calls to the new method. Otherwise, the method call can be inserted anywhere between the start of the source method, and the first use of v, and you can now consider using the Separate Query from Modifier refactoring, to move the method call from f to all its call sites Compile and test

Behaviour Preservation Opdyke s 7 properties Properties 1-6: syntactic correctness Property 7: semantic correctness: Semantically Equivalent References and Operations [Opdyke, William F. Refactoring Object-Oriented Frameworks. Ph.D. Dissertation, University of Illinois at Urbana-Champaign, 1992]

Semantically Equivalent References and Operations Definition: let the external interface to the program be via the function main. If the function main is called twice (once before and once after the refactoring) with the same set of inputs, the resulting set of output values must be the same [Opdyke, William F. Refactoring Object-Oriented Frameworks. Ph.D. Dissertation, University of Illinois at Urbana-Champaign, 1992]

Semantically Equivalent References and Operations(2) Explanation: Imagine that a circle is drawn around the parts of a program affected by a refactoring. The behavior as viewed from outside the circle does not change. For some refactorings, the circle surrounds most or all of the program the key idea is that the results (including side effects) of operations invoked and references made from outside the circle do not change, as viewed from outside the circle [Opdyke, William F. Refactoring Object- Oriented Frameworks. Ph.D. Dissertation, University of Illinois at Urbana- Champaign, 1992]

Limitations: Do Not Extract Return Statements void sum_and_prod() { int sum = 0, prod = 1, i = 0; if (array.length == 0) return; while (i<array.length) { sum += array[i]; prod *= array[i]; i++; System.out.println(sum); System.out.println(prod);

Limitations: Do Not Extract Global Modifications void sum_and_prod() { int sum = 0, prod = 1, i = 0; array = initarray(); while (i<array.length) { sum += array[i]; prod *= array[i]; i++; System.out.println(sum); System.out.println(prod);

Limitations: Must Delete All Intermediate References void sum_and_prod() { int sum = 0, midsum, prod = 1, i = 0; while (i<array.length) { sum += array[i]; if (i==array.length/2) midsum = sum; prod *= array[i]; i++; System.out.println(sum); System.out.println(midSum); System.out.println(prod);

Limitations: Do Not Extract Input Statements void sum_and_prod(inputdatastream in) { int sum = 0, prod = 1, i = 0; int size = System.in.readInt(); while (i<size) { sum += array[i]; prod *= array[i]; i++; System.out.println(sum); System.out.println(prod);

Limitations: Do Not Extract Output Statements void sum_and_prod(outputdatastream out) { int sum = 0, prod = 1, i = 0; while (i<array.length) { sum += array[i]; out.print(sum); prod *= array[i]; out.print(prod); i++; System.out.println(sum); System.out.println(prod);

Summary of Limitations and Preconditions Target method name is valid and fresh The extracted code must be clear of any: input or output statements return statements Modifications of global-scoped variables (other than v itself, if it is global at all) All references to intermediate values of v were extracted and deleted