0% found this document useful (0 votes)
51 views15 pages

Cyclomatic and Loc Complexity

Lines of code (LOC) is used to measure the volume of code in a software project by counting non-comment non-blank lines. LOC is commonly used to estimate project size and compare projects using the same language and coding standards. While easy to calculate, LOC does not consider code complexity and quality. Cyclomatic complexity measures the logical complexity of code by counting the number of independent paths through the program. It provides insights into testability, maintainability, and development effort needed for a software project.

Uploaded by

poonam Bhalla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views15 pages

Cyclomatic and Loc Complexity

Lines of code (LOC) is used to measure the volume of code in a software project by counting non-comment non-blank lines. LOC is commonly used to estimate project size and compare projects using the same language and coding standards. While easy to calculate, LOC does not consider code complexity and quality. Cyclomatic complexity measures the logical complexity of code by counting the number of independent paths through the program. It provides insights into testability, maintainability, and development effort needed for a software project.

Uploaded by

poonam Bhalla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Lines of Code (LOC) in Software Engineering

A line of code (LOC) is any line of text in a code that is not a


comment or blank line, and also header lines, in any case of the
number of statements or fragments of statements on the line. LOC
clearly consists of all lines containing the declaration of any
variable, and executable and non-executable statements. As Lines
of Code (LOC) only counts the volume of code, you can only use it
to compare or estimate projects that use the same language and
are coded using the same coding standards.
Features
• Variations such as “source lines of code”, are
used to set out a codebase.
• LOC is frequently used in some kinds of
arguments.
• They are used in assessing a project’s
performance or efficiency.
Advantages :
• Most used metric in cost estimation.
• Its alternates have many problems as
compared to this metric.
• It is very easy in estimating the efforts.
Disadvantages
• Very difficult to estimate the LOC of the final
program from the problem specification.
• It correlates poorly with quality and efficiency
of code.
• It doesn’t consider complexity
void selSort(int x[], int n) {
//Below function sorts an array in ascending order
int i, j, min, temp;
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++)
if (x[j] < x[min])
min = j;
temp = x[i];
x[i] = x[min];
x[min] = temp;
}
}
• now If LOC is simply a count of the number of
lines then the above function shown
contains 13 lines of code (LOC). But when
comments and blank lines are ignored, the
function shown above contains 12 lines of
code (LOC).
Cyclomatic Complexity-

• Cyclomatic Complexity may be defined as-


• It is a software metric that measures the
logical complexity of the program code.
• It counts the number of decisions in the given
program code.
• It measures the number of linearly
independent paths through the program code.
Cyclomatic complexity indicates several information
about the program code
Cyclomatic Complexity Meaning

•Structured and Well Written


Code
1 – 10
•High Testability
•Less Cost and Effort
•Complex Code
10 – 20 •Medium Testability
•Medium Cost and Effort
•Very Complex Code
20 – 40 •Low Testability
•High Cost and Effort
•Highly Complex Code
> 40 •Not at all Testable
•Very High Cost and Effort
Importance of Cyclomatic Complexity


It helps in determining the software quality.It is an
important indicator of program code’s readability,
maintainability and portability.It helps the
developers and testers to determine independent
path executions.It helps to focus more on the
uncovered paths.It evaluates the risk associated
with the application or program.It provides
assurance to the developers that all the paths have
been tested at least once.
Properties of Cyclomatic Complexity-

• It is the maximum number of independent


paths through the program code.
• It depends only on the number of decisions in
the program code.
• Insertion or deletion of functional statements
from the code does not affect its cyclomatic
complexity.
• It is always greater than or equal to 1
Calculating Cyclomatic Complexity

• Cyclomatic complexity is calculated using the control


flow representation of the program code.

• In control flow representation of the program code,
• Nodes represent parts of the code having no branches.
• Edges represent possible control flow transfers during
program execution

• There are 3 commonly used methods for calculating the
cyclomatic complexity-
Method-01:

• Cyclomatic Complexity = Total number of closed


• Method-02:

• Cyclomatic Complexity = E – N + 2

• Here-
• E = Total number of edges in the control flow graph
• N = Total number of nodes in the control flow graph

• Method-03:

• Cyclomatic Complexity = P + 1

• Here,
• P = Total number of predicate nodes contained in the control flow graph
• regions in the control flow graph + 1
PRACTICE PROBLEMS BASED ON CYCLOMATIC
COMPLEXITY-

• IF A = 354
• THEN IF B > C
• THEN A = B
• ELSE A = C
• END IF
• END IF
• PRINT A
Diagram
• We draw the following control flow graph for
the given code-


• Using the above control flow graph, the cyclomatic complexity may be calculated as-

• Method-01:

• Cyclomatic Complexity
• = Total number of closed regions in the control flow graph + 1
• =2+1
• =3

• Method-02:

• Cyclomatic Complexity
• =E–N+2
• =8–7+2
• =3

• Method-03:

• Cyclomatic Complexity
• =P+1
• =2+1
• =3

You might also like