chapter 16hossein/teaching/fa09/814/lectures...... a craftsman’s approach, 4th edition chapter 16...

56
Software Testing: A Craftsmans Approach, 4 th Edition Chapter 16 Software Complexity Chapter 16 Software Complexity

Upload: vuongthu

Post on 11-Apr-2018

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Chapter 16

Software Complexity

Page 2: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Levels of Software Complexity

•  Unit Level –  Topological (cyclomatic) complexity, based on a program

graph –  Decisional complexity, a refinement of topological

complexity, based on the number of simple conditions in a compound condition

–  Computational (Halstead’s Metrics)

•  Integration Level (based on a Call Graph) –  Procedure/function invocation in traditional code –  Message passing in object-oriented code

•  System Level (is/does incidence matrix) •  Why? Complexity implies more testing.

Page 3: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Program Graph Definitions"

"Given a program written in an imperative programming language, its program graph is a directed graph in which nodes are either entire statements or fragments of a statement, and edges represent flow of control (there is an edge from node i to node j iff the statement (fragment) corresponding to node j can be executed immediately after the statement or statement fragment corresponding to node i)."

Page 4: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Example: Triangle Program Specification"

•  Inputs: a, b, and c are non-negative integers, taken to be sides of a triangle"

•  Output: type of triangle formed by a, b, and c "–  Not a triangle"–  Scalene (no equal sides)"–  Isosceles (exactly 2 sides equal)"–  Equilateral (3 sides equal)"

•  To be a triangle, a, b, and c must satisfy"–  a < b + c,"–  b < a + c, and"–  c < a + b"

Page 5: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

11

1 2 3 4 5

6 7 8

9

10

12

13

14 15

16

17

18

19

20

Triangle Program Pseudo-Code and Program Graph"

Program Triangle Dim a, b, c As Integer Dim IsATriangle As Boolean 'Step 1: Get Input 1. Output ("Enter 3 integers which are sides of a triangle") 2. Input (a,b,c) 3. Output ("Side A is ",a) 4. Output ("Side B is ",b) 5. Output ("Side C is ",c) 'Step 2: Is A Triangle? 6. If (a < b + c) AND (b < a + c) AND (c < a + b) 7.  Then IsATriangle = True 8.  Else IsATriangle = False 9. Endif 'Step 3: Determine Triangle Type 10. If IsATriangle 11. Then If (a = b) AND (b = c) 12.  Then Output (“Equilateral”) 13.  Else If (a ≠ b) AND (a ≠ c) AND (b ≠ c) 14.  Then Output (“Scalene”) 15.  Else Output (“Isosceles”) 16.  Endif 17.  Endif 18.  Else Output (“Not a Triangle”) 19. Endif 20. End Program Triangle

Page 6: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Cyclomatic Complexity"

•  In a strongly connected directed graph G, its cyclomatic complexity, denoted by V(G), is given by V(G) = e – n + p, where"•  e is the number of edges in G"•  n is the number of nodes in G"•  p is the number of connected regions in G"

•  In code that conforms to structured programming (single entry, single exit), p = 1."

•  In a directed graph that is not strongly connected, V(G) = e – n + 2p. "

Page 7: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

A Closer Look at Cyclomatic Complexity

•  We need to distinguish between strongly connected call graphs and call graphs that are “almost” strongly connected…

•  V(G) = e – n + p, for strongly connected call graphs •  Tempting to say V(G) = e – n + 2p for call graphs that

have a single source node and multiple sink nodes.

•  Best answer: add edges from each sink node to each source node, making the Call Graph strongly connected.

Page 8: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Cattle Pens and Cyclomatic Complexity"

•  Cyclomatic complexity describes the number of “enclosed regions” in a directed graph."

•  Given a directed graph drawing, to compute V(G) without counting nodes, edges, and connected regions, consider the nodes to be fence posts, and edges are 4-wire fences. Then the number of cattle pens is V(G). (The outside is also a cattle pen.)"

Page 9: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Cattle Pens and Cyclomatic Complexity"

11

1 2 3 4 5

6 7 8

