Halstead Software Science, Function Point & Cyclomatic Complexity Explained with Examples

Software Engineering • Chapter 24

Halstead Software Science, Function Point & Cyclomatic Complexity Explained with Examples

Software metrics become more interesting when we move beyond simply counting Lines of Code. Three important techniques that students repeatedly meet in Software Engineering are Halstead Software Science, Function Point Analysis, and Cyclomatic Complexity. They do not measure exactly the same thing. Halstead studies the operators and operands present in program code. Function Point measures the amount of functionality delivered to the user. Cyclomatic Complexity studies the control flow of a program and estimates the number of independent execution paths. Once this difference is clear, all three techniques become much easier to understand.
Direct Answer Halstead Software Science measures a program using the number of operators and operands. Function Point Analysis measures software size from the functionality visible to the user, such as inputs, outputs, inquiries and logical files. Cyclomatic Complexity measures control-flow complexity by counting the number of linearly independent paths through a program.

Halstead, Function Point and Cyclomatic Complexity at a Glance

Halstead Software Science, Function Point & Cyclomatic Complexity Explained with Examples
Halstead Software Science Looks at: Operators and operands

Main purpose: Estimate lexical program size, volume, difficulty and effort.
Function Point Looks at: User-visible functionality

Main purpose: Measure functional software size without depending directly on Lines of Code.
Cyclomatic Complexity Looks at: Program control flow

Main purpose: Measure independent execution paths and support basis-path testing.

What is Halstead Software Science?

Halstead Software Science is a family of software metrics introduced by Maurice Halstead. Instead of measuring only the number of source-code lines, Halstead studies the operators and operands used in a program.

The basic idea is simple:

Halstead Thinking A computer program is made from a vocabulary of: Operators + Operands

By counting how many different operators and operands exist, and how often they occur, several other software measures can be calculated.

Four Basic Halstead Counts

Almost every Halstead formula starts with four basic values.

n₁
Distinct Operators
Number of unique operators in the program.
n₂
Distinct Operands
Number of unique operands in the program.
N₁
Total Operators
Total occurrences of all operators.
N₂
Total Operands
Total occurrences of all operands.

What is an Operator?

An operator represents an operation performed by the program.

Depending on the language and counting convention, examples may include:

  • +
  • -
  • *
  • /
  • =
  • ==
  • >
  • <
  • return
  • function calls or other language constructs according to the counting rule

What is an Operand?

Operands are the values or data items on which operators act.

Typical examples include:

  • Variables
  • Constants
  • Literal values
  • Identifiers
  • Parameters
Important Halstead Rule Operator and operand classification can depend on the programming language and the measurement tool. Therefore, when comparing two programs, use the same counting convention.

Complete Halstead Metrics and Formulas

1. Program Vocabulary

Program vocabulary represents the total number of unique symbols used as operators and operands.

Program Vocabulary
n = n₁ + n₂

2. Program Length

Program length represents the total number of operator and operand occurrences.

Actual Program Length
N = N₁ + N₂

3. Estimated Program Length

Halstead also proposed an estimated or calculated program length based on the number of distinct operators and operands.

Estimated Program Length
N̂ = n₁ log₂(n₁) + n₂ log₂(n₂)

This calculated value can be compared with the actual program length.

4. Program Volume

Program Volume represents the amount of information contained in the program based on its length and vocabulary.

Program Volume
V = N × log₂(n)
Easy Meaning A program becomes larger in Halstead terms when it contains more tokens and/or uses a larger vocabulary.

5. Program Difficulty

Difficulty attempts to represent how difficult the implementation may be based on unique operators and operand usage.

Difficulty
D = (n₁ / 2) × (N₂ / n₂)

6. Program Level

Program Level is the inverse of difficulty.

Program Level
L = 1 / D

A higher program level indicates a lower value of calculated difficulty.

7. Programming Effort

Halstead Effort combines Volume and Difficulty.

Programming Effort
E = D × V

8. Estimated Programming Time

A traditional Halstead formulation estimates implementation time using the effort value.

