Software Metrics (Part II). Product Metrics Product metrics are generally concerned with the...

31
Software Metrics (Part Software Metrics (Part II) II)

Transcript of Software Metrics (Part II). Product Metrics Product metrics are generally concerned with the...

Page 1: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Software Metrics (Part II)Software Metrics (Part II)

Page 2: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Product MetricsProduct Metrics

Product metrics are generally concerned Product metrics are generally concerned with the structure of the source code with the structure of the source code (example LOC).(example LOC).

Product metrics are metrics that can be Product metrics are metrics that can be calculated independent of how the calculated independent of how the document is produced.document is produced.

They can be calculated for other documents They can be calculated for other documents too. For example the number of words in a too. For example the number of words in a requirements specification.requirements specification.

Page 3: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

McCabe’s Cyclomatic NumberMcCabe’s Cyclomatic Number

McCabes cyclomatic numberMcCabes cyclomatic number (also called (also called McCabe’s complexity measure), after LOC, is one McCabe’s complexity measure), after LOC, is one of the most commonly used software metrics.of the most commonly used software metrics.

It was introduced in 1976.It was introduced in 1976.

It is based on graph theory’s cyclomatic It is based on graph theory’s cyclomatic number.number.

Thomas McCabe Sr.

http://reengineer.org/stevens/previous.htm

Page 4: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

A Little Graph TheoryA Little Graph Theory

A graph G is planar if it can be drawn on a plane A graph G is planar if it can be drawn on a plane so that none of its edges cross.so that none of its edges cross.

Planar or not?

Page 5: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Strongly Connected GraphsStrongly Connected Graphs

A directed graph G is A directed graph G is strongly connectedstrongly connected if if thee is a path from any thee is a path from any node to any other node to any other node.node.

Strongly Connected

Not Strongly Connected

Page 6: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Weakly Connected GraphsWeakly Connected Graphs

A graph G is weakly A graph G is weakly connected, if every connected, if every node can reach every node can reach every other node and you other node and you don’t care which way don’t care which way the directed edges the directed edges point.point.

Weakly Connected

Not Weakly Connected

Page 7: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Strongly Connected ComponentsStrongly Connected Components

A A strongly connected strongly connected componentcomponent of a graph of a graph G is a maximal G is a maximal subgraph of G that is subgraph of G that is strongly connected.strongly connected.

What are the strongly connected component?

2

1

3

4 5

Page 8: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Euler’s TheoremEuler’s Theorem

e = the number of edgese = the number of edges

n = the number of nodesn = the number of nodes

r = the number of regions formed by the graphr = the number of regions formed by the graph

Leonhard Euler (1707-1783) proved that for a Leonhard Euler (1707-1783) proved that for a planar graph G:planar graph G:

wherewhere

2 = n – e + r2 = n – e + r

Page 9: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Euler’s TheoremEuler’s Theorem

n = n =

2

1

3

4 5

e = e = r = r =

n – e + r =

55

66

33

22

Consider the graph below

Page 10: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

McCabe’s Cyclomatic NumberMcCabe’s Cyclomatic Number

2 = n – e + r can be rewritten to find the 2 = n – e + r can be rewritten to find the number of regions in a planar graph number of regions in a planar graph knowing the number of nodes and edges:knowing the number of nodes and edges:

r = e – n + 2r = e – n + 2

McCabe used the number of regions formed by a control flow graph as his software metric and named it “McCabe’s Complexity Measure”

Page 11: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

McCabe’s Cyclomatic NumberMcCabe’s Cyclomatic Number

McCabe generalized the number for graphs McCabe generalized the number for graphs that contained more than one strongly that contained more than one strongly connected component as:connected component as:

Cyclomatic number (C) = e – n + 2pCyclomatic number (C) = e – n + 2p

where p = number of strongly connected where p = number of strongly connected componentscomponents

Page 12: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

McCabe’s Cyclomatic NumberMcCabe’s Cyclomatic Number

Find McCabe’s cyclomatic number for the Find McCabe’s cyclomatic number for the following control flow graph:following control flow graph:

2

1

3

4 5

Page 13: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Calculate McCabe’s Calculate McCabe’s cyclomatic number cyclomatic number for the following for the following code:code:

/* main routine */ x := 1; while (x = 1) {

x := 2; test ( x, 1 ); x := 3; } // End while

while (x = 1) {x := 4; x := 5; test ( x, 2 ); } //End while

