nov 200291.3913 r. mcfadyen1 metrics fan-in/fan-out lines of code cyclomatic complexity* comment...

29
Nov 2002 91.3913 R. McFady en 1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional nesting Fog index Weighted methods per class Response for a class Lack of cohesion of methods* Coupling between objects* Depth of inheritance tree Number of children Number of overriding operations Traditional Object-Oriented * November 2002 see http://satc.gsfc.nasa.gov/support/STC_APR98/apply_oo/appl y_oo.doc

Post on 15-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 1

Metrics

Fan-in/fan-out

Lines of code

Cyclomatic complexity*

Comment percentage

Length of identifiers

Depth of conditional nesting

Fog index

Weighted methods per class

Response for a class

Lack of cohesion of methods*

Coupling between objects*

Depth of inheritance tree

Number of children

Number of overriding operations

Traditional Object-Oriented

* November 2002

see http://satc.gsfc.nasa.gov/support/STC_APR98/apply_oo/apply_oo.doc

Page 2: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 2

Traditional Metrics

Fan-in/fan-out Fan-in is a measure of the number of functions that call some other function, say X

Fan-out is a measure of the number of functions which are called by a function, say X.

(See RFC)

Cyclomatic complexity

CC is a count of the number of test cases required to test comprehensively

Page 3: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 3

Traditional Metrics

Lines of Code LOC can be counted as•lines (LOC)•non-comment non-blank lines (NCNB)•executable statements (EXEC)

Note that NASA uses executable lines of code to evaluate size

Comment percentage

Calculated as the total number of comments divided by the {total lines of code less the number of blank lines}

Page 4: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 4

Traditional Metrics

Depth of conditional nesting

Measure of the depth of nesting of IF statements in programs

Fog index Measure of the average length of words and sentences in documents. The higher the value for the Fog index, the more difficult ...

Length of identifiers

Measures average length of distinct identifiers

Page 5: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 5

OO Metrics

Weighted methods per class

WMC is the number of methods included in a class weighted by the complexity of each method

Response for a class

RFC is a count of all methods that can be invoked in response to a message to an object of the class

Page 6: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 6

OO Metrics

Lack of cohesion of methods

LOCM measures the dissimilarity of methods in a class. A highly cohesive module stands alone. Lack of cohesion increases complexity.

Coupling between objects

CBO is a count of the number of other classes to which a class is coupled. Measured by counting the number of distinct non-inheritance related class hierarchies on which a class depends

Page 7: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 7

OO Metrics

Depth of inheritance tree

DIT is the maximum number of steps from the class node to the root of the tree

Number of children

NOC is the number of immediate subclasses subordinate to a class in the hierarchy

Number of overriding operations

Number of operations in a superclass that are overridden in a subclass

Page 8: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 8

WMC

Weighted methods per class

WMC is the number of methods included in a class weighted by the complexity of each method

How does one count this?

a) simply count the number of methods, or,

b) sum the complexities of the methods (complexity might be rated: 1 Simple, 2 Moderately complex, 3 Complex)

Page 9: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 9

WMC

Shows that most classes

have less than 20

methods, … but some

have over 200 methods.

Page 10: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 10

RFC

Response for a class

RFC is a count of all methods that can be invoked in response to a message to an object of the class

How does one count this?

For a class, we count the number of distinct methods that can be invoked in response to a message, including all methods within the class hierarchy

For a class, we count all messages that can be sent by all methods in the class

Page 11: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 11

RFC

In this case, about half th

e

classes have an RFC <=20

Page 12: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 12

LCOM

Lack of cohesion of methods

LOCM measures the dissimilarity of methods in a class. A highly cohesive module stands alone. Lack of cohesion increases complexity.

How does one count this?

We need a way of measuring cohesiveness (or not).

Possibilities:

a) compute the average over all attributes in a class

number of methods using the attribute

total number of methods

convert to a percentage and subtract from 100

Page 13: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 13

LCOM

Possibilities (continued):

b) for each method, create a set of the attributes it accesses. For each pair of methods, perform the intersection of attributes. If an intersection is not empty put the set in P; if it is empty put the set in Q.

The measure is the difference in cardinalities of P and Q:

LCOM = P - Q if P > Q

otherwise LCOM = 0

Page 14: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 14

LCOM

Page 15: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 15

CBO

Coupling between objects

CBO is a count of the number of other classes to which a class is coupled. Measured by counting the number of distinct non-inheritance related class hierarchies on which a class depends

How does one count this?

• count the number of distinct noninheritance-related class hierarchies on which a class depends

Page 16: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 16

CBO

A CBO value of zero

corresponds to a class

that is only coupled to

others in its

related

inheritance hierarchy

Page 17: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 17

DIT

Depth of inheritance tree

Most classes are at

level 0 and 1

DIT is the maximum number of steps from the class node to the root of the tree

Page 18: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 18

NOC

Number of children

NOC is the number of immediate subclasses subordinate to a class in the hierarchy

How does one count this?

• the number of immediate subclasses subordinate to a class in the hierarchy

Page 19: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 19

NOC

One o

f the

se cl

asse

s, at

dept

h 3,

has

alm

ost 4

0

child

ren!

Page 20: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 20

CC

Cyclomatic complexity

CC is a count of the number of test cases required to test comprehensively

CC measures the amount of decision logic in a module.

A value of 10 or less is the general recommendation.

Formally, based on a program’s flowgraph,

Cyclomatic complexity

= number of edges

– number of nodes

+ 2

Page 21: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 21

CC

Control flowgraphs describe the logic structure of software modules.

• A module corresponds to a single function or subroutine in typical languages, has a single entry and exit point, and is able to be used as a design component via a call/return mechanism.

Each flowgraph consists of nodes and edges.

• nodes represent computational statements or expressions,

• edges represent transfer of control between nodes.

Page 22: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 22

Flowgraph Construction - Sequence

x=z

y=7

y=10

Sequences are reducible to a single node:

CC=?

Page 23: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 23

Flowgraph Construction – If then

if x>4 then

y=7

end ifCC=

Page 24: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 24

Flowgraph Construction – If Else

if x>4 then

y=7

else

y=10

end if

CC=

Page 25: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 25

Flowgraph Construction - While

while x>0 do

x=x/2

end while

CC=

Page 26: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 26

Flowgraph Construction – Do Until

Do

Read x

Until x=0

CC=

Page 27: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 27

Program

FindMean (FILE ScoreFile){ float SumOfScores = 0.0;

int NumberOfScores = 0; float Mean=0.0; float Score;Read(ScoreFile, Score);while (! EOF(ScoreFile) {

if (Score > 0.0 ) {SumOfScores = SumOfScores + Score;NumberOfScores++;}

Read(ScoreFile, Score);}/* Compute the mean and print the result */if (NumberOfScores > 0) {

Mean = SumOfScores / NumberOfScores;printf(“ The mean score is %f\n”, Mean);

} elseprintf (“No scores found in file\n”);

}

1

2

3

4

5

7

6

89

Page 28: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 28

Flowgraph

4

3

2

1

5

6

7 8

9

Cyclomatic complexity

= CC

= 11-9+2 = 4

decision structures:

•one while

•one if then

•one if else

Page 29: Nov 200291.3913 R. McFadyen1 Metrics Fan-in/fan-out Lines of code Cyclomatic complexity* Comment percentage Length of identifiers Depth of conditional

Nov 2002 91.3913 R. McFadyen 29

CC