Estimated Time
T = E / 18 seconds
Use This Carefully The value 18 is an empirical constant traditionally associated with Halstead's model. It should not be treated as a precise modern project schedule.

9. Estimated Delivered Bugs

You may find more than one delivered-bugs equation in books, tools, and lecture notes.

One common textbook form is:

B = V / 3000

Another implementation found in some software tools is:

B = E^(2/3) / 3000
Exam Advice For university exams, follow the exact Halstead bug formula given in your teacher's notes or syllabus. The basic Halstead values n₁, n₂, N₁, N₂, N, n, V, D and E are more important to understand than treating a fractional estimated-bug value as an exact prediction.

10. Potential Minimum Volume

Some deeper Halstead discussions also include the potential minimum volume, which represents the smallest theoretical volume needed to express a problem.

V* = (2 + n₂*) × log₂(2 + n₂*)

Here, n₂* represents the number of unique input and output parameters required by the algorithm in the simplified theoretical model.

Halstead Formula Map

From Basic Counts to Derived Metrics
n₁, n₂ Distinct operators and operands
→
Vocabulary n = n₁ + n₂
→
N₁, N₂ Total occurrences
→
Length N = N₁ + N₂
→
Volume V = N log₂n
→
Effort E = D × V

Solved Halstead Software Science Example

Halstead Numerical Example

Suppose analysis of a small program gives:

Distinct Operators n₁ = 6
Distinct Operands n₂ = 4
Total Operators N₁ = 8
Total Operands N₂ = 7
1
Vocabulary

n = n₁ + n₂
= 6 + 4
= 10
2
Program Length

N = N₁ + N₂
= 8 + 7
= 15
3
Program Volume

V = N × log₂(n)
= 15 × log₂(10)
≈ 15 × 3.322
≈ 49.83
4
Difficulty

D = (n₁ / 2) × (N₂ / n₂)
= (6 / 2) × (7 / 4)
= 3 × 1.75
= 5.25
5
Effort

E = D × V
= 5.25 × 49.83
≈ 261.61
6
Estimated Time

T = E / 18
= 261.61 / 18
≈ 14.53 seconds

Advantages of Halstead Software Science

Uses Program Structure

It considers operators and operands instead of only physical source-code lines.

Produces Multiple Measures

The same basic counts can be used to calculate length, vocabulary, volume, difficulty and effort.

Can Be Automated

Static-analysis tools can calculate Halstead values directly from source code.

Useful for Comparative Analysis

When the same language and counting rules are used, metrics can help compare modules.

Limitations of Halstead Metrics

Language and Tool Dependence

What is counted as an operator or operand may vary across programming languages and tools.

Requires Source Code

Most Halstead values are calculated after code exists, so they are not primarily requirements-stage measures.

Ignores Many Design Factors

Two programs can have similar Halstead values but very different architecture, security, readability or maintainability.

Derived Estimates Are Approximate

Effort, time and delivered-bug estimates should be interpreted as model outputs, not exact real-world predictions.

What is Function Point Analysis?

Function Point Analysis measures software size from the functionality delivered to the user.

This is very different from LOC-based measurement.

Simple Difference LOC asks: How much source code exists?

Function Point asks: How much functional capability does the software provide to its users?

Function Point Analysis was originally developed by Allan Albrecht at IBM and later became a widely standardised functional-size measurement approach.

Five Main Function Point Components

1. External Input (EI)

Data or control information enters the application from outside and causes internal information or behaviour to be maintained.

Examples:

  • Create student
  • Add customer
  • Submit order
  • Update employee details

2. External Output (EO)

Processed information leaves the application and includes meaningful processing or derived information.

Examples:

  • Salary report
  • Invoice
  • Result report
  • Monthly sales summary

3. External Inquiry (EQ)

A request retrieves information with relatively simple processing and normally does not update internal logical data.

Examples:

  • Search student
  • Check account balance
  • View product details

4. Internal Logical File (ILF)

A logical group of data maintained within the application boundary.

Examples:

  • Student database
  • Employee records
  • Order data

5. External Interface File (EIF)

A logical data group referenced by the application but maintained by another application.

