Ch 4: Program Comprehension

 
Ch 4: Program Comprehension
Part 3
Program 
Analysis 
Program
 
Slicing
 
O
u
t
l
i
n
e
2
1.
I
n
t
r
o
d
u
c
t
i
o
n
2.
S
t
a
t
i
c
 
S
l
i
c
i
n
g
3.
Refactoring
 
I
n
t
r
o
d
u
c
t
i
o
n
The size and complexity of a software today gets harder to understand,
maintain and test
 You might have had questions like:
 If I change this statement, what pieces of the program are going to be
affected?
 Where are the values that flow into this statement coming from?
 How can I limit the functionality to only what I need?
 
I
n
t
r
o
d
u
c
t
i
o
n
Goals:-
-
Debug your thousands lines of code easily by reducing the
complexity of the program.
-
Write a robust program before testing your code.
-
Save your regression testing time by limiting the tests to only
those that exercise the changed code.
 
I
n
t
r
o
d
u
c
t
i
o
n
How ?
-“Break larger code into smaller pieces”
During program design, some known decomposition techniques
are
-Information hiding and data abstraction
Unlike most other methods, slicing is applied to programs after
they are written, and is therefore useful in maintenance rather
than design
 
W
h
a
t
 
i
s
 
p
r
o
g
r
a
m
 
s
l
i
c
i
n
g
?
Program slice is a decomposition technique that
extracts statements relevant to a particular computation
from a program
Program slices was first introduced by Mark Weiser
(1980) are known as executable backward static slices
Program slicing describes a mechanism which allows
the automatic generation of a slice
 
W
h
a
t
 
i
s
 
p
r
o
g
r
a
m
 
s
l
i
c
i
n
g
?
Slicing criterion <s , v>
- Where s specifies a location (statement s) and v specifies a
variable (v)
All statements affecting or affected by the variables
mentioned in the slicing criterion becomes a part of the slice
 
W
h
a
t
 
i
s
 
p
r
o
g
r
a
m
 
s
l
i
c
i
n
g
?
Program slice must satisfy the following conditions
- Slice S(V,n) must be derived from P by deleting
statements from P
- Slice S(V,n) must be syntactically correct
- For all executions of P, the value of V in the execution of
S(V,n) just before the location n must be the same value of
V in the execution of the program P just before location n
 
P
r
i
n
c
i
p
l
e
 
o
f
 
d
e
p
e
n
d
e
n
c
e
s
Data dependence
-Definition of variable v at statement s1 reaches a use
of v at statement s2
Control dependence
-Conditional statement controls whether or not the
current statement is executed
 
D
a
t
a
 
d
e
p
e
n
d
e
n
c
e
 
C
o
n
t
r
o
l
 
d
e
p
e
n
d
e
n
c
e
 
P
r
o
g
r
a
m
 
S
l
i
c
i
n
g
12
E
x
t
r
a
c
t
 
a
n
 
e
x
e
c
u
t
a
b
l
e
 
s
u
b
s
e
t
 
o
f
 
a
p
r
o
g
r
a
m
 
t
h
a
t
 
(
p
o
t
e
n
t
i
a
l
l
y
)
 
a
f
f
e
c
t
s
 
t
h
e
v
a
l
u
e
s
 
a
t
 
a
 
p
a
r
t
i
c
u
l
a
r
 
p
r
o
g
r
a
m
 
l
o
c
a
t
i
o
n
Slicing
 
criterion
 
=
 program
 
location
 
+
 
variable
An observer 
focusing 
on 
the slicing 
criterion 
cannot
 
distinguish
 
a
 
run
 
of
 
the
 program 
from
 
a 
run
 
of
 
the
 
slice
 
E
x
a
m
p
l
e
va
r
 
n
 
=
 
readInpu
t
()
;
va
r
 
i
 
=
 
1
;
va
r
 
su
m
 
=
 
0
;
va
r
 
pro
d
 
=
 
1
;
whil
e
 
(
i
 
<
=
 
n
)
 
