Halstead Software Science, Function Point & Cyclomatic Complexity Explained with Examples
Halstead, Function Point and Cyclomatic Complexity at a Glance
Main purpose: Estimate lexical program size, volume, difficulty and effort.
Main purpose: Measure functional software size without depending directly on Lines of Code.
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:
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.
Number of unique operators in the program.
Number of unique operands in the program.
Total occurrences of all operators.
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
Complete Halstead Metrics and Formulas
1. Program Vocabulary
Program vocabulary represents the total number of unique symbols used as operators and operands.
2. Program Length
Program length represents the total number of operator and operand occurrences.
3. Estimated Program Length
Halstead also proposed an estimated or calculated program length based on the number of distinct operators and operands.
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.
5. Program Difficulty
Difficulty attempts to represent how difficult the implementation may be based on unique operators and operand usage.
6. Program Level
Program Level is the inverse of difficulty.
A higher program level indicates a lower value of calculated difficulty.
7. Programming Effort
Halstead Effort combines Volume and Difficulty.
8. Estimated Programming Time
A traditional Halstead formulation estimates implementation time using the effort value.
9. Estimated Delivered Bugs
You may find more than one delivered-bugs equation in books, tools, and lecture notes.
One common textbook form is:
Another implementation found in some software tools is:
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.
Here, n₂* represents the number of unique input and output parameters required by the algorithm in the simplified theoretical model.
Halstead Formula Map
Solved Halstead Software Science Example
Suppose analysis of a small program gives:
n = n₁ + n₂
= 6 + 4
= 10
N = N₁ + N₂
= 8 + 7
= 15
V = N × log₂(n)
= 15 × log₂(10)
≈ 15 × 3.322
≈ 49.83
D = (n₁ / 2) × (N₂ / n₂)
= (6 / 2) × (7 / 4)
= 3 × 1.75
= 5.25
E = D × V
= 5.25 × 49.83
≈ 261.61
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.
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
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
Unadjusted Function Points
After identifying and weighting each function, calculate the total Unadjusted Function Points.
Solved Function Point 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 |
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
- Data communications
- Distributed data processing
- Performance
- Heavily used configuration
- Transaction rate
- Online data entry
- End-user efficiency
- Online update
- Complex processing
- Reusability
- Installation ease
- Operational ease
- Multiple sites
- Facilitate change
Here, ΣFi is the total influence score of the general system characteristics.
Traditional Adjusted Function Point
Example
Suppose:
- UFP = 125
- ΣFi = 40
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:
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
Cyclomatic Complexity Formulas
Formula 1: Using Edges and Nodes
Where:
- M = Cyclomatic Complexity
- E = Number of edges
- N = Number of nodes
- P = Number of connected components
For one connected program or function:
Therefore:
Formula 2: Using Decision Points
For many simple structured programs:
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.
Solved Cyclomatic Complexity Example
Suppose a Control Flow Graph contains:
Method 1: Using Graph Formula
Method 2: Using Decision Points
Both methods give:
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:
Cyclomatic Complexity and Basis Path Testing
One of the most important practical uses of Cyclomatic Complexity is Basis Path Testing.
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.
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?
You want to analyse the lexical structure of source code using operators and operands.
You want to estimate or compare software functional size from the user's perspective.
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.
Common Mistakes Students Make
Memory Tricks
Important Exam Questions
Important 5-Mark Question
- Vocabulary
- Program Length
- Volume
- Difficulty
- Effort
- Estimated Time
Important 10-Mark Question
- Definition of Function Point
- EI, EO, EQ, ILF and EIF
- Low, Average and High weight table
- UFP calculation
- Traditional VAF formula if part of your syllabus
- One solved example
- Advantages and limitations
Important Cyclomatic Complexity Question
Practice Numerical 1: Halstead
- n₁ = 8
- n₂ = 12
- N₁ = 20
- N₂ = 30
- Vocabulary
- Program Length
- Volume
- Difficulty
- Effort
Answer
Vocabulary:
Program Length:
Volume:
Difficulty:
Effort:
Practice Numerical 2: Function Point
- EI = 6
- EO = 4
- EQ = 3
- ILF = 2
- EIF = 1
Answer
Practice Numerical 3: Cyclomatic Complexity
- E = 14 edges
- N = 11 nodes
- P = 1 connected component
Answer
Therefore, the graph has 5 independent basis paths.
Quick Revision Notes
Operators + Operands
n = n₁ + n₂
N = N₁ + N₂
V = N log₂n
D = (n₁/2)(N₂/n₂)
E = D × V
User-visible functionality
EI, EO, EQ, ILF, EIF
Σ Count × Weight
M = E - N + 2P
M = Decisions + 1
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.
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.