Example:

  • External tax-rate master
  • Another system's customer data
  • External product catalogue
Easy Memory Trick Remember: EI – EO – EQ – ILF – EIF

Input → Output → Inquiry → Internal File → External File

Transactional vs Data Functions

Transactional Functions

  • External Input
  • External Output
  • External Inquiry

Data Functions

  • Internal Logical File
  • External Interface File

DET, FTR and RET

In detailed traditional Function Point counting, the complexity of a function may depend on elements such as DET, FTR and RET.

DET

Data Element Type

A unique user-recognisable, non-repeated field of data.

FTR

File Type Referenced

A logical file read or maintained by a transactional function.

RET

Record Element Type

A user-recognisable subgroup of data elements within a logical file.

Traditional Function Point Weight Table

Many university textbooks use the following low, average and high complexity weights.

Component Low Average High
External Input (EI) 3 4 6
External Output (EO) 4 5 7
External Inquiry (EQ) 3 4 6
Internal Logical File (ILF) 7 10 15
External Interface File (EIF) 5 7 10

Function Point Calculation Process

Function Point Analysis Flow
Define Scope & Boundary
→
Identify EI, EO, EQ
→
Identify ILF & EIF
→
Determine Complexity
→
Apply Weights
→
Calculate UFP

Unadjusted Function Points

After identifying and weighting each function, calculate the total Unadjusted Function Points.

Unadjusted Function Points
UFP = Σ (Number of Functions × Weight)

Solved Function Point Example

Function Point Numerical Example

Suppose all functions in the following example have average complexity.

Type Count Average Weight Total
EI 10 4 40
EO 5 5 25
EQ 4 4 16
ILF 3 10 30
EIF 2 7 14
UFP = 40 + 25 + 16 + 30 + 14
UFP = 125

Traditional Value Adjustment Factor

Many Software Engineering textbooks teach an older/traditional Function Point model in which the Unadjusted Function Point count is modified using 14 General System Characteristics.

Each characteristic is traditionally rated from 0 to 5.

14 General System Characteristics

  1. Data communications
  2. Distributed data processing
  3. Performance
  4. Heavily used configuration
  5. Transaction rate
  6. Online data entry
  7. End-user efficiency
  8. Online update
  9. Complex processing
  10. Reusability
  11. Installation ease
  12. Operational ease
  13. Multiple sites
  14. Facilitate change
Traditional Value Adjustment Factor
VAF = 0.65 + (0.01 × ΣFi)

Here, ΣFi is the total influence score of the general system characteristics.

Traditional Adjusted Function Point

Adjusted FP = UFP × VAF

Example

Suppose:

  • UFP = 125
  • ΣFi = 40
VAF = 0.65 + (0.01 × 40)
VAF = 1.05
Adjusted FP = 125 × 1.05 = 131.25
Modern vs Textbook Note Current functional-size measurement practice should follow the counting standard being used. The VAF/14-GSC calculation is still frequently taught in older university Software Engineering textbooks and exam notes, so it is important academically, but you should label it as the traditional adjusted Function Point model rather than assuming every modern Function Point standard uses it in exactly the same way.

Advantages of Function Point Analysis

Language Independent

Function Points focus on functionality rather than the number of Java, Python, C++, or other source-code lines.

Can Be Used Earlier

Functional size can be estimated from sufficiently detailed requirements before the final source code exists.

User-Oriented

It focuses on functionality recognised by the user.

Useful for Comparison

Metrics such as cost per FP, effort per FP and defects per FP can be calculated.

Limitations of Function Point Analysis

Counting Requires Training

Correct application boundaries and functional classifications require clear rules and experience.

Some Judgement Is Required

Poor requirements or inconsistent classification can affect the count.

Does Not Directly Measure Code Complexity

A Function Point count represents functional size, not the internal complexity of an algorithm.

Non-Functional Work Needs Separate Attention

Performance, security and other non-functional requirements may require additional measurement approaches.

What is Cyclomatic Complexity?

Cyclomatic Complexity is a software metric introduced by Thomas McCabe. It measures the logical complexity of a program using its control-flow structure.