{
su
m
 
=
 
su
m
 
+
 
i
;
pro
d
 
=
 
pro
d
 
*
 
i
;
i
 
=
 
i
 
+
 
1
;
}
console.log(sum); 
c
o
n
s
o
l
e
.
lo
g
(
pro
d
)
;
 
E
x
a
m
p
l
e
va
r
 
n
 
=
 
readInpu
t
()
;
va
r
 
i
 
=
 
1
;
va
r
 
su
m
 
=
 
0
;
va
r
 
pro
d
 
=
 
1
;
whil
e
 
(
i
 
<
=
 
n
)
 
{
su
m
 
=
 
su
m
 
+
 
i
;
pro
d
 
=
 
pro
d
 
*
 
i
;
i
 
=
 
i
 
+
 
1
;
}
console.log(sum); 
c
o
n
s
o
l
e
.
lo
g
(
pro
d
)
;
S
l
i
c
e
 
f
o
r
 
v
a
l
u
e
o
f
 
s
u
m
 
a
t
 
t
h
i
s
s
t
a
t
e
m
e
n
t
?
 
E
x
a
m
p
l
e
va
r
 
n
 
=
 
readInpu
t
()
;
va
r
 
i
 
=
 
1
;
va
r
 
su
m
 
=
 
0
;
va
r
 
pro
d
 
=
 
1
;
whil
e
 
(
i
 
<
=
 
n
)
 
{
su
m
 
=
 
su
m
 
+
 
i
;
pro
d
 
=
 
pro
d
 
*
 
i
;
i
 
=
 
i
 
+
 
1
;
}
console.log(sum);
console.log(prod);
S
l
i
c
e
 
f
o
r
 
v
a
l
u
e
o
f
 
s
u
m
 
a
t
 
t
h
i
s
s
t
a
t
e
m
e
n
t
?
 
E
x
a
m
p
l
e
va
r
 
n
 
=
 
readInpu
t
()
;
va
r
 
i
 
=
 
1
;
va
r
 
su
m
 
=
 
0
;
va
r
 
pro
d
 
=
 
1
;
whil
e
 
(
i
 
<
=
 
n
)
 
{
su
m
 
=
 
su
m
 
+
 
i
;
pro
d
 
=
 
pro
d
 
*
 
i
;
i
 
=
 
i
 
+
 
1
;
}
console.log(sum);
console.log(prod);
S
l
i
c
e
 
f
o
r
 
v
a
l
u
e
o
f
 
p
r
o
d
 
a
t
 
t
h
i
s
s
t
a
t
e
m
e
n
t
 
E
x
a
m
p
l
e
va
r
 
n
 
=
 
readInpu
t
()
;
va
r
 
i
 
=
 
1
;
va
r
 
su
m
 
=
 
0
;
va
r
 
pro
d
 
=
 
1
;
whil
e
 
(
i
 
<
=
 
n
)
 
{
su
m
 
=
 
su
m
 
+
 
i
;
pro
d
 
=
 
pro
d
 
*
 
i
;
i
 
=
 
i
 
+
 
1
;
}
console.log(sum); 
c
o
n
s
o
l
e
.
lo
g
(
pro
d
)
;
S
l
i
c
e
 
f
o
r
 
v
a
l
u
e
o
f
 
n
 
a
t
 
t
h
i
s
s
t
a
t
e
m
e
n
t
 
W
h
y
 
D
o
 
W
e
 
N
e
e
d
 
S
l
i
c
i
n
g
?
V
a
r
i
o
u
s
 
a
p
p
l
i
c
a
t
i
o
n
s
,
 
e
.
g
.
Debugging
:
 
Focus
 
on
 
parts
 of 
program 
relevant 
for
 
a
 
bug
Program
 understanding
:
 
Which 
statements 
influence
 
this
 
statement?
Change
 
impact
 
analysis
:
 
Which
 
parts
 
of
 
a 
program
 
are
 
affected
 
by
 
a
 
change?
 
What
 
should 
be
 
retested?
Parallelization
:
 
Determine
 
parts
 
of
 program 
that 
can
 
be
 
computed
 
independently
 
of
 
each
 
other
 
