counting standards c c

12
8/21/2019 Counting Standards c c http://slidepdf.com/reader/full/counting-standards-c-c 1/12  C/C++ CodeCount™ Counting Standard University of Southern California Center for Systems and Software Engineering June , 2007

Upload: nova514

Post on 07-Aug-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 1/12

 

C/C++ CodeCount™ 

Counting Standard 

University of Southern California

Center for Systems and Software Engineering 

June , 2007

Page 2: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 2/12

Center for Systems and Software Engineering 2013

2

Revision Sheet 

Date Version Revision Description Author

6/22/2007 1.0 Original Release CSSE

10/16/2007 1.1 Updated the example ! s"it#h statement $u %gu&en

1/2/201' 1.2 Updated d#ument template CSSE1/1(/201' 1.' )dded #&#lmati# #mplexit& CSSE

Page 3: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 3/12

Center for Systems and Software Engineering 2013

3

Table of Contents

No Contents !age No

1.0 *e!initins 

1.1 S+OC 

1.2 h&si#al S+OC 

1.' +gi#al S+OC 

1.( *ata de#laratin line 

1.- Cmpiler dire#tie 

1.6 lan line 

1.7 Cmment line 

1. Exe#utale line ! #de 

(

(

(

(

(

(

-

-

-

2.0 Che#list !r sur#e statement #unts  6

'.0 Examples ! lgi#al S+OC #unting 

'.1 Exe#utale +ines 

'.1.1 Sele#tin Statements 

'.1.2 3teratin Statements 

'.1.' Jump Statements 

'.1.( Expressin Statements 

'.1.- l# Statements 

'.2 *e#laratin lines 

'.' Cmpiler dire#ties 

7

7

7

4

10

10

11

11

(.0 C&#lmati# Cmplexit&  12

Page 4: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 4/12

Center for Systems and Software Engineering 2013

4

1.  Definitions

1.1. 

S"#C $ Sur#e +ines ! Cde is a unit used t measure the si5e ! s!t"are prgram. S+OC #unts the

prgram sur#e #de ased n a #ertain set ! rules. S+OC is a e& input !r estimating pre#t e!!rt and is

als used t #al#ulate prdu#tiit& and ther measurements.

1.2. 

!hysica% S"#C $ One ph&si#al S+OC is #rrespnding t ne line starting "ith the !irst #hara#ter and ending

& a #arriage return r an end!!ile marer ! the same line, and "hi#h ex#ludes the lan and #mment

line.

1.'.  "ogica% S"#C $ +ines ! #de intended t measure 8statements9, "hi#h nrmall& terminate & a semi#ln

:C/C;;, Jaa, C<= r a #arriage return :$, )sseml&=, et#. +gi#al S+OC are nt sensitie t !rmat and st&le

#nentins, ut the& are languagedependent.

1.(. 

Data dec%aration %ine or data %ine $ ) line that #ntains de#laratin ! data and used & an assemler

#mpiler t interpret ther elements ! the prgram.

>he !ll"ing tale lists the C/C;; e&"rds that dente data de#laratin lines?

Simp%e Data &ypesCompound and 'ser

Defined Data &ypesAccess Specifiers &ype (ua%ifiers

l #lass priate #nst

#har stru#t prte#ted latile

dule unin puli#

!lat enum !riend

int t&pede! Storage C%ass Specifiers  )isce%%aneous 

lng template  aut asm

shrt t&pename extern  expli#it

signed mutale inline

unsigned register namespa#e

id stati# using

"#har@t peratr

irtual

&a*%e Data Dec%aration &ypes

1.-. 

Compi%er Directives $ ) statement that tells the #mpiler h" t #mpile a prgram, ut nt "hat t

#mpile. 

>he !ll"ing tale lists the C/C;; e&"rds that dente #mpiler dire#tie lines?

<de!ine <i!nde! <in#lude <di#tinar&

<unde! <else <line <mdule

<i! <eli! <pragma <imprt

<i!de! <endi! <errr <using

&a*%e , Compi%er Directives

Page 5: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 5/12

Center for Systems and Software Engineering 2013

5

1.6. 

-%an. "ine $ ) ph&si#al line ! #de, "hi#h #ntains an& numer ! "hite spa#e #hara#ters :spa#es, tas

!rm !eed, #arriage return, line !eed, r their deriaties=. 

1.7. 

Comment "ine $ ) #mment is de!ined as a string ! 5er r mre #hara#ters that !ll" languagespe#i!i#

#mment delimiter. 

C/C;; #mment delimiters are 8//9 and 8/A9. ) "hle #mment line ma& span ne line and des n

#ntain an& #mpilale sur#e #de. )n emedded #mment #an #exist "ith #mpilale sur#e #de n

the same ph&si#al line. anners and empt& #mments are treated as t&pes ! #mments. 

1.. 

Eecuta*%e "ine of code $ ) line that #ntains s!t"are instru#tin exe#uted during runtime and n "hi#h a

reapint #an e set in a deugging tl. )n instru#tin #an e stated in a simple r #mpund !rm. 

•  )n exe#utale line ! #de ma& #ntain the !ll"ing prgram #ntrl statements?

  Sele#tin statements :i!, B peratr, s"it#h=

  3teratin statements :!r, "hile, d"hile=

 

Empt& statements :ne r mre 89=

  Jump statements :return, gt, rea, #ntinue, exit !un#tin=

  Expressin statements :!un#tin #alls, assignment statements, peratins, et#.=

  l# statements

•  )n exe#utale line ! #de ma& nt #ntain the !ll"ing statements?

  Cmpiler dire#ties

  *ata de#laratin :data= lines

  Dhle line #mments, in#luding empt& #mments and anners

  lan lines

Page 6: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 6/12

Center for Systems and Software Engineering 2013

6

2.  Checklist for source statement counts

!01S2CA" S"#C C#'N&2N3 R'"ES 

E)SUREE%> U%3>

OR*ER OF

RECE*E%CE GHS3C)+ S+OC COE%>S

Eecuta*%e "ines 1 One er line *e!ined in 1.

Non4eecuta*%e "ines

*e#laratin :*ata= lines 2 One per line *e!ined in 1.(

Cmpiler *ire#ties ' One per line *e!ined in 1.-

Cmments *e!ined in 1.7

On their "n lines ( %t 3n#luded :%3=

Emedded - %3

anners 6 %3

Empt& Cmments 7 %3

lan +ines %3 *e!ined in 1.6

"#32CA" S"#C C#'N&2N3 R'"ES 

%O.  S>RUC>UREOR*ER OF

RECE*E%CE +OI3C)+ S+OC RU+ES  COE%>S

R01 8!r9, 8"hile9 r 8i!9 statement 1 Cunt On#e 8"hile9 is an independent

statement.

R02 do {…} while (…); statement   2 Cunt On#e ra#es KL and semi#ln

used "ith this statement

are nt #unted.R0' Statements ending & a

semi#ln

' Cunt n#e per statement,

in#luding empt& statement

Semi#lns "ithin 8!r9

statement are nt

#unted. Semi#lns used

"ith R01 and R02 are nt

#unted.

R0( l# delimiters, ra#es KL ( Cunt n#e per pair ! ra#es

..L, ex#ept "here a #lsing

ra#e is !ll"ed & a

semi#ln, i.e. Lr an

pening ra#e #mes a!ter a

e&"rd 8else9.

ra#es used "ith R01 and

R02 are nt #unted.

Fun#tin de!initin is

#unted n#e sin#e it is

!ll"ed & KL.

R0- Cmpiler *ire#tie - Cunt n#e per dire#tie

Page 7: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 7/12

Center for Systems and Software Engineering 2013

7

3.  Examples

E5EC'&A-"E "2NES 

SE"EC&2#N Statement

ESS $ if6 e%se if6 e%se and nested if statements

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

i! :Nlean expressin=

Nstatements

i! :Nlean expressin= Nstatement

else Nstatement

i! :Nlean expressin=

Nstatements

else i! :Nlean expressin=

Nstatements.

.

.

else Nstatements

i! :Nlean expressin=

Nstatements

L

else

Nstatements

L

N#&E7  #mplexit& is nt #nsidered, i.e.

multiple 8PP9 r 8QQ9 as part ! the

expressin.

i! :x 0=

print! :8nn5er9=

i! :x 0= print! :8psitie9=

else print! :8negatie9=

i! :x 0=

print! :85er9=

else i! :x 0=

print! :8psitie9=

else

print! :8negatie9=

i! ::x 0= PP :x 0==

print! :8Td9, x=

i! :x 0=

print! :8nn5er9=

L

else

print! :85er9=

L

1

1

2

1

1

1

1

1

0

1

1

1

10

1

0

0

0

1

0

ESS, $ 8 operator

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

Exp1BExp2?Exp' x 0 B print! :8;9= ? print! :89= 1

Page 8: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 8/12

Center for Systems and Software Engineering 2013

8

ESS9 $ switch and nested switch statements

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

s"it#h :Nexpressin=

#ase N#nstant 1 ?

Nstatementsrea

#ase N#nstant 2 ?

Nstatements

rea

#ase N#nstant ' ?

Nstatements

rea

de!ault

Nstatements

L

s"it#h :numer=

#ase 1?

#ase 11?!1:=

rea

#ase 2?

!2:=

rea

#ase '?

!':=

rea

de!ault

print! :8inalid #ase9=

L

1

0

0

01

1

0

1

1

0

1

1

0

1

0

ESS: $ try4catch

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

tr& L #at#h:= L tr&

#ut NN Calling !un# Vn

L

Cat#h:3OEx#eptin e=

#ut NN 8Errr? 8 NN e

L

1

0

1

0

1

0

1

0

2&ERA&2#N Statement

E2S $ for

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

!r :initiali5atin #nditin in#rement=

statement 

N#&E7 8!r9 statement #unts as ne, n

matter h" man& ptinal expressins it

#ntains, i.e.

!r :i 0, 0 i N -, N 10 i;;, ,;;=

!r :i 0 i N 10 i;;=

print! :8Td9, i=

!r :i 0 i N 10 i;;=

print! :8Td9, i=

L

1

1

1

0

1

0

Page 9: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 9/12

Center for Systems and Software Engineering 2013

9

E2S, $ empty statements ;cou%d *e used for time de%ays<

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

!r :i 0 i N SOE@$)+UE i;;= !r :i 0 i N 10 i;;= 2

E2S9 $ whi%e

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

"hile :Nlean expressin=

Nstatement

"hile :i N 10=

print! :8Td9, i=

i;;

L

1

0

1

1

0

E2S: $ do4whi%e

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

d

Nstatements

L "hile :Nlean expressin=

d

#h get#har:=

L "hile :#h WVnX=

00

1

1

=')! Statement

E=S $ return

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

return expressin 3! :i0= return 2

E=S, $ goto6 %a*e%

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

gt lael lp1?

x;;

i! :x N &= gt lp1

0

1

2

E=S9 $ *rea.

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

rea i! :i 10= rea 2

E=S: $ eit function

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

id exit :int return@#de= i! :x N 0= exit :1= 2

Page 10: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 10/12

Center for Systems and Software Engineering 2013

10

E=S> $ continue

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

#ntinue "hile :dne=

#h get#har:=

i! :#har WVnX=

dne true

#ntinue

L

L

1

0

1

10

1

1

0

0

E5!RESS2#N Statement

EES $ function ca%%

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

N!un#tin@name : Nparameters = read@!ile :name= 1

EES, $ assignment statement

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

Nname Nalue x &

#har nameY6Z 8!ile19

a 1 2 # '

1

1

'

EES9 $ empty statement ;is counted as it is considered to *e a p%aceho%der for something to ca%%attention<

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

ne r mre 89 in su##essin 1 per ea#h

-"#C? Statement

E-S $ *%oc.@re%ated statements treated as a unit

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

/A start ! l# A/

Nde!initins

Nstatement

L

/A end ! l# A/

/A start ! l# A/

i 0

print! :8Td9, i=

L

/A end ! l# A/

0

0

1

1

1

0

Page 11: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 11/12

Center for Systems and Software Engineering 2013

11

DEC"ARA&2#N #R DA&A "2NES 

DD" $ function prototype6 varia*%e dec%aration6 struct dec%aration6 typedef

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

Nt&pe Nname : N parameter@list =

Nt&pe Nname

stru#t Nname

Nt&pe Nname

Nt&pe Nname

L

stru#t

Nt&pe Nname

Nt&pe Nname

L Nname

t&pede! Nt&pe Nname

t&pede! stru#t Nname

Nt&pe Nname

K

L Nstru#t@name

using namespa#e Nname

Nt&pe Nname : N parameter@list =

K

L

id ! :int param=

dule amunt, pri#e

int index

stru#t S

int x

int &

L

stru#t

int x

int &

L S

t&pede! int H@3%>

t&pede! stru#t S

int i

#har #h

L Nstru#t@name

using namespa#e std

id main:=

print!:8hell9=

L

1

1

1

0

0

1

1

1

00

1

1

2

1

0

0

1

1

2

1

0

0

1

1

C#)!2"ER D2REC&2VES 

CD" $ directive types

IE%ER)+ EM)+E SEC3F3C EM)+E S+OC COU%>

