program understanding: what programmers really want

153

Click here to load reader

Upload: einar-host

Post on 19-Jun-2015

566 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Program understanding: What programmers really want

Program Understanding:What Programmers Really Want

Einar W. HøstMay 5th 2011

Page 2: Program understanding: What programmers really want

Evaluation Examples

DescriptionIntroduction

Consider Limitations of Current Tools

Look at somewell-known

Analyses

How Analysis can help

Understanding

Understanding Program

Understanding

AgendaAgenda

Page 3: Program understanding: What programmers really want

Program Analysis

Program Analysis

Program Understanding

Page 4: Program understanding: What programmers really want

Why is program understanding important?

Page 5: Program understanding: What programmers really want

It’s what programmers do!

Page 6: Program understanding: What programmers really want

“To program is to understand” - Kristen Nygaard

Page 7: Program understanding: What programmers really want

Why is program understanding hard?

Page 8: Program understanding: What programmers really want

Size.

Page 9: Program understanding: What programmers really want

Complexity.

Page 10: Program understanding: What programmers really want

Heterogenity.

Page 11: Program understanding: What programmers really want

Side-effects.

Page 12: Program understanding: What programmers really want

The concept assignment problem.

Page 13: Program understanding: What programmers really want

Intangibility.

Page 14: Program understanding: What programmers really want

=>

Page 15: Program understanding: What programmers really want

Disorientation.

Page 16: Program understanding: What programmers really want

“Few are the programmers who can explain their code well enough so that reading it is not incredibly frustrating.” - Jef Raskin

Page 17: Program understanding: What programmers really want

How can program analysis help?

Page 18: Program understanding: What programmers really want

$s.=($f=$ARGV[$_%2])x(substr$s,$_,1or$f)for0..500;

Program Understanding

?

Page 19: Program understanding: What programmers really want

Program Understanding

!

Through the magic lens of a Tool

$s.=($f=$ARGV[$_%2])x(substr$s,$_,1or$f)for0..500;

Page 20: Program understanding: What programmers really want

What do programmers want?

Page 21: Program understanding: What programmers really want

”Help me in finding what needs to be changed...

Page 22: Program understanding: What programmers really want

...make the change...

Page 23: Program understanding: What programmers really want

...and get the !$&?/% out!”

Page 24: Program understanding: What programmers really want

Just-in-time program understanding.

Page 25: Program understanding: What programmers really want

Complete understanding is

- Not realistic- Not cost-effective- Not necessary

Page 26: Program understanding: What programmers really want

Understand the program well enough to

implement a new feature

Page 27: Program understanding: What programmers really want

Understand the program well enough to

fix a bug

Page 28: Program understanding: What programmers really want

Understand the program well enough to

improve performance

Page 29: Program understanding: What programmers really want

Without change, there is no need for understanding!

Page 30: Program understanding: What programmers really want

Program understanding

- Understanding execution behavior- Understanding control flow- Understanding data flow- Understanding dependencies- Understanding side-effects- Understanding change impact- Reasoning about design

at the code level

Page 31: Program understanding: What programmers really want

Program understanding

- Understanding what is relevant

at the task level

Page 32: Program understanding: What programmers really want

Comprehension strategies

Page 33: Program understanding: What programmers really want

Comprehension strategies

Top-down

Reconstruct knowledge about the program domain and mapping it to the source code.

Page 34: Program understanding: What programmers really want

Comprehension strategies

Bottom-up

Read code statements and mentally group these statements into higher level abstractions.

Page 35: Program understanding: What programmers really want

Comprehension strategies

Systematic

Read code in detail,follow control flow,gain global understanding.

Page 36: Program understanding: What programmers really want

Comprehension strategies

Opportunistic

Scan code, looking for clues indicating relevance to the task at hand.

Page 37: Program understanding: What programmers really want

Comprehension strategies

Inquiries

conjecture

read

searchquestion

Page 38: Program understanding: What programmers really want

Comprehension strategies

Integrated

Understanding is formed by switching between different strategies as needed.

Page 39: Program understanding: What programmers really want

Tools for program understanding

Page 40: Program understanding: What programmers really want

Presentation

Analysis

Page 41: Program understanding: What programmers really want

Analysis tools

- Call graphs- Program slicing- Feature location- Effects of change- Software metrics

Page 42: Program understanding: What programmers really want