9

10

12

13

14 15

16

17

18

19

20

1

2

5

4

3

V(G) = e – n + 2p" = 23 – 20 + 2" = 5

Page 10: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Cattle Pens and Cyclomatic Complexity"

Another example…"V(G) = e – n + 2p" = 37 – 31 + 2" = 8

1

2 3

4

5

4

6

7

8

9

10

11

12

13

14 15

16

17

31

1

1716

18

20

8

2

3

5

6

7

9

11 12 13

10

19

18

14 15

21

19

20

21

29

30

23

26

27

28

25

32

34

33

37

28

27

22

36

23

30

22

24

24

25

35

1

2

3

4

5

6

7

8

26

29

31

Page 11: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Node Outdegrees and Cyclomatic Complexity

Rather than draw directed graphs and count nodes, edges, or even cattle pens, We can use easier notions of graph theory, but first, more definitions...

Page 12: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

More definitions…

•  Definition: The indegree of a node in a directed graph is the number of edges that terminate on the node. Denote the degree of node n as inDeg(n).

•  Definition: The outdegree of a node in a directed graph

is the number of edges that originate at the node. Denote the outdegree of node n as outdeg(n).

•  Definition: The reduced outdegree of a node in a

directed graph is the outdegree – 1. Denote the reduced outdegree of node n as reducedOut(n).

Page 13: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

A cattle pen “begins” with a node with outDeg >= 2

1

2 3

4

5

4

6

7

8

9

10

11

12

13

14 15

16

17

31

1

1716

18

20

8

2

3

5

6

7

9

11 12 13

10

19

18

14 15

21

19

20

21

29

30

23

26

27

28

25

32

34

33

37

28

27

22

36

23

30

22

24

24

25

35

1

2

3

4

5

6

7

8

26

29

31

Node outDeg reducedOut(node) 1 2 1 7 3 2

13 2 1 19 3 2 26 2 1

total = 7

Page 14: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Why bother? More sophisticated than counting cattle pens."

Theorem: Given a directed graph G of n nodes, the cyclomatic complexity V(G) of G is given by the sum of the reduced outdegrees of the nodes of G plus 1, i.e.! V(G) = (∑ i=1..n reducedOut(i)) + 1

Why the “+1”? This takes care of the “outside” of

the graph (the open range cattle pen)."

Page 15: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Another Example—NextDate"

•  Pseudo-code on the next four slides"•  Program graph after pseudo-code"

Page 16: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Program NextDate Dim tomorrowDay,tomorrowMonth,tomorrowYear As Integer Dim day,month,year As Integer 1. Output ("Enter today's date in the form MM DD YYYY") 2. Input (month, day, year) 3. Case month Of 4. Case 1: month Is 1,3,5,7,8, Or 10: '31 day months (except Dec.) 5. If day < 31 6. Then tomorrowDay = day + 1 7. Else 8. tomorrowDay = 1 9. tomorrowMonth = month + 1 10. EndIf 11. Case 2: month Is 4,6,9, Or 11 '30 day months 12. If day < 30 13. Then tomorrowDay = day + 1 14. Else 15. tomorrowDay = 1 16. tomorrowMonth = month + 1 17. EndIf

Page 17: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

18. Case 3: month Is 12: 'December 19. If day < 31 20. Then tomorrowDay = day + 1 21. Else 22. tomorrowDay = 1 23. tomorrowMonth = 1 24. If year = 2012 25. Then Output ("2012 is over") 26. Else tomorrow.year = year + 1 27. EndIf 28. EndIf

Page 18: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

29. Case 4: month is 2: 'February 30. If day < 28 31. Then tomorrowDay = day + 1 32. Else 33. If day = 28 34. Then If (isLeap) 35. Then tomorrowDay = 29 'leap year 36. Else 'not a leap year 37. tomorrowDay = 1 38. tomorrowMonth = 3 39. EndIf

Page 19: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