S
l
i
c
i
n
g
:
 
O
v
e
r
v
i
e
w
F
o
r
w
a
r
d
 
v
s
.
 
b
a
c
k
w
a
r
d
Backward 
slice
 
(our
 
focus):
 
Statements
 
that 
influence
 
the
 
slicing
 
criterion
Forward
 
slice:
 
Statements
 
that
 
are
 
influenced
 
by
the
 
slicing
 
criterion
S
t
a
t
i
c
 
v
s
.
 
d
y
n
a
m
i
c
Statically
 
computing
 
a
 
minimum
 
slice
 
is 
undecidable
Dynamically
 
computed
 
slice
 
focuses
 
on
 
particular
execution/input
 
K
i
n
d
s
 
o
f
 
p
r
o
g
r
a
m
 
s
l
i
c
i
n
g
Static slicing
using static information(a source program)
Dynamic slicing
using dynamic information(an execution trace)
 
S
t
a
t
i
c
 
P
r
o
g
r
a
m
 
S
l
i
c
i
n
g
 
S
t
a
t
i
c
 
P
r
o
g
r
a
m
 
S
l
i
c
i
n
g
V
a
r
i
o
u
s
 
a
l
g
o
r
i
t
h
m
s
 
t
o
 
c
o
m
p
u
t
e
 
s
l
i
c
e
s
H
e
r
e
:
 
G
r
a
p
h
 
r
e
a
c
h
a
b
i
l
i
t
y
 
p
r
o
b
l
e
m
b
a
s
e
d
 
o
n
 
p
r
o
g
r
a
m
 
d
e
p
e
n
d
e
n
c
e
 
g
r
a
p
h
 
P
r
o
g
r
a
m
 
D
e
p
e
n
d
e
n
c
e
 
G
r
a
p
h
D
i
r
e
c
t
e
d
 
g
r
a
p
h
 
r
e
p
r
e
s
e
n
t
i
n
g
 
t
h
e
 
d
a
t
a
 
a
n
d
c
o
n
t
r
o
l
 
d
e
p
e
n
d
e
n
c
e
s
 
b
e
t
w
e
e
n
s
t
a
t
e
m
e
n
t
s
 
Nodes
:
Q
 
Statements
Q
 
Predicate
 
expressions
 
Edges
:
Q
 
Data
 flow 
dependences:
 
One
 
edge
 
for
 
each 
definition-use
 
pair
Q
 
Control
 
flow
 
dependences
 
V
a
r
i
a
b
l
e
 
D
e
f
i
n
i
t
i
o
n
 
a
n
d
 
U
s
e
A
 
v
a
r
i
a
b
l
e
 
d
e
f
i
n
i
t
i
o
n
 
f
o
r
 
a
 
v
a
r
i
a
b
l
e
 
v
 
i
s
a
 
b
a
s
i
c
 
b
l
o
c
k
 
t
h
a
t
 
a
s
s
i
g
n
s
 
t
o
 
v
Q
 
v
 
can
 
be
 
a
 
local
 
or
 
global
 
variable,
 
parameter, 
or
 
property
A
 
v
a
r
i
a
b
l
e
 
u
s
e
 
f
o
r
 
a
 
v
a
r
i
a
b
l
e
 
v
 
i
s
 
a
b
a
s
i
c
 
b
l
o
c
k
 
t
h
a
t
 
r
e
a
d
s
 
t
h
e
 
v
a
l
u
e
 
o
f
 
v
Q
 
In
 
conditions,
 
computations,
 
output,
 
etc.
 
D
e
f
i
n
i
t
i
o
n
-
C
l
e
a
r
 
P
a
t
h
s
A
 
d
e
f
i
n
i
t
i
o
n
-
c
l
e
a
r
 
p
a
t
h
 
f
o
r
 
a
 
v
a
r
i
a
b
l
e
 
v
 
i
s
a
 
p
a
t
h
 
n
1
,
 
.
.
,
 
n
k
i
n
 
t
h
e
 
C
F
G
 
s
u
c
h
 
t
h
a
t
n
1
 
is
 