Presentation tools

- Visualisations- Context-awareness

Page 43: Program understanding: What programmers really want

What is a program?

Page 44: Program understanding: What programmers really want

print ”This is not a program.”

Page 45: Program understanding: What programmers really want

print(”This is not a program.”)

Page 46: Program understanding: What programmers really want

The program as

text

Page 47: Program understanding: What programmers really want

The program as

binary

Page 48: Program understanding: What programmers really want

The program as

versioned

Page 49: Program understanding: What programmers really want

The program as

process

Page 50: Program understanding: What programmers really want

The program ecosystem

- Source code / Compiled binary- Execution environment (runtime)- Test suite - Version control system- Issue tracking system- Integrated development environment- The programmer- External sources

Page 51: Program understanding: What programmers really want

What questions can we ask about a program?

Page 52: Program understanding: What programmers really want

Behavior-centric

- What does this piece of code do? - What happens if I change this?- Where is this code used?- Which parts of the program are affected by change?

Requires program code

Page 53: Program understanding: What programmers really want

Evolution-centric

- Who wrote this piece of code?- Who can help me understand this?- Why was this feature implemented this way? - What are the most bug-prone parts of the program?- Where do changes happen most often?

Requires program history

Page 54: Program understanding: What programmers really want

Interaction-centric

- How was this piece of code written?- Which tasks are hard to accomplish?- Which parts of the program are hard to change?

Requires coding-session history

Page 55: Program understanding: What programmers really want

What is program analysis?

Page 56: Program understanding: What programmers really want

Traditional program analysis

All you need is codeAll you need is code

All you need is code, codeCode is all you need

Page 57: Program understanding: What programmers really want

Static

Dynamic

Page 58: Program understanding: What programmers really want

Static

All possible executions

Page 59: Program understanding: What programmers really want

Static

Sound+

Conservative

Page 60: Program understanding: What programmers really want

Static

Trades precision

for soundness

Page 61: Program understanding: What programmers really want

Sample executions

Dynamic

Page 62: Program understanding: What programmers really want

Efficient+

Precise

Dynamic

Page 63: Program understanding: What programmers really want

Trades completeness

forefficiency

Dynamic

Page 64: Program understanding: What programmers really want

Static

Dynamic

Synergy

Page 65: Program understanding: What programmers really want

Program analysis for program understanding

Page 66: Program understanding: What programmers really want

Call graph construction

Page 67: Program understanding: What programmers really want

Call graph construction

Essential idea

Show calling relationships between parts of a program.

Page 68: Program understanding: What programmers really want

Call graph construction

Sample graph

main

f

g

h

Page 69: Program understanding: What programmers really want

Call graph construction

The ideal call graph

The relation describing exactly those calls made from one entity to another in any possible execution of the program.

Page 70: Program understanding: What programmers really want

Call graph construction

Benefit

Make the program seem less fragmented by showing the control flow links between program parts.

Page 71: Program understanding: What programmers really want

Call graph construction

Limitation

The graph becomes too large and complex to be comprehensible.

Page 72: Program understanding: What programmers really want

Call graph construction

”The sight of gcc's call graph frightened my students so much that they requested a different project”

- Arun Lakhotia

Limitation

Page 73: Program understanding: What programmers really want
Page 74: Program understanding: What programmers really want

Program slicing

Page 75: Program understanding: What programmers really want

Program slicing

Essential idea

Find a reduced program exhibiting the same behavior of interest as the full program.

Page 76: Program understanding: What programmers really want

Program slicing

The reduced program is called a program slice.

Page 77: Program understanding: What programmers really want

Program slicing

x : a statement in program PV : a subset of variables in P

C = < x, V >

Slicing criterion

Page 78: Program understanding: What programmers really want

1 begin

2 read(x,y)

3 total := 0.0

4 sum := 0.0

5 if x <= 1

6 then sum := y

7 else begin

8 read(z)

9 total := x*y

10 end

11 write(total, sum)

12 end.

1 begin

2 read(x,y)

5 if x <= 1

6 then

7 else

8 read(z)

12 end.

Criterion: <12, z>

Page 79: Program understanding: What programmers really want

1 begin

2 read(x,y)

12 end.

Criterion: <12, x> 1 begin

2 read(x,y)

3 total := 0.0

4 sum := 0.0

5 if x <= 1

6 then sum := y