40. Else If day = 29 41. Then If (isLeap) 42. Then tomorrowDay = 1 43. tomorrowMonth = 3 44. Else 'not a leap year 45. Output("Cannot have Feb.", day) 46. EndIf 47. EndIf 48. EndIf 49. EndIf 50. EndCase 51. Output ("Tomorrow's date is", tomorrowMonth, tomorrowDay, tomorrowYear) 52. End NextDate

Page 20: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

11

1

2

3

4

5

6

7

8

9

10

12

13

14

15

16

17

18

19

20

21

22

23

24

25 26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

50

47

48

49

44

45

46

Page 21: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Find V(G) for the Program Graph of NextDate"

11

1

2

3

4

5

6

7

8

9

10

12

13

14

15

16

17

18

19

20

21

22

23

24

25 26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

50

47

48

49

44

45

46

e = 60"n = 50 "p = 1""V(G) = 60 – 50 + 2" = 12"

Page 22: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Find V(G) for the Program Graph of NextDate"

11

1

2

3

4

5

6

7

8

9

10

12

13

14

15

16

17

18

19

20

21

22

23

24

25 26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

50

47

48

49

44

45

46

node outdeg reducedOut

3 4 3

5 2 1

12 2 1

19 2 1

24 2 1

30 2 1

33 2 1

35 2 1

41 2 1

∑ = 11

Page 23: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Decisional Complexity"

•  Topological (cyclomatic) complexity only captures the overall control structure of source code. "

•  The complexities due to compound conditions are ignored."

•  Any decision-making compound condition (loop, switch, or If) can always be rewritten as nested If-Then logic."

•  For testing purposes, compound conditions need to be analyzed as decision tables."

Page 24: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Decisional Complexity"

Consider the code fragment 1. If (a < b + c) AND (b < a + c) AND (c < a + b) 2. Then IsATriangle = True 3. Else IsATriangle = False 4. Endif Expanding the compound condition to nested If logic, we have 1. If (a < b + c) 2. Then If (b < a + c) 3. Then If (c < a + b) 4. Then IsATriangle = True 5. Else IsATriangle = False 6. EndIf ‘(c < a + b) 7. Else IsATriangle = False 8. EndIf ‘(b < a + c) 9. Else IsATriangle = False 10. EndIf ‘(a < b + c)

1

2 3

4

1

2

3

4 5

6

7

8

9

10

Page 25: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Decisional Complexity

•  As a refinement of cyclomatic complexity… –  Rewrite code fragments corresponding to compound

conditions as nested-If logic. –  Find the cyclomatic complexity of the rewritten code. –  Use that as a weighting coefficient on the code fragment.

•  Alternatively, just rewrite any compound conditions and then compute the cyclomatic complexity.

Page 26: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Estimating Decisional Complexity"

•  The added complexity of compound conditions cannot be determined from a program graph.

•  Must be derived from the source code •  Could go the truth table analysis route, but… •  We can estimate the added complexity of

compound conditions to be one less than the number of simple conditions in the expression.

•  Why one less? The compound condition creates a unit of cyclomatic complexity, so this avoids “double counting.”

Page 27: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Total Unit Level Complexity for NextDate

•  Cyclomatic complexity is 12 •  Added Decisional complexity is 0 •  BUT the boolean function isLeap is used at

statements 34 and 41 –  cyclomatic complexity of isLeap is 2 –  added decisional complexity of isLeap is 2

•  Total unit level (counting isLeap) is 16

Page 28: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Computational Complexity"

•  Are all nodes “created equal”? No!"•  Digression: we need to define (and use) a new

concept—a DD-Path."•  Definition: A DD-Path is a sequence of two or

more statements that begins with the outcome of a decision-making statement fragment and ends with the “next” decision."

•  DD-Path execution is like a sequence of dominoes, once the first statement executes, every statement in the DD-Path executes, until the next decision point is reached.""

Page 29: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

DD-Paths and Cyclomatic Complexity"

•  Given a program P, and its program graph, G(P), identify all the DD-Paths in P."

•  Form the reduced program graph of P where nodes are either single nodes of P or nodes that correspond to DD-Paths of P."

•  Theorem: Given a program P, its program graph, G(P), and its reduced program graph Reduced(G(P)), the cyclomatic complexities of the two graphs are equal, i.e., "