a
 
variable 
definition
 
for
 
v
n
k
 
is
 
a
 
variable
 
use
 
for
 
v
No
 
n
i
 
(
1
 
<
 
i
 
 
k
)
 
is 
a
 
variable 
definition
 
for
 
v
Q
 
n
k
 
may
 
be
 
a
 
variable
 definition if 
each
 
assignment 
to
 
v
 
occurs
 after
 
a
 
use
 
D
e
f
i
n
i
t
i
o
n
-
U
s
e
 
P
a
i
r
A 
definition-use pair 
(DU-pair) 
for
 a 
 
v
aria
b
le v is a pair of nodes 
(
d,
 
u
)
 
su
c
h  that
 
there
is
 
a
 
definition-clear
 
path
 
d,
 
..,
 
u 
 
in
 
the CFG
 
Data
 flow 
dependences:
Example
 
Data
 flow 
dependences:
Example
 
85
Data
 flow 
dependences:
Example
 
C
o
n
t
r
o
l
 
F
l
o
w
 
D
e
p
e
n
d
e
n
c
e
s
17
Post-dominator
:
Node 
n
2 
(strictly) post-dominates 
node 
n
1
(
/
= 
n
2
) 
if
 
every
 
path
 
n
1
,
 
...,
 
exit
 
in 
the control
 
flow
 
graph 
contains
 
n
2
Control
 
dependence
:
Node
 
n
2
 
is
 control-dependent
 
on
 
node
 
n
1
 
/
=
 
n
2
 
if 
Q
 
there 
e
xists 
a
 control fl
o
w
 
path
 
P
 
=
 
n
1
,
 
...,
 
n
2
 
 
where
n
2
 
post-dominates 
any
 node 
in
 
P
 
(excluding 
n
1
),
and
Q
 
n
2
 
does
 
not
 
post-dominate
 
n
1
 
Control
 
Flow
 
Dependences
:
Example
1)
(strictly) post-dominates
2)
Control
 
dependence
 
Control
 
Flow
 
Dependences
:
Example
1) (strictly) post-dominates
2) Control
 
dependence
 
Control
 
Flow
 
Dependences
:
Example
1) (strictly) post-dominates
2) Control
 
dependence: 
6 is 
Control
 
dependence on 5
                                      
7 is 
Control
 
dependence on 5
                                      8 is 
Control
 
dependence on 5
 
C
o
m
p
u
t
i
n
g
 
S
l
i
c
e
s
20
Given:
Program
 
dependence
 
graph
 
G
PD
Slicing 
criterion 
(
n, 
V 
)
, where 
n 
is 
a 
statement 
and
 
V
 
is
 
the
 
set
 
of
 variables
 
defined
 
or
 
used
 
at
 
n
Slice
 
f
or
 
(
n,
 
V
 
)
:
All
 statements from which 
n
 
is
 
reachable 
(i.e., 
all 
statements
 
on
 
which
 
n
 
depends)
 
P
r
o
g
r
a
m
 
D
e
p
e
n
d
e
n
c
e
 
G
r
a
p
h
:
 
e
x
a
m
p
l
e
1
2
3
4
5
8
9
7
6
10
 
P
r
o
g
r
a
m
 
D
e
p
e
n
d
e
n
c
e
 
G
r
a
p
h
:
 
e
x
a
m
p
l
e
1
2
3
4
5
8
9
7
6
10
 
Slice (9, {sum} )
= { n I reachable (n,9)}
= {1,2,3,5,6,7,8,9}
Program
 
Dependence
 
Graph: example
 
Ch 4: Program Comprehension
Part 4
Program 
Analysis 
Code  
Refactoring
 
Definition: 
Refactoring is a disciplined technique for restructuring
an existing body of code, altering its internal structure without
changing its external behavior.
Refactoring does not fix bugs, but it may help find bugs. It may also
reduce the further introduction of bugs by cleaning-up code.
It is an essential part of agile software development such as Extreme
Programming or incremental development.
R
e
f
a
c
t
o
r
i
n
g
:
 
w
h
a
t
 
i
s
 
