Programe Comprehension
Programe Comprehension
1
Outline
1. General Idea
2. Basic Terms
3. Cognition Models for Program Understanding
4. Protocol Analysis
5. Visualization for Comprehension
6. Summary
2
General Idea
3
Basic Maintenance Task
4
Basic Terms Cognitive Model
Model Maintenance activity Authors
5
Cognitive Model code Comprehension
• Knowledge
• Mental model
6
Goal of Code Cognition
A code maintainer tries to understand a program with a specific goal in
mind.
Example 1: Debugging a program to detect the cause of a known failure
Example 2: Adding a new function to the existing program
8
Knowledge for code
Software-specific knowledge
• The software system has implemented public-key cryptography for data encryption.
• A certain for loop in method y may execute for a random number of times.
9
Knowledge for code
Software-specific knowledge (New Knowledge )
• Functionality
• Software architecture
• Exception handling
• Stable storage
• Implementation details 10
Knowledge
11
Mental Model
14
Mental Model static elements
Text-structures
• Code and its structure are known as text-structures.
• It is useful in gaining control flow knowledge in program understanding.
• A programmer can easily identify the following text-structures:
• Loop constructs: for, while, and until
• Sequences
• Conditional statements: if-then-else
• Variable definitions and initializations
• Calling hierarchies within and among modules
• Definitions of module parameters
• Understanding text-structures is the beginning of program comprehension.
15
Mental Model static elements
Chunks
Chunks enable programmers to create higher level abstractions from lower-level abstractions.
Examples
• A code block initializing a module’s parameters tells the programmer about the nature of
16
Mental Model static elements
Schemas
• Schemas are generic knowledge structures that guide the programmer’s interpretations,
17
Mental Model static elements
Plans
18
Mental Model static elements
Plans
– Example of plan
• A doubly-linked list is an example of a plan; a designer has planned to implement
certain concepts with this data structure.
– A plan is a kind of schema with two parts:
• Slot type
– Slot types describe generic objects.
– Example: A tree data structure is a generic slot type.
• Slot filler
– Slot fillers are customized to hold elements of particular types.
– Example: A code segment, such as a for loop’s code is a slot filler.
– The programmer links the slot-type and slot-filler structures by means of the kind-of
and is-a modeling relationships.
19
Mental Model static elements
Plans
There are two broad kinds of plans.
Domain plans
Programming plans
Domain plans
These include knowledge about the real world problem, including the program’s environment.
Example: If the software is for numerical analysis application, plans will include schemas for
different aspects of linear algebra, such as matrix multiplication and matrix inversion.
Programming plans
Example: A programmer may design a for loop to search an item in a data set and repeatedly use
Hypothesis
– As programmers start reading code and the related documents, they start developing
an understanding of the program to varying degrees.
– Programmers can test the results of their understanding as conjectures (aka
hypotheses.)
• Why: Why conjectures hypothesize the purpose of a program element.
– Verification of a why conjecture enables a programmer to have a good understanding of
the program element.
• How: How conjectures hypothesize the method for realizing a program goal.
– Given a program goal, the programmer needs to know how that goal has been
implemented.
• What: What conjectures enable programmers to classify program elements.
21
Mental Model Dynamic elements
Chunking
Cross-referencing
Strategies
22
Mental Model Dynamic elements
Chunking
– In a program, the lowest level of chunks are code segments.
– creates higher level abstraction structures by combining lower level
chunks.
– This process of creating higher level chunks is called chunking.
– The process of chunking is repeatedly applied to create increasingly
higher levels of abstractions.
– When a block of code is recognized, it is replaced by the programmer
with a label representing the functionality of the code block.
– A block of lower level labels can be replaced with one higher level
label representing a higher level functionality.
23
Mental Model Dynamic elements
Cross-referencing
– Cross-referencing means being able to link elements of different
abstraction levels.
– This helps in building a mental model of the program under study.
– Example:
• Control flow and data-flow can be program elements at a lower level,
whereas functionalities are higher level program elements.
• There is a need to cross-reference between control-flow and data-flow elements
and program functionalities.
24
Mental Model Dynamic elements
Cross-referencing
– A strategy is a planned sequence of actions to reach a specific goal.
– A strategy is formulated by identifying actions to achieve a goal.
– Example: if the goal is to understand the code representing a function, one can define
a strategy as follows:
• Understand the overall computational functionality of the function by reading its
specification, if it exists.
• Understand all the input parameters to the function.
• Read all code line by line.
• Identify chunks of related code.
• Create a higher level model of the function in terms of the chunks.
– Strategies guide the two dynamic elements, namely, chunking and cross-referencing,
to produce higher-level abstraction structures.
25
Class Activity
Find the coding examples for following mental elements OR take an example
code and find the following mental model elements in that specific code.
• Chunking
• Cross-Referencing
• Chunks
• Plans
26