While (x = 1) {

x := 6; if (x = 7) x := 8; else test ( x, 3 ); } // End while

} // End main

Page 14: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

ThresholdThreshold

McCabe analyzed large projects and found McCabe analyzed large projects and found that when the cyclomatic number for a that when the cyclomatic number for a module exceeds 10, there is a history of module exceeds 10, there is a history of many more errors and difficulties in many more errors and difficulties in maintenance.maintenance.

Page 15: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Halstead Measure (1977)Halstead Measure (1977)

Maurice Halstead did his work in the late Maurice Halstead did his work in the late 1960’s and early 1970’s.1960’s and early 1970’s.

He was a pioneer in software metrics (as He was a pioneer in software metrics (as was McCabe).was McCabe).

His goal was to find out what contributed to His goal was to find out what contributed to software complexity.software complexity.

Page 16: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Halstead MetricsHalstead Metrics

A program is considered to be a collection of A program is considered to be a collection of tokens.tokens.

Tokens are either operators or operands.Tokens are either operators or operands.

Operands were tokens that had a value Operands were tokens that had a value (typically variables or constants).(typically variables or constants).

Everything else is an operator (commas Everything else is an operator (commas parenthesis, arithmetic operators, i.e. all the parenthesis, arithmetic operators, i.e. all the C++ operators)C++ operators)

Page 17: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Halstead MetricsHalstead Metrics

All tokens that occur as pairs, triples, etc. All tokens that occur as pairs, triples, etc. count as one token (example: parenthesis).count as one token (example: parenthesis).

Halstead did not count declarations, input or Halstead did not count declarations, input or output statements, or comments do not output statements, or comments do not count as tokens.count as tokens.

Most organizations currently count all parts Most organizations currently count all parts of a program.of a program.

Page 18: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Halstead MetricsHalstead Metrics

The count of The count of uniqueunique operators is operators is ήή11 (eta (eta

one) and the count of one) and the count of uniqueunique operands is operands is ήή22

(eta two).(eta two).

z = 0;z = 0;

While x > 0While x > 0

z = z + y;z = z + y;

x = x – 1x = x – 1

End-while;End-while;

Print (z);Print (z);

Consider the code below:Consider the code below:

Operators:Operators:== WhileWhile;; ++ -->> PrintPrint ( )( )

Operands:Operands:

ήή1 1 = 8= 8

zz 00xx yy 11ήή2 2 = 5= 5

Page 19: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Halstead MetricsHalstead Metrics The length (N) of the program is the total The length (N) of the program is the total

count of operators and operands.count of operators and operands.

For example, for:For example, for:z = 0;z = 0;

While x > 0While x > 0

z = z + y;z = z + y;

x = x – 1x = x – 1

End-while;End-while;

Print (z);Print (z);

Number of operands = Number of operands = 1111

Number of operators = Number of operators = 1414

N = 25N = 25

Page 20: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Halstead MetricsHalstead Metrics The The Estimate of LengthEstimate of Length (est N or N_hat) is (est N or N_hat) is

defined as:defined as:

Est N = Est N = ήή1 1 * log* log22 ήή1 1 + + ήή22 * log * log22 ήή2 2

This formula can be used to estimate the total This formula can be used to estimate the total number of tokens, based on the number of number of tokens, based on the number of different tokens.different tokens.

The author of your book has found that if N and The author of your book has found that if N and Est N differ by more than 30%, then it may not be Est N differ by more than 30%, then it may not be reasonable to apply other software science reasonable to apply other software science measures.measures.

Then again, it may be!Then again, it may be!

Page 21: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Halstead MetricsHalstead Metrics

Halstead’s measureHalstead’s measure: :

Est N = Est N = ήή1 1 * log* log22 ήή1 1 + + ήή22 * log * log22 ήή

is based on Claude Shannon’s formula for is based on Claude Shannon’s formula for entropy (the amount of information in entropy (the amount of information in something) in information theory.something) in information theory.

H = n * log S where H = n * log S where SS is the number of is the number of possible symbols, and possible symbols, and nn the number of the number of symbols in a transmissionsymbols in a transmission

April 30, 1916 – February 24, 2001

Page 22: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Halstead MetricsHalstead Metrics

For the code:For the code:

z = 0;z = 0;

While x > 0While x > 0

z = z + y;z = z + y;

x = x – 1x = x – 1

End-while;End-while;

Print (z);Print (z);