The basic meaning is:

Simple Meaning Cyclomatic Complexity tells us the number of linearly independent execution paths through a program's control-flow graph.

A straight-line program with no decisions has very low complexity. Every new decision or branch can increase the number of possible execution paths.

What is a Control Flow Graph?

A Control Flow Graph, or CFG, visually represents how program control can move between statements or basic blocks.

Node

Represents a statement or a block of statements.

Edge

Represents a possible flow of control from one node to another.

Simple Program

Start Read marks if marks >= 40 print "Pass" else print "Fail" End
Simple Control Flow
Start / Read Marks
↓
marks ≥ 40 ?
↙      ↘
Print Pass
Print Fail
↘      ↙
End

Cyclomatic Complexity Formulas

Formula 1: Using Edges and Nodes

M = E - N + 2P

Where:

  • M = Cyclomatic Complexity
  • E = Number of edges
  • N = Number of nodes
  • P = Number of connected components

For one connected program or function:

P = 1

Therefore:

M = E - N + 2

Formula 2: Using Decision Points

For many simple structured programs:

M = Number of Decision Points + 1

Decision points may include constructs such as:

  • if
  • while
  • for
  • conditional branches
  • case branches, depending on the tool and counting method

Formula 3: Using Regions

In a planar control-flow graph, cyclomatic complexity can also be understood through the number of regions.

M = Number of Regions
Important Tool Difference Some modern static-analysis tools increase complexity for individual Boolean conditions inside a compound expression such as: if (a > 0 && b > 0) Traditional McCabe counting and tool-specific implementations may therefore report slightly different values. For exams, use the counting convention taught in your syllabus.

Solved Cyclomatic Complexity Example

Cyclomatic Complexity Numerical

Suppose a Control Flow Graph contains:

Edges E = 11
Nodes N = 9
Components P = 1
Decision Points 3

Method 1: Using Graph Formula

M = E - N + 2P
M = 11 - 9 + 2(1)
M = 4

Method 2: Using Decision Points

M = Decisions + 1
M = 3 + 1 = 4

Both methods give:

Cyclomatic Complexity = 4

This means there are four linearly independent basis paths in the graph.

What is an Independent Path?

An independent path is an execution path that introduces at least one edge not included in previously selected basis paths.

Suppose the program contains three decisions and has cyclomatic complexity 4. A possible basis set may contain:

Path 1: All decisions take their first branch.
Path 2: First decision changes branch while the remaining flow continues.
Path 3: Second decision introduces another independent branch.
Path 4: Third decision introduces the remaining independent branch.

Cyclomatic Complexity and Basis Path Testing

One of the most important practical uses of Cyclomatic Complexity is Basis Path Testing.

Basis Path Testing Process
Create Control Flow Graph
→
Calculate Cyclomatic Complexity
→
Identify Independent Paths
→
Design Test Cases
→
Execute Basis Paths
Very Important Exam Point Cyclomatic Complexity provides the number of linearly independent basis paths in the control-flow graph. Therefore, it is commonly used to guide the minimum basis set of path-oriented test cases.

Interpreting Cyclomatic Complexity

A higher value generally means that a unit contains more branching logic and may require more testing and understanding.

But the value should not be interpreted alone.

Lower Complexity

Usually fewer independent control-flow paths and simpler branch structure.

Higher Complexity

More independent paths, more branch combinations, and potentially greater testing and maintenance effort.

Do Not Use a Number Blindly Cyclomatic Complexity measures control-flow complexity. It does not directly measure architecture quality, security, algorithmic performance, naming quality, or business functionality.

Advantages of Cyclomatic Complexity

Simple Quantitative Measure

Control-flow complexity can be represented using a numerical value.

Useful for Testing

It supports Basis Path Testing and helps identify independent paths.

Highlights Complex Modules

Very branch-heavy functions can be identified for review or possible refactoring.

Can Be Automated

Static-analysis tools can calculate the value from source code or control flow.

Limitations of Cyclomatic Complexity

Only Control Flow