7 else begin

8 read(z)

9 total := x*y

10 end

11 write(total, sum)

12 end.

Page 80: Program understanding: What programmers really want

Program slicing

Forward conditioning

What would the program look like if we assume an initial state satisfying C ?

Page 81: Program understanding: What programmers really want

Program slicing

Forward conditioning

Deletes statements that will not be executed given the initial state.

Page 82: Program understanding: What programmers really want

Program slicing

Backward conditioning

What would the program look like if we assume an eventual state satisfying C ?

Page 83: Program understanding: What programmers really want

Program slicing

Backward conditioning

Deletes statements which cannot lead to the eventual state.

Page 84: Program understanding: What programmers really want

Program slicing

Benefit

The reduced programis smaller.

Page 85: Program understanding: What programmers really want

Program slicing

Limitation

The reduced programlooks foreign.

Page 86: Program understanding: What programmers really want

Program slicing

Limitation

Hard to integrate into the programmer’s work flow.

Page 87: Program understanding: What programmers really want

Concept analysis

Page 88: Program understanding: What programmers really want

Concept analysis

Essential idea

Identify groupings of objects that have common attributes.

Page 89: Program understanding: What programmers really want

Concept analysis

Formal context

O Set of objectsA Set of attributesR Relation R⊆O ×A

C = (O, A, R )

Page 90: Program understanding: What programmers really want

Concept analysis

σ(O) = {a∈A⎮∀o∈O : (o, a)∈R}

Common attributes

O Set of objectsA Set of attributesR Relation R⊆O ×A

Page 91: Program understanding: What programmers really want

Concept analysis

τ(A) = {o∈O⎮∀a∈A : (o, a)∈R}

Common objects

O Set of objectsA Set of attributesR Relation R⊆O ×A

Page 92: Program understanding: What programmers really want

Concept analysis

A pair (O, A) is a conceptif A = σ(O) and O = τ(A)

Definition of concept

Page 93: Program understanding: What programmers really want

Concept analysis

O => SubprogramsA => Features

Feature location

Page 94: Program understanding: What programmers really want

Concept analysis

if subprogram s is invoked when feature f is invoked

Feature location

(s, f) ⊆ R

Page 95: Program understanding: What programmers really want

Concept analysis

Benefit

Identify parts of the program relevant for a feature.

Page 96: Program understanding: What programmers really want

Concept analysis

Benefit

Recovery of components and generation of high-level architecture views.

Page 97: Program understanding: What programmers really want

Concept analysis

Limitation

Imperfect high-level descriptions have limited application for concrete tasks.

Page 98: Program understanding: What programmers really want

Concept analysis

Limitation

Requires effort from the programmer, with unclear benefits.

Page 99: Program understanding: What programmers really want

Concept analysis

Limitation

Hard to integrate into the programmer’s work flow.

Page 100: Program understanding: What programmers really want

Change impact analysis

Page 101: Program understanding: What programmers really want

Change impact analysis

Essential idea

Identify the potential consequences of a program change.

Page 102: Program understanding: What programmers really want

Change impact analysis

Essential idea

Estimate what must be modified to accomplish a change of behavior.

Page 103: Program understanding: What programmers really want

Change impact analysis

Challenges in OO languages

Subtyping and dynamic dispatch means that change impact can be non-local and unexpected.

Page 104: Program understanding: What programmers really want

Add empty classDelete empty class

Add fieldDelete field

Add empty methodDelete empty method

Change method bodyChange method lookup

Change impact analysis

Atomic changes

Page 105: Program understanding: What programmers really want

Change impact analysis

Benefit

Provide a boundary around the effects of a program edit.

Page 106: Program understanding: What programmers really want

Change impact analysis

Benefit

Provide confidence that an edit does not have unexpected effects outside the boundary.

Page 107: Program understanding: What programmers really want

Change impact analysis

Limitation

Limited by the precision of the change impact analysis.

Page 108: Program understanding: What programmers really want

Change impact analysis

Limitation

Still need assurance that no unexpected effects occur in the impacted part of the program.

Page 109: Program understanding: What programmers really want

Change impact analysis

Limitation

Hard to integrate into the programmer’s work flow.

Page 110: Program understanding: What programmers really want

Program metrics

Page 111: Program understanding: What programmers really want

Program metrics

Essential idea

Quantify aspects of the program presumed to be relevant.