ήή1 1 = 8= 8

ήή2 2 = 5= 5

Est N = 8 logEst N = 8 log22 8 + 5 log 8 + 5 log22 5 5

Est N = 8 * 3 + 5 * 2.32Est N = 8 * 3 + 5 * 2.32

Est N = 35.6Est N = 35.6

N = 25N = 25difference = 10.6 / 35.6 = 29.8%difference = 10.6 / 35.6 = 29.8%

Page 23: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

VolumeVolume

The volume of a program is related to the The volume of a program is related to the number of bits needed to encode the number of bits needed to encode the programprogram

The volume of a program is defined as:The volume of a program is defined as:

V = N * logV = N * log22((ήή1 + 1 + ήή22))

For our sample code:For our sample code:

V = 25 * logV = 25 * log22(8 + 5)(8 + 5) = 25 *3.7= 25 *3.7 = 92.5= 92.5

Page 24: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Potential VolumePotential Volume

The The potential volumepotential volume is the minimal size of is the minimal size of a solution solved in any language.a solution solved in any language.

Halstead defined the Halstead defined the potential volumepotential volume as: as:

V* = (2 + V* = (2 + ήή22**) log) log22((ήή22*)*)

Where Where ήή22* is the minimum set of operands * is the minimum set of operands

needed to implement the algorithm in any needed to implement the algorithm in any language .language .

Page 25: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Implementation LevelImplementation Level

The implementation level is defined as:The implementation level is defined as:

L = Potential Volume / Volume = V* / VL = Potential Volume / Volume = V* / V

This relates to how close the current This relates to how close the current implementation is to the minimal implementation is to the minimal implementation.implementation.

Page 26: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Henry–Kafura Information FlowHenry–Kafura Information Flow

Sallie Henry and Dennis Kafurn developed a Sallie Henry and Dennis Kafurn developed a software metric to measure the intermodule software metric to measure the intermodule complexity of code. complexity of code.

For a module, a count is made of all the For a module, a count is made of all the information flows into the module (fan-ins) information flows into the module (fan-ins) and all the information flows out of the and all the information flows out of the module (fan-outs).module (fan-outs).

Page 27: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Henry–Kafura Information FlowHenry–Kafura Information Flow The information flows include:The information flows include:

ParametersParameters

Global variablesGlobal variables

Inputs and outputsInputs and outputs

HK = weight * (out * in)HK = weight * (out * in)22

Where the weight is determined by the Where the weight is determined by the size of the module and the HK’s are size of the module and the HK’s are summed over all modules.summed over all modules.

Their formula is (for a single module)Their formula is (for a single module)

Page 28: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Advantages: Henry–Kafura Advantages: Henry–Kafura Information FlowInformation Flow

They look at higher level complexity, while They look at higher level complexity, while McCabe looks at lower level complexity. McCabe looks at lower level complexity. McCabe’s complexity can only be applied McCabe’s complexity can only be applied when you have detail source code or when you have detail source code or pseudo-code. pseudo-code.

Henry and Kafura’s complexity can be Henry and Kafura’s complexity can be applied during design phase and also at applied during design phase and also at scoping phase if proper information flow scoping phase if proper information flow block diagrams are drawnblock diagrams are drawn

Page 29: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Disadvantages: Henry–Kafura Disadvantages: Henry–Kafura Information FlowInformation Flow

If fan-in or fan-out is zero the whole If fan-in or fan-out is zero the whole complexity will become zero. complexity will become zero.

Just imagine you are using Henry and Just imagine you are using Henry and Kafura’s complexity and fan-in or fan-out is Kafura’s complexity and fan-in or fan-out is zero the quotation is zero Dollars.zero the quotation is zero Dollars.

Page 30: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

Disadvantages: Henry–Kafura Disadvantages: Henry–Kafura Information FlowInformation Flow

The length factor is a completely different The length factor is a completely different attribute. attribute.

Multiplication of the factor with complexity Multiplication of the factor with complexity has been questioned by experts.has been questioned by experts.

Arthimetic calculations can be performed on Arthimetic calculations can be performed on measurements of similar type. measurements of similar type.

Example adding 1 litre + 10 volts will yield Example adding 1 litre + 10 volts will yield nothing sensiblenothing sensible

Page 31: Software Metrics (Part II). Product Metrics  Product metrics are generally concerned with the structure of the source code (example LOC).  Product metrics.

The EndThe End