260 likes | 394 Views
CASE/Re-factoring and program slicing. CASE tool construction. File level - programming environment Language level - program representation, compiling, testing
E N D
CASE/Re-factoringand program slicing COMP 319
CASE tool construction • File level - programming environment • Language level - program representation, compiling, testing • Work flow level - stages in the software engineering process itself: specification, design, development, verification, validation, management. COMP319
Program Language Level Tool construction at the language level exploits form – which is usually either: • Grammar – to capture the notion of text based instructions • Graph – to deal with the concept of sequence
Refactoring • Why CASE? • not alter functionality (must be correct) • cover all instances (i.e. be complete) • Keep code tidy and to standard format • Be quick COMP319
Re-factor types • Encapsulate field public intgetLength() { return(length); } • Re-name method, field • String pw • - • String password COMP319
Generalisation of type class Customer { } class Person { } class Customer extends Person { } COMP319
Code breaking up re-factors • Extract method void setLength(int length) { if (length<0) { throw (new BadArgumentException()); } this.length=length; } void validateLength(int length) { if (length<0) { throw (new BadArgumentException()); } } void setLength(int length) { validateLength(length); this.length=length; } COMP319
Graphs • A diagram depicting a network • Points at the end of arcs • Nodes at the junction of arcs • Regions enclosed by arcs • Used for representing: • Solid figures (vertices, edges, faces) • Electrical circuits • Relationships between entities • Graph or network theory COMP319
Dependence graphs • All computing systems have dependencies • Control dependence • 1 method calling another • Data dependence • 1 expression effecting another • A=B*2 • C=A*4 • Control/data dependence • If (age<=18) { println(“Age invalid”); COMP319
Program dependency graphs • Term appears in a paper by Kuck, Muraoka & Chen (1981) although the idea is in Turing’s early description of “algorithms” in 1936 • Captures sequence/time between entities (compare connection/distance) • Control Dependence • Data Dependence COMP319
Dependency graph usage • Optimisation • Multiple independent statements can run in parallel • Code that never runs can be removed • Boolean skip=true; • If (!skip) then • Loop invariance • For (k=1;k<max_items;k++) { • sum=sum+a*b; • } COMP319
Program slicing • Interactive method for • Debugging • Program understanding • Program maintenance • Program reduction technique (highlighter) • A demonstration of SDG allowing: • Control flow analysis • Data flow analysis COMP319
How does it work • Choose v a variable or set of variables • Choose n a point of interest • Using the dependence graph the slice v at n is constructed • The slice v at n can be compiled and studied separately • Slices may be forward or backward from n COMP319
Backward Slicing Original program x = 1; y = 2; z = y-2; r = x; z = x+y; /* the slice point is the end of the program */. Backward Slice x = 1;y = 2;z = x+y;
Debugging with a slice Pass = 0 ; Fail = 0 ; Count = 0 ;while (!eof()) { TotalMarks=0; scanf("%d",Marks); if (Marks >= 40) Pass = Pass + 1; if (Marks < 40) Fail = Fail + 1; Count = Count + 1; TotalMarks = TotalMarks+Marks ; }printf("Out of %d, %d passed and %d failed\n", Count, Pass, Fail) ;average = TotalMarks/Count; /* point of interest */printf("The average was %d\n",average) ;PassRate = Pass/Count*100 ;printf("This is a pass rate of %d\n",PassRate) ; COMP319
Bug location with backward slicing while (!eof()) { TotalMarks = 0;scanf("%d",Marks); Count = Count + 1;TotalMarks = TotalMarks+Marks;}average = TotalMarks/Count;printf("The average was %d\n",average) ; COMP319
Forward Slicing Original program x = 1; /* considering changing this line */y = 3;p = x + y ;z = y -2 ;if(p==0)r++ ; Forward Slice /* Change to first line will affect */p = x + y ;if(p==0)r++ ; COMP319
Maintenance - Example n = 0; product = 1; sum = 1;scanf("%d",&x) ;while (x >= 0) { sum = sum + x; product = product * x ; n = n + 1; scanf("%d",&x);}average = (sum - 1) / n ;printf("The total is %d\n",sum) ;printf("The product is %d\n",product) ;printf("The average is %d\n",average) ; COMP319
Maintenance – backward slice sum = 1;scanf("%d",&x) ;while (x >= 0) { sum = sum + x; scanf("%d",&x);}printf("The total is %d\n",sum) ; COMP319
Maintenance – forward slice n = 0; product = 1; sum = 0;scanf("%d",&x) ;while (x >= 0) { sum = sum + x; /* AFFECTED */ product = product * x ; n = n + 1;scanf("%d",&x);}Average = (sum - 1) / n ; /* AFFECTED */printf("The total is %d\n",sum) ; /* AFFECTED */printf("The product is %d\n",product) ;printf("The average is %d\n",average) ; /* AFFECTED */ COMP319
Types of slicing • Static – described above. Slices are constructed at compile time • Dynamic slicing where slices are constructed once the input is known • Conditional slicing done at breakpoints during execution • Inter-modular slicing - complex systems COMP319
The Horowitz, Prins, & Rep (HPR) algorithm (merging) • Step 1. Determine changed and preserved slices e.g. adding a diameter calculation • Step 2. Form the merged graph. Using the idea of ‘graph union’ • Step 3. Test for interference i.e the merged graph preserves all the slices of all the variants. • Step 4. Construct source from the merged graph COMP319
Why richer constructs? • More useful slicing • SDG and inter-module slicing • SDG from parse trees • Other methods of generating an SDG • Calls and variable scope handling • Pointers, aliases, classes COMP319
JSlice • Java slicing software developed by National University of Singapore • Performs dynamic slicing • Uses a compressed trace which records • Flow control instructions • Data manipulation • JVM • Kaffe • Clean room implementation of Java COMP319