It mainly focuses on branch structure rather than every type of software complexity.

Same Number, Different Code

Two functions with equal cyclomatic complexity may still differ greatly in readability and difficulty.

Tool Counting Can Differ

Boolean expressions, switch cases, exception handling and language features may be counted differently by tools.

Not a Complete Quality Metric

A low complexity value does not guarantee that the software is correct, secure or maintainable.

Halstead vs Function Point vs Cyclomatic Complexity

Basis Halstead Function Point Cyclomatic Complexity
Main Focus Operators and operands User-visible functionality Control flow
Primary Input Source-code tokens Functional requirements Control Flow Graph / branching logic
Can Be Used Before Coding? Usually not fully Yes, with sufficient requirements Usually after detailed logic/design or code exists
Typical Output Volume, Difficulty, Effort Function Points Independent path count
Language Dependence Generally affected by language/token rules Designed to be largely technology independent Based mainly on control-flow structure
Main Use Code measurement Functional size measurement Complexity and testing
Key Formula V = N log₂n UFP = Σ Count × Weight M = E - N + 2P

Which Metric Should You Use?

Use Halstead When

You want to analyse the lexical structure of source code using operators and operands.

Use Function Points When

You want to estimate or compare software functional size from the user's perspective.

Use Cyclomatic Complexity When

You want to analyse branching, independent paths, Basis Path Testing or control-flow complexity.

Can These Metrics Be Used Together?

Yes. They measure different dimensions, so one metric does not completely replace the others.

Different Views of the Same Software
Function Point How much functionality?
+
Halstead How complex is the token vocabulary?
+
Cyclomatic Complexity How complex is the control flow?

Common Mistakes Students Make

Mistake 1: Halstead and Cyclomatic Complexity Are the Same Halstead studies operators and operands. Cyclomatic Complexity studies control flow and independent paths.
Mistake 2: Function Point is Based on LOC Function Point is a functional-size measure. It is not directly based on Lines of Code.
Mistake 3: n₁ and N₁ Are the Same n₁ = number of distinct operators. N₁ = total occurrences of operators. The same distinction applies to n₂ and N₂.
Mistake 4: Cyclomatic Complexity = Number of Conditions Only A common simple formula is: M = Decision Points + 1 Do not forget the +1.
Mistake 5: Adjusted FP and UFP Are the Same In traditional textbook models, UFP is calculated first and may then be modified using a Value Adjustment Factor.

Memory Tricks

Halstead = O² Operators + Operands
Function Point = F Functionality
Cyclomatic = Path Control-flow paths

Important Exam Questions

Q1. What is Halstead Software Science? Halstead Software Science is a family of software metrics that measures program characteristics using the number of distinct and total operators and operands.
Q2. Define n₁, n₂, N₁ and N₂. n₁ is the number of distinct operators, n₂ is the number of distinct operands, N₁ is the total number of operator occurrences, and N₂ is the total number of operand occurrences.
Q3. Write the Halstead Program Volume formula. V = N × log₂(n) where N is program length and n is program vocabulary.
Q4. What is Function Point Analysis? Function Point Analysis measures software functional size from the functionality requested by and delivered to users.
Q5. Name the five Function Point components. EI, EO, EQ, ILF and EIF.
Q6. What is Cyclomatic Complexity? Cyclomatic Complexity measures the number of linearly independent execution paths through a program's control-flow graph.
Q7. Write the Cyclomatic Complexity graph formula. M = E - N + 2P.

Important 5-Mark Question

Explain Halstead Software Science with formulas. For a good 5-mark answer, define n₁, n₂, N₁ and N₂. Then explain:
  • Vocabulary
  • Program Length
  • Volume
  • Difficulty
  • Effort
  • Estimated Time
Write at least one small numerical example.

Important 10-Mark Question

Explain Function Point Analysis with a suitable numerical example. Write:
  1. Definition of Function Point
  2. EI, EO, EQ, ILF and EIF
  3. Low, Average and High weight table
  4. UFP calculation
  5. Traditional VAF formula if part of your syllabus
  6. One solved example
  7. Advantages and limitations