<de!ine Nname Nalue

<in#lude Nlirar&@name

<de!ine )M@S3[E 100

<in#lude Nstdi.h

1

1

Page 12: Counting Standards c c

8/21/2019 Counting Standards c c

http://slidepdf.com/reader/full/counting-standards-c-c 12/12

Center for Systems and Software Engineering 2013

12

4.  Cyclomatic Complexity

C&#lmati# #mplexit& measures the numer ! linearl& independent paths thrugh a prgram. 3t is measured !r ea#h

!un#tin, pr#edure, r methd a##rding t ea#h spe#i!i# prgram language. >his metri# indi#ates the ris ! prgram

#mplexit& and als determines the numer ! independent test re\uired t eri!& prgram #erage.

>he #&#lmati# #mplexit& is #mputed & #unting the numer ! de#isins plus ne !r the linear path. *e#isins are

determined & the numer ! #nditinal statements in a !un#tin. ) !un#tin "ithut an& de#isins "uld hae a

#&#lmati# #mplexit& ! ne. Ea#h de#isin su#h as an i! #nditin r a !r lp adds ne t the #&#lmati# #mplexit&.

>he #&#lmati# #mplexit& metri# :I= "as de!ined & >hmas #Cae. Seeral ariatins are #mmnl& used ut are

nt in#luded in the UCC. >he mdi!ied #&#lmati# #mplexit& #unts sele#t l#s as a single de#isin rather than

#unting ea#h #ase. >he stri#t r extended #&#lmati# #mplexit& in#ludes lean peratrs "ithin #nditina

statements as additinal de#isins.

Cyc%omatic Comp%eity Ris. Eva%uation

110 ) simple prgram, "ithut mu#h ris

1120 re #mplex, mderate ris

21-0 Cmplex, high ris prgram

-0 Untestale prgram, er& high ris

Fr C/C;;, the !ll"ing tale lists the #nditinal e&"rds used t #mpute #&#lmati# #mplexit&.

Statement CC Count Rationa%e

i! ;1 i! adds a de#isin

else i! ;1 else i! adds a de#isin

else 0 *e#isin is at the i! statement

s"it#h #ase ;1 per #ase Ea#h #ase adds a de#isin ] nt the s"it#h

s"it#h de!ault 0 *e#isin is at the #ase statements

!r ;1 !r adds a de#isin at lp start

"hile ;1 "hile adds a de#isin at lp start r at end ! d lp

d 0 *e#isin is at "hile statement ] n de#isin at un#nditinal lp

tr& 0 *e#isin is at #at#h statement

#at#h ;1 #at#h adds a de#isin

ternar& B ? ;1 >ernar& B adds a de#isin ] ? is similar t de!ault r else