0% found this document useful (0 votes)
14 views

Program Slicing

Program slicing is a technique that extracts a subset of code relevant to understanding a computation or variable. It helps developers focus on specific parts of code rather than the entire program. There are different types of slicing based on criteria and direction, including dynamic, static, and forward slicing. Program slicing has various applications like debugging, understanding code, testing, and security analysis.

Uploaded by

brankliz7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Program Slicing

Program slicing is a technique that extracts a subset of code relevant to understanding a computation or variable. It helps developers focus on specific parts of code rather than the entire program. There are different types of slicing based on criteria and direction, including dynamic, static, and forward slicing. Program slicing has various applications like debugging, understanding code, testing, and security analysis.

Uploaded by

brankliz7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

What is Program slicing

Program slicing is a technique used in software development and debugging to


analyze and understand the behavior of a program by extracting a subset of the
code relevant to a particular computation or variable.
The subset extracted is called a slice.
 This technique helps developers focus on specific parts of the code relevant to understanding a
particular behavior or debugging an issue, rather than dealing with the entire program.
General Workflow of slicing

1. Selection of slicing criterion: Developers choose a criterion such as a specific


variable, statement, or computation that they want to analyze or debug.
2. Slice extraction: The program is analyzed to identify all statements and
control-flow elements that contribute to the computation or the value of the
selected criterion.
3. Pruning: Irrelevant parts of the program that do not influence the selected
criterion are removed from the slice, simplifying the code and focusing
attention on the relevant portions.
Program slicing
4 Presentation: The extracted slice is often presented as a subset of the original
code, making it easier for developers to understand the behavior of the program with
respect to the selected criterion.
Program slicing category
 Program slicing can be categorized into different types based on the slicing
criterion and the direction of the slicing. Here are some common types of program
slicing:
 Dynamic Slicing:
 Forward Slicing:
 Static Slicing:
Dynamic slicing
 Dynamic slicing is used to identify these parts of the program that contribute to the
computation of the selected function for a given program execution (program input).
 Dynamic slicing may help to narrow down this part of the program that contributes to the
computation of the function of interest for particular program input.
Program slicing
Cont…
 In dynamic program slicing, the slice is determined based on a specific execution of the program.
 Let's say we execute the program with the inputs a = 10 and b = 5. During execution, the value of
c will be computed as 15 (since c = a + b). Consequently, the output will be:

 For dynamic slicing, the resulting slice would include the following statements:

 These statements are considered relevant because they directly affect the execution and output of the program for that
specific run.
Static Slicing

 In static slicing, the analysis is performed on the source code without executing it.
 It is typically performed using static code analysis techniques like data flow
analysis and control flow analysis.
 Static slicing is useful for identifying potential dependencies in the code without
the need for actual execution.
Cont…
Step 1:-Let's create a simple Java program that calculates the factorial of a number.

Step 2: Identify Slicing Criterion


Let's choose the slicing criterion as the calculation of the factorial of the number of 5
Step 3: Perform Data Flow Analysis
to identify all statements and variables that contribute to the calculation of the factorial
of 5
Cont…
Step 4: Extract Relevant Code
Based on the data flow analysis, extract the relevant portion of the code that directly influences
the calculation of the factorial of 5

 This static slicing process helps isolate the relevant portion of the code related to the calculation of the factorial of 5
making it easier to analyze and understand
Forward slicing
 Forward slicing starts from a slicing criterion and identifies all statements that are
affected by it.
 It traces the flow of data and control from the slicing criterion forward in the
program.
 Forward slicing is useful for understanding how changes to certain variables or
statements propagate through the program.
Forward slicing
 Let's demonstrate forward slicing with a Java example. We'll consider a simple program that
calculates the sum of the first N natural numbers

Let's choose the slicing criterion as the calculation of the sum of the first 5 natural numbers.
Perform Forward Slicing
Trace the flow of data and control from the slicing criterion (calculation of the sum) forward in the program.
Forward slicing
Extract Relevant Code
 Based on the forward slicing analysis, extract the relevant portion of the code that directly
contributes to the calculation of the sum of the first 5 natural numbers.

Step 5: Validate the Slice


Ensure that the extracted code snippet retains the behavior of the original program in calculating the sum of
the first 5 natural numbers.
This forward slicing process isolates the relevant portion of the code related to the calculation of the sum of
the first 5 natural numbers, making it easier to analyze and understand how changes to variables like “n” and
“sum” affect the final result.
Application of program slicing
 Program slicing finds various applications in software engineering and debugging.
Here are some common applications:
1. Debugging: Program slicing helps developers isolate the relevant portion of the code related to a bug or
unexpected behavior, making it easier to identify and fix issues.
2. Understanding Code: It aids in understanding complex code by focusing on specific portions relevant
to a particular functionality or behavior.
3. Code Maintenance: When maintaining or refactoring code, program slicing helps identify
dependencies and understand the impact of changes on other parts of the program.
Cont…
4.Testing: Program slicing can be used to generate focused test cases by isolating specific portions of the code for
testing, improving test coverage and efficiency.
5.Optimization: It assists in identifying redundant or unnecessary code, allowing for optimization and improve
performance.
6.Security Analysis: Program slicing can be applied in security analysis to identify potential vulnerabilities or points of attack by
analyzing the flow of sensitive data through the program.
7.Understanding Legacy Code: When working with legacy codebases, program slicing can help developers comprehend unfamiliar
or poorly documented code by extracting and focusing on relevant parts.
8.Impact Analysis: Program slicing helps assess the impact of changes by identifying all code that depends on a particular variable,
function, or statement.
9.Parallelization: For parallel computing, program slicing can be used to identify independent portions of the code that can be
executed concurrently, improving performance.
Reference
1. https://dl.acm.org/doi/10.1007/978-3-031-17108-6_9#:~:text=Program%20slicing%20is%20a%20static,%2Duse%2C%20and%
20closed%20source
.

2. https://www.sciencedirect.com/science/article/pii/S2352220822000797
3. https://core.ac.uk/download/pdf/295539081.pdf
4. https://mieszko.ece.ubc.ca/slicer4j-esec-fse-2021.pdf

You might also like