Page 112: Program understanding: What programmers really want

Program metrics

Code-centric metrics

- Lines of code- Depth of inheritance tree- Internal cohesion- Coupling to other elements- Cyclomatic complexity- Halstead complexity- ...

Page 113: Program understanding: What programmers really want

Program metrics

Other metrics

- Test coverage- Bug density- Change rate- ...

Page 114: Program understanding: What programmers really want

Program metrics

Benefit

Answer questions regardingprogram quality.

Page 115: Program understanding: What programmers really want

Program metrics

Benefit

Identify potential problemareas in the program.

Page 116: Program understanding: What programmers really want

Program metrics

Limitation

Metrics are indirectindicators of quality.

Page 117: Program understanding: What programmers really want

Program metrics

Limitation

Task-generating, not task-solving.

Page 118: Program understanding: What programmers really want

Software visualisation

Page 119: Program understanding: What programmers really want

Software visualisation

Goal

Avoid overwhelming the programmer by compressing information and using graphics.

Page 120: Program understanding: What programmers really want

Table lens

Page 121: Program understanding: What programmers really want

Table lens

Idea

Zoom out the table layout and display cells as pixel bars scaled and colored by data values

Page 122: Program understanding: What programmers really want

Table lens

Example

Page 123: Program understanding: What programmers really want

Table lens

Application

Present a compact view of software metrics for program elements.

Page 124: Program understanding: What programmers really want

Treemap

Page 125: Program understanding: What programmers really want

Treemap

Idea

Display hierarchical data as nested rectangles.

Page 126: Program understanding: What programmers really want

Treemap

Example

Page 127: Program understanding: What programmers really want

Treemap

Application

Compare metric values for various program elements.

Page 128: Program understanding: What programmers really want

Polymetric view

Page 129: Program understanding: What programmers really want

Polymetric view

Idea

Combine several metrics for an entity into a single view, by relating a visual aspect to each metric.

Page 130: Program understanding: What programmers really want

Polymetric view

Example

Page 131: Program understanding: What programmers really want

Polymetric view

Application

Present a compact view of software metrics for program elements.

Page 132: Program understanding: What programmers really want

Hierarchical edge bundles

Page 133: Program understanding: What programmers really want

Hierarchical edge bundles

Idea

Bundle association data with structure data in a radial view.

Page 134: Program understanding: What programmers really want

Hierarchical edge bundles

Example

Page 135: Program understanding: What programmers really want

Hierarchical edge bundles

Application

Compact view of associations between program elements.

Page 136: Program understanding: What programmers really want

What about task-orientedprogram understanding?

Page 137: Program understanding: What programmers really want

The paradox of software visualisation

Page 138: Program understanding: What programmers really want

Programmers use visualisation all the time when discussing programs informally.

Page 139: Program understanding: What programmers really want

Computers are good at presenting information graphically.

Page 140: Program understanding: What programmers really want

...and yet...

Page 141: Program understanding: What programmers really want

Software visualisation tools are rarely used for everyday software development and understanding.

Page 142: Program understanding: What programmers really want

Why?

Page 143: Program understanding: What programmers really want

Hypothesis

Research is out of touch with reality!

Page 144: Program understanding: What programmers really want

Claim

Software visualisationis a generic solution looking for problems.

Page 145: Program understanding: What programmers really want

Claim

Programmers need specific solutions to specific problems.

Page 146: Program understanding: What programmers really want

Remedy

An easy-to-use tool that lets the programmer define the problem that needs visualisation.

Page 147: Program understanding: What programmers really want

Task-aware environments

Page 148: Program understanding: What programmers really want

Task-aware environments

Idea

Adapt interface to reflect the task the programmer is currently working on.

Page 149: Program understanding: What programmers really want

Task-aware environments

Details

Introduces the notion of development sessions.

Page 150: Program understanding: What programmers really want

Task-aware environments

Details

Links development sessions to tasks.

Page 151: Program understanding: What programmers really want

Task-aware environments

Details

Sessions can be thrown away or saved for later.

Page 152: Program understanding: What programmers really want

Code bubbles

Page 153: Program understanding: What programmers really want

Evaluation Examples

DescriptionIntroduction

Consider Limitations of Current Tools

Look at somewell-known

Analyses

How Analysis can help

Understanding

Understanding Program

Understanding

AgendaSummary