i
t
?
Joey Paquet, 2006-2014
39
SOEN 6441 - Advanced Programming Practices
 
R
e
f
a
c
t
o
r
i
n
g
Definition: Refactoring modifies software to improve
its readability, maintainability, and extensibility
without changing what it actually does.
External behavior does NOT change
Internal structure is improved
The goal of refactoring is NOT to add new functionality
The goal is refactoring is to make code easier to maintain
in the future
 
Refactoring ought to be done continuously as “bad smells” are
encountered during programming.
“Bad smells” or “anti-patterns” are portions of design or code that
are characterized as potentially confusing and identifies as
refactoring targets.
More importantly, when using iterative development, a major
refactoring stage should precede the beginning of the development
of a new build. This will remove slight design problems and ease the
addition of further functionality.
R
e
f
a
c
t
o
r
i
n
g
:
 
w
h
e
n
?
Joey Paquet, 2006-2014
41
SOEN 6441 - Advanced Programming Practices
 
Refactoring is usually done to:
Improve quality
improve design quality
improve maintainability
improve extensibility
Improve sustainability of development
requires proper testing, so improves testability
helps to find bugs
Improve productivity
improve code readability & comprehensibility
simplify code structure
R
e
f
a
c
t
o
r
i
n
g
:
 
w
h
y
?
Joey Paquet, 2006-2014
42
SOEN 6441 - Advanced Programming Practices
 
W
h
y
 
d
o
 
g
o
o
d
 
d
e
v
e
l
o
p
e
r
s
 
w
r
i
t
e
 
b
a
d
s
o
f
t
w
a
r
e
?
Requirements change over time, making it hard to
update your code (leading to less optimal designs)
Time and money cause you to take shortcuts
You learn a better way to do something (the second
time you paint a room, it’s always better than the
first because you learned during the first time!)
 
Each refactoring is implemented as a small behavior-preserving
transformation.
Behavior-preservation is achieved through pre- and post-
transformation testing.
Refactoring process: test-refactor-test
R
e
f
a
c
t
o
r
i
n
g
:
 
h
o
w
?
Joey Paquet, 2006-2014
44
SOEN 6441 - Advanced Programming Practices
 
Cost Overhead: 
Refactoring is an add-on activity and therefore will
incur extra cost in form of time, effort, and resource allocation,
especially if elaborated design and code documentation is
maintained. However, when done sparingly and only on key issues,
its benefits are greater than its overhead. Automated documentation
tools, code browsing tools, refactoring tools and testing tools will
also diminish the refactoring overhead.
Requires Expertise: 
Refactoring requires some expertise and
experience and considerable effort in going through the process,
especially if proper testing is involved. However, this overhead can
be minimized by using refactoring tools and automated testing such
as with a unit testing framework.
R
e
f
a
c
t
o
r
i
n
g
:
 
d
r
a
w
b
a
c
k
s
Joey Paquet, 2006-2014
45
SOEN 6441 - Advanced Programming Practices
 
Consolidate Conditional Expression: 
You have a sequence of
conditional tests with the same result. Refactor by combining them
into a single conditional expression and extract it.
R
e
f
a
c
t
o
r
i
n
g
:
 