•  V(G(P)) = V(G(Reduced(G(P)))."

Page 30: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Halstead’s Metrics"

•  Given a program P, these metrics are based on"•  The number of distinct operators, n1,"•  The number of distinct operands, n2,"•  The total number of operators, N1, and"•  The total number of operands, N2."

•  With these, Halstead defined"•  Program length as N = N1 + N2"•  Program vocabulary as n = n1 + n2"•  Program volume as V = Nlog2(n)"•  Program difficulty as D = (n11N2)/2n2"

•  We will apply Halstead’s Metrics to code in a DD-Path."

Page 31: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Example: Day of week with Zeller’s Congruence

•  determines the day of the week of a given date •  The inputs d, m, y respectively are day, month, and year •  We compare two implementations

Page 32: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

First Implementation

if (m < 3) { m += 12;

y -= 1; }

int k = y % 100;int j = y / 100;int dayOfWeek = ((d + (((m + 1) * 26) / 10) + k + (k / 4) + (j / 4)) + (5 * j)) % 7;

Page 33: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Second Implementation

if (month < 3) { month += 12; --year; } return dayray[(int)(day + (month + 1) * 26 / 10 + year + year / 4 + 6 * (year / 100) + year / 400) % 7];

Page 34: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

operator Number of occurrences Operand Number of occurrences If 1 m 3 < 1 y 3

+= 1 k 3 -= 1 j 3 = 3 dayOfWeek 1 % 2 d 1 / 4 3 1 + 6 12 1 * 2 1 1

n1 = 9 N1 = 21 100 2 26 1 10 1 4 2 5 1 7 1

n2 = 15 N2 = 25

Halstead’s Metrics for first Version

Page 35: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Halstead’s Metrics for Second Version operator Number of occurrences operand Number of occurrences

If 1 month 3

< 1 year 5

+= 1 dayray 1

-- 1 day 1

return 1 3 1

+ 6 12 1

* 2 1 1

/ 2 26 1

% 1 10 1

n1 = 9 N1 = 16 4 1

6 1

100 1

400 1

7 1

n2 = 14 N2 = 20

Page 36: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Halstead metrics for the two implementations

Halstead’s Metric Version 1 Version 2

program length, N = N1 + N2 21 + 25 = 46 16 + 20 = 36

program vocabulary, n = n1 + n2 9 + 15 = 24 9 + 14 = 23

program volume, V = Nlog2(n) 46 (log2(24)) = 211 36 (log2(23)) = 163

program difficulty, D = (n1N2)/2n2 (9*25)/(2*15) = 7.50 (9*20)/(2*14) = 6.43

Page 37: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Integration Level Complexity •  concern shifts from correctness of individual units

to correct function across units.

•  integration level testing presumes that the units have been thoroughly tested “in isolation”.

•  attention shifts to interfaces among units and what we might call “communication traffic”

•  The starting point to analyze integration level complexity of a program is its Call Graph.

•  This is the level where procedural and object-oriented code are distinct.

Page 38: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Integration Level Cyclomatic Complexity

•  Definition: Given the Call Graph of a program the integration level cyclomatic complexity is the cyclomatic complexity of the call graph

•  (this applies to procedural and object-oriented

code)

Page 39: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Integration Call Graph •  Definition: Given a program written in an

imperative programming language, its Call Graph is a directed graph in which nodes correspond to methods, and edges correspond to messages.

•  For object-oriented code, if method A sends a message to method B, there is an edge from node A to node B. For procedural code, if unit A refers to unit B, there is an edge from node A to node B.

•  Must add edges from every sink node to every source node.

Page 40: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Integration Version of NextDate Example (see next four slides for pseudo-code)

msg1

Main

GetDate

ValidDate

IncrementDate

isLeaplastDayOfMonth

printDate

msg3 Msg2, msg4

msg7 msg8

msg5msg6

addedadded

V(G) = edges – nodes + 1 = 9 – 7 + 1 = 3.

Page 41: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

isLeap V(G) = 4

7

8

9

10

11 12

13

14 15

17

16

18

19

20

1 Main integrationNextDate

Type Date Month As Integer Day As Integer Year As Integer EndType

Dim today As Date Dim tomorrow As Date

2 GetDate(today) 'msg13 PrintDate(today) 'msg24 tomorrow = IncrementDate(today) 'msg35 PrintDate(tomorrow) 'msg46 End Main

7 Function isLeap(year) Boolean8 If (year divisible by 4)9 Then10 If (year is NOT divisible by 100)11 Then isLeap = True12 Else13 If (year is divisible by 400)14 Then isLeap = True15 Else isLeap = False16 EndIf17 EndIf18 Else isLeap = False19 EndIf20 End (Function isLeap)

1 2 3 4 5 6

Main V(G) = 1

Page 42: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

21 Function lastDayOfMonth(month, year) Integer22 Case month Of23 Case 1: 1, 3, 5, 7, 8, 10, 1224 lastDayOfMonth = 3125 Case 2: 4, 6, 9, 1126 lastDayOfMonth = 3027 Case 3: 228 If (isLeap(year)) 'msg529 Then lastDayOfMonth = 2930 Else lastDayOfMonth = 2831 EndIf32 EndCase33 End (Function lastDayOfMonth)

68 Function IncrementDate(aDate) Date69 If (aDate.Day < lastDayOfMonth(aDate.Month)) 'msg870 Then aDate.Day = aDate.Day + 171 Else aDate.Day = 172 If (aDate.Month = 12)73 Then aDate.Month = 174 aDate.Year = aDate.Year + 175 Else aDate.Month = aDate.Month + 176 EndIf77 EndIf78 End (IncrementDate)

79 Procedure PrintDate(aDate)80 Output( "Day is ", aDate.Month, "/", aDate.Day, "/", aDate.Year)81 End (PrintDate)

IncrementDate V(G) = 3

68

69

71

74

77

78

72

73 75

76

70

LastDayOfMonthV(G) = 4

21

22

23

24

25

26

27

28

29 30

31

32

33

PrintDate V(G) = 1

79

80

81

Page 43: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

34 Function GetDate(aDate) Date dim aDate As Date35 Function ValidDate(aDate) Boolean 'within scope of GetDate dim aDate As Date dim dayOK, monthOK, yearOK As Boolean36 If ((aDate.Month > 0) AND (aDate.Month <=12)

‘added decisional complexity = +137 Then monthOK = True38 Else monthOK = False39 EndIf 40 If (monthOK)41 Then42 If ((aDate.Day > 0) AND 'msg6 (aDate.Day <= lastDayOfMonth(aDate.Month, aDate.Year))

‘added decisional complexity = +143 Then dayOK = True44 Else dayOK = False45 EndIf46 EndIf47 If ((aDate.Year > 1811) AND (aDate.Year <= 2012)

‘added decisional complexity = +148 Then yearOK = True49 Else yearOK = False50 EndIf 51 If (monthOK AND dayOK AND yearOK)

‘added decisional complexity = +252 Then ValidDate = True53 Else ValidDate = False54 EndIf55 End (Function ValidDate)

ValidDate V(G) = 6

43

47

38

51

55

42

46

39

50

54

41

45

37

49

53

40

44

36

48

52

35

Page 44: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

' GetDate body begins here56 Do57 Output("enter a month")58 Input(aDate.Month)59 Output("enter a day")60 Input(aDate.Day)61 Output("enter a year")62 Input(aDate.Year)63 GetDate.Month = aDate.Month64 GetDate.Day = aDate.Day65 GetDate.Year = aDate.Year66 Until (ValidDate(aDate)) 'msg767 End (Function GetDate)

GetDate V(G) = 2

56

66

34

67

57 - 65

Page 45: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Total Unit Level Complexity for the Integration Version of NextDate

Unit Cyclomatic Complexity

Added Decisional Complexity

Total Unit Complexity

Main integrationNextDate 1 0 1 GetDate 2 0 2 IncrementDate 3 0 3 PrintDate 1 0 1 ValidDate 6 4 10 lastDayOfMonth 4 0 4 isLeap 4 0 4 Sum of total unit complexities 25

Page 46: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Total Complexity for the Integration Version of NextDate

•  Total unit level complexity is 25 •  Integration level cylomatic complexity is 3 •  Added message traffic complexity is 8 •  Total complexity is 36

•  Compare with total complexity of 16 for the procedural version.

Page 47: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Finding V(G) from Source Code

Mai

n

Get

Dat

e In

crem

ent

Dat

e

prin

tDat

e

Val

idD

ate

last

Day

Of

Mon

th

isle

ap

row

sum

(o

utde

gree

)

Main 1 1 1 3 GetDate 1 1 IncrementDate 1 1 printDate 0 ValidDate 1 1 lastDayOfMonth 1 1 isleap 0 column sum 0 1 1 1 1 2 1 7

Page 48: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

Message Traffic Complexity Just as not all decisions are equal, neither are all interfaces

Mai

n

Get

Dat

e In

crem

ent

Dat

e

prin

tDat

e

Val

idD

ate

last

Day

Of

Mon

th

isle

ap

row

sum

(o

utde

gree

)

Main 1 1 2 4 GetDate 1 1 IncrementDate 1 1 printDate 0 ValidDate 1 1 lastDayOfMonth 1 1 isleap 0 column sum 0 1 1 1 1 2 1 8

Page 49: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

O-O complexity—CK Metrics

•  WMC—Weighted Methods per Class •  DIT—Depth of Inheritance Tree •  NOC—Number of Child Classes •  CBO—Coupling between Classes •  RFC—Response for Class •  LCOM—Lack of Cohesion on Methods

S. R. Chidamber and C. F. Kemerer, “A metrics suite for object-oriented design,” IEEE Transactions of Software Engineering vol. 20, issue 6: pp. 476 – 493 (1994)

Page 50: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

WMC—Weighted Methods per Class

•  Number of methods in a class weighted by the cyclomatic complexity of each method.

•  Equivalent to unit level complexity •  Could be extended to include decisional complexity •  In a follow-up paper [Kemerer 2005], Kemerer

observes that this metric is a good predictor of implementation and testing effort.

•  (this has been known for decades for procedural code.)

Page 51: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

DIT—Depth of Inheritance Tree

•  Similar to a call graph •  Depth is the longest path in the inheritance

directed graph from root node to leaves. •  Expert consensus is that this depth should be

about 3 or 4 at most.

Page 52: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

NOC—Number of Child Classes

•  Also closely related to the Call Graph.

Page 53: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

CBO—Coupling between Classes

•  An idea borrowed from the work of Yourdon and Constantine.

•  Yourdon and Constantine refused to reduce coupling to a number. Instead they defined types of coupling... –  content –  common –  external –  control –  stamp –  data

•  Many of these are/were directed at languages such as FORTRAN and COBOL

Page 54: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

RFC—Response for Class

•  The length of a sequence of messages that result from an initial message.

•  (The length of an MM-Path)

Page 55: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

LCOM—Lack of Cohesion on Methods •  Another idea borrowed from the work of Yourdon and

Constantine. •  Yourdon and Constantine refused to reduce coupling to

a number. Instead they defined types of cohesion... –  coincidental –  logical –  temporal –  communication –  sequential –  functional –  data

•  Cohesion is a precursor to well-defined classes.

Page 56: Chapter 16hossein/Teaching/Fa09/814/Lectures...... A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity ... 29 30 23 26 27 28 25 32 34 3 37 28 27 22 36 23 30 22 24

Software Testing: A Craftsman’s Approach, 4th Edition Chapter 16 Software Complexity

What about System Level Complexity?

•  Consider an incidence matrix in which rows are use cases (or features) and columns are classes (or procedures)."

•  Since the use cases (or features) do not change, we can compare system level design complexity of different designs by examining the extent to which their respective incidence matrices are sparse or dense."

•  Other driving factors include... "–  size –  performance constraints –  concurrency –  difficult hardware interface –  and many more"

"