Important Cyclomatic Complexity Question

Explain Cyclomatic Complexity and Basis Path Testing. Draw a simple Control Flow Graph and calculate complexity using: M = E - N + 2P and M = Number of Decision Points + 1 Then explain that the value represents the number of independent basis paths used to guide Basis Path Testing.

Practice Numerical 1: Halstead

Given:
  • n₁ = 8
  • n₂ = 12
  • N₁ = 20
  • N₂ = 30
Calculate:
  1. Vocabulary
  2. Program Length
  3. Volume
  4. Difficulty
  5. Effort

Answer

Vocabulary:

n = 8 + 12 = 20

Program Length:

N = 20 + 30 = 50

Volume:

V = 50 × log₂(20) ≈ 216.10

Difficulty:

D = (8/2) × (30/12) = 10

Effort:

E ≈ 10 × 216.10 = 2161

Practice Numerical 2: Function Point

For average-complexity functions:
  • EI = 6
  • EO = 4
  • EQ = 3
  • ILF = 2
  • EIF = 1
Calculate UFP.

Answer

UFP = (6×4) + (4×5) + (3×4) + (2×10) + (1×7)
UFP = 24 + 20 + 12 + 20 + 7 = 83

Practice Numerical 3: Cyclomatic Complexity

A control-flow graph contains:
  • E = 14 edges
  • N = 11 nodes
  • P = 1 connected component
Find Cyclomatic Complexity.

Answer

M = E - N + 2P
M = 14 - 11 + 2(1)
M = 5

Therefore, the graph has 5 independent basis paths.

Quick Revision Notes

Halstead Basis:
Operators + Operands
Vocabulary:
n = n₁ + n₂
Length:
N = N₁ + N₂
Volume:
V = N log₂n
Difficulty:
D = (n₁/2)(N₂/n₂)
Effort:
E = D × V
Function Point Basis:
User-visible functionality
FP Components:
EI, EO, EQ, ILF, EIF
UFP:
Σ Count × Weight
Cyclomatic Formula:
M = E - N + 2P
Decision Formula:
M = Decisions + 1
Main Testing Use:
Basis Path Testing

Frequently Asked Questions

What is the main difference between Halstead and Cyclomatic Complexity?

Halstead measures a program using operators and operands, while Cyclomatic Complexity measures the structure of control-flow paths.

Is Function Point based on Lines of Code?

No. Function Point Analysis measures functionality from the user perspective and is designed to avoid depending directly on source-code size.

What are the four basic Halstead values?

They are n₁, n₂, N₁ and N₂: distinct operators, distinct operands, total operator occurrences and total operand occurrences.

What are the five Function Point components?

External Input, External Output, External Inquiry, Internal Logical File and External Interface File.

What does Cyclomatic Complexity of 5 mean?

It means the control-flow graph has five linearly independent basis paths under the counting method being used.

Why is Cyclomatic Complexity useful in testing?

It helps determine a basis set of independent paths that can guide path-oriented test case design.

Which metric can be used before source code exists?

Function Point Analysis can be applied from sufficiently detailed functional requirements, while Halstead and code-level Cyclomatic Complexity normally need more detailed implementation or control-flow information.

Conclusion

Halstead Software Science, Function Point Analysis, and Cyclomatic Complexity are all software metrics, but each one looks at software from a different angle.

Halstead Software Science studies the vocabulary of a program through operators and operands and derives metrics such as Program Length, Volume, Difficulty, Effort, and estimated implementation time.

Function Point Analysis moves away from physical source-code size and measures the functionality delivered to users through External Inputs, External Outputs, External Inquiries, Internal Logical Files, and External Interface Files.

Cyclomatic Complexity focuses on program control flow. It measures the number of linearly independent paths and is especially useful in Basis Path Testing.

Halstead = Operators & Operands

Function Point = User Functionality

Cyclomatic Complexity = Independent Paths

For exams, do not try to memorise all three metrics as one topic. First remember what each one measures. After that, their formulas and numerical problems become much easier to understand.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Join Telegram Exam updates and free resources