e
x
a
m
p
l
e
s
Joey Paquet, 2006-2014
46
SOEN 6441 - Advanced Programming Practices
double disabilityAmount() {
 
if 
(_seniority < 2)
 return 0;
 
if 
(_monthsDisabled > 12)
 return 0;
 
if 
(_isPartTime)
 return 0;
 
// compute the disability amount
double disabilityAmount() {
 
if 
(isNotEligibleForDisability())
 return 0;
 
// compute the disability amount
 
Consolidate Duplicate Conditional Fragments: 
The same
fragment of code is in all branches of a conditional expression.
Refactor by moving it outside of the expression.
R
e
f
a
c
t
o
r
i
n
g
:
 
e
x
a
m
p
l
e
s
Joey Paquet, 2006-2014
47
SOEN 6441 - Advanced Programming Practices
if (isSpecialDeal()) {
 
total = price * 0.95;
 
send();
} else {
 
total = price * 0.98;
 
send();
}
if (isSpecialDeal())
 
total = price * 0.95;
else
 
total = price * 0.98;
send();
 
Rename Method: 
The name of a method does not reveal its
purpose. Refactor it by changing the name of the method.
R
e
f
a
c
t
o
r
i
n
g
:
 
e
x
a
m
p
l
e
s
Joey Paquet, 2006-2014
48
SOEN 6441 - Advanced Programming Practices
int 
getInvCdtLmt
(){
}
int 
getInvoiceableCreditLimit
(){
}
 
Pull Up Field: 
Two subclasses have the same field. Refactor it by
moving the field to the superclass.
R
e
f
a
c
t
o
r
i
n
g
:
 
e
x
a
m
p
l
e
s
Joey Paquet, 2006-2014
49
SOEN 6441 - Advanced Programming Practices
 
Push Down Method: 
Behavior on a superclass is relevant only for
some of its subclasses. Refactor it by moving it to those subclasses.
R
e
f
a
c
t
o
r
i
n
g
:
 
e
x
a
m
p
l
e
s
Joey Paquet, 2006-2014
50
SOEN 6441 - Advanced Programming Practices
Slide Note
Embed
Share

Program slicing is a technique introduced by Mark Weiser in 1980 that extracts relevant statements from a program based on a specific computation criterion. This technique aids in reducing the complexity of software, making it easier to debug, maintain, and test. Through slicing criteria involving statements and variables, program slices are generated to facilitate program analysis, refactoring, and static slicing.

  • Program Slicing
  • Decomposition Technique
  • Software Complexity
  • Debugging
  • Maintenance

Uploaded on Feb 15, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Ch 4: Program Comprehension Part 3 Program Analysis Program Slicing

  2. Outline 1. Introduction 2. Static Slicing 3.Refactoring 2

  3. Introduction The size and complexity of a software today gets harder to understand, maintain and test You might have had questions like: If I change this statement, what pieces of the program are going to be affected? Where are the values that flow into this statement coming from? How can I limit the functionality to only what I need?

  4. Introduction Goals:- - Debug your thousands lines of code easily by reducing the complexity of the program. - Write a robust program before testing your code. - Save your regression testing time by limiting the tests to only those that exercise the changed code.

  5. Introduction How ? - Break larger code into smaller pieces During program design, some known decomposition techniques are -Information hiding and data abstraction Unlike most other methods, slicing is applied to programs after they are written, and is therefore useful in maintenance rather than design

  6. What is program slicing? Program slice is a decomposition technique that extracts statements relevant to a particular computation from a program Program slices was first introduced by Mark Weiser (1980) are known as executable backward static slices Program slicing describes a mechanism which allows the automatic generation of a slice

  7. What is program slicing? Slicing criterion <s , v> - Where s specifies a location (statement s) and v specifies a variable (v) All statements affecting or affected by the variables mentioned in the slicing criterion becomes a part of the slice

  8. What is program slicing? Program slice must satisfy the following conditions - Slice S(V,n) must be derived from P by deleting statements from P - Slice S(V,n) must be syntactically correct - For all executions of P, the value of V in the execution of S(V,n) just before the location n must be the same value of V in the execution of the program P just before location n

  9. Principle of dependences Data dependence -Definition of variable v at statement s1 reaches a use of v at statement s2 Control dependence -Conditional statement controls whether or not the current statement is executed

  10. Data dependence

  11. Control dependence

  12. Program Slicing Extract an executable subset of a program that (potentially) affects the values at a particular program location Slicing criterion = program location + variable An observer focusing on the slicing criterion cannot distinguish a run of the program from a run of the slice 12

  13. Example var n = readInput(); var i = 1; var sum = 0; var prod = 1; while (i <= n) { sum = sum + i; prod = prod* i; i = i + 1; } console.log(sum); console.log(prod);

  14. Example var n = readInput(); var i = 1; var sum = 0; var prod = 1; while (i <= n) { sum = sum + i; prod = prod* i; i = i + 1; } console.log(sum); console.log(prod); Slice for value of sum at this statement?

  15. Example var n = readInput(); var i = 1; var sum = 0; var prod = 1; while (i <= n) { sum = sum + i; prod = prod* i; i = i + 1; } console.log(sum); console.log(prod); Slice for value of sum at this statement?

  16. Example var n = readInput(); var i = 1; var sum = 0; var prod = 1; while (i <= n) { sum = sum + i; prod = prod* i; i = i + 1; } console.log(sum); console.log(prod); Slice for value of prod at this statement

  17. Example var n = readInput(); var i = 1; var sum = 0; var prod = 1; while (i <= n) { sum = sum + i; prod = prod* i; i = i + 1; } console.log(sum); console.log(prod); Slice for value of n at this statement

  18. Why Do We Need Slicing? Various applications, e.g. Debugging: Focus on parts of program relevant for a bug Program understanding: Which statements influence this statement? Change impact analysis: Which parts of a program are affected by a change? What should be retested? Parallelization: Determine parts of program that can be computed independently of each other

  19. Slicing: Overview Forward vs. backward Backward slice (our focus): Statements that influence the slicing criterion Forward slice: Statements that are influenced by the slicing criterion Static vs. dynamic Statically computing a minimum slice is undecidable Dynamically computed slice focuses on particular execution/input

  20. Kinds of program slicing Static slicing using static information(a source program) Dynamic slicing using dynamic information(an execution trace)

  21. Static Program Slicing

  22. Static Program Slicing Various algorithms to compute slices Here: Graph reachability problem based on program dependence graph

  23. Program Dependence Graph Directed graph representing the data and control dependences between statements Nodes: Q Statements Q Predicate expressions Edges: Q Data flow dependences: One edge for each definition-use pair Q Control flow dependences

  24. Variable Definition and Use A variable definition for a variable v is a basic block that assigns to v Q v can be a local or global variable, parameter, or property A variable use for a variable v is a basic block that reads the value of v Q In conditions, computations, output, etc.

  25. Definition-Clear Paths A definition-clear path for a variable v is a path n1,..,nkin the CFG such that n1is a variable definition for v nk is a variable use for v No ni (1 < i k) is a variable definition for v Q nk may be a variable definition if each assignment to v occurs after a use

  26. Definition-Use Pair A definition-use pair (DU-pair) for a variable v is a pair of nodes (d,u) such that there is a definition-clear path d,..,u in the CFG

  27. Data flow dependences: Example

  28. Data flow dependences: Example

  29. Data flow dependences: Example 85

  30. Control Flow Dependences Post-dominator: Node n2(strictly) post-dominates node n1(/= n2) if every path n1, ..., exit in the control flow graph contains n2 Control dependence: Node n2is control-dependent on node n1/= n2if Q there exists a control flow path P = n1,...,n2 where n2 post-dominates any node in P (excluding n1), and Q n2 does not post-dominate n1 17

  31. Control Flow Dependences: Example 1) (strictly) post-dominates 2) Control dependence

  32. Control Flow Dependences: Example 1) (strictly) post-dominates 2) Control dependence

  33. Control Flow Dependences: Example 1) (strictly) post-dominates 2) Control dependence: 6 is Control dependence on 5 7 is Control dependence on 5 8 is Control dependence on 5

  34. Computing Slices Given: Program dependence graph GPD Slicing criterion (n, V ), where n is a statement and V is the set of variables defined or used at n Slice for (n,V): All statements from which n is reachable (i.e., all statements on which n depends) 20

  35. Program Dependence Graph: example 1 2 3 4 5 9 10 6 7 8

  36. Program Dependence Graph: example 1 2 3 4 5 9 10 6 7 8

  37. Program Dependence Graph: example Slice (9, {sum} ) = { n I reachable (n,9)} = {1,2,3,5,6,7,8,9}

  38. Ch 4: Program Comprehension Part 4 Program Analysis Code Refactoring

  39. SOEN 6441 - Advanced Programming Practices 39 Refactoring: what is it? Definition: Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Refactoring does not fix bugs, but it may help find bugs. It may also reduce the further introduction of bugs by cleaning-up code. It is an essential part of agile software development such as Extreme Programming or incremental development. Joey Paquet, 2006-2014

  40. Refactoring Definition: Refactoring modifies software to improve its readability, maintainability, and extensibility without changing what it actually does. External behavior does NOT change Internal structure is improved The goal of refactoring is NOT to add new functionality The goal is refactoring is to make code easier to maintain in the future

  41. SOEN 6441 - Advanced Programming Practices 41 Refactoring: when? Refactoring ought to be done continuously as bad smells are encountered during programming. Bad smells or anti-patterns are portions of design or code that are characterized as potentially confusing and identifies as refactoring targets. More importantly, when using iterative development, a major refactoring stage should precede the beginning of the development of a new build. This will remove slight design problems and ease the addition of further functionality. Joey Paquet, 2006-2014

  42. SOEN 6441 - Advanced Programming Practices 42 Refactoring: why? Refactoring is usually done to: Improve quality improve design quality improve maintainability improve extensibility Improve sustainability of development requires proper testing, so improves testability helps to find bugs Improve productivity improve code readability & comprehensibility simplify code structure Joey Paquet, 2006-2014

  43. Why do good developers write bad software? Requirements change over time, making it hard to update your code (leading to less optimal designs) Time and money cause you to take shortcuts You learn a better way to do something (the second time you paint a room, it s always better than the first because you learned during the first time!)

  44. SOEN 6441 - Advanced Programming Practices 44 Refactoring: how? Each refactoring is implemented as a small behavior-preserving transformation. Behavior-preservation is achieved through pre- and post- transformation testing. Refactoring process: test-refactor-test Joey Paquet, 2006-2014

  45. SOEN 6441 - Advanced Programming Practices 45 Refactoring: drawbacks Cost Overhead: Refactoring is an add-on activity and therefore will incur extra cost in form of time, effort, and resource allocation, especially if elaborated design and code documentation is maintained. However, when done sparingly and only on key issues, its benefits are greater than its overhead. Automated documentation tools, code browsing tools, refactoring tools and testing tools will also diminish the refactoring overhead. Requires Expertise: Refactoring requires some expertise and experience and considerable effort in going through the process, especially if proper testing is involved. However, this overhead can be minimized by using refactoring tools and automated testing such as with a unit testing framework. Joey Paquet, 2006-2014

  46. SOEN 6441 - Advanced Programming Practices 46 Refactoring: examples Consolidate Conditional Expression: You have a sequence of conditional tests with the same result. Refactor by combining them into a single conditional expression and extract it. double disabilityAmount() { if (_seniority < 2) return 0; if (_monthsDisabled > 12) return 0; if (_isPartTime) return 0; // compute the disability amount double disabilityAmount() { if (isNotEligibleForDisability()) return 0; // compute the disability amount Joey Paquet, 2006-2014

  47. SOEN 6441 - Advanced Programming Practices 47 Refactoring: examples Consolidate Duplicate Conditional Fragments: The same fragment of code is in all branches of a conditional expression. Refactor by moving it outside of the expression. if (isSpecialDeal()) { total = price * 0.95; send(); } else { total = price * 0.98; send(); } if (isSpecialDeal()) total = price * 0.95; else total = price * 0.98; send(); Joey Paquet, 2006-2014

  48. SOEN 6441 - Advanced Programming Practices 48 Refactoring: examples Rename Method: The name of a method does not reveal its purpose. Refactor it by changing the name of the method. int getInvCdtLmt(){ } int getInvoiceableCreditLimit(){ } Joey Paquet, 2006-2014

  49. SOEN 6441 - Advanced Programming Practices 49 Refactoring: examples Pull Up Field: Two subclasses have the same field. Refactor it by moving the field to the superclass. Joey Paquet, 2006-2014

  50. SOEN 6441 - Advanced Programming Practices 50 Refactoring: examples Push Down Method: Behavior on a superclass is relevant only for some of its subclasses. Refactor it by moving it to those subclasses. Joey Paquet, 2006-2014

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#