Halstead’s Software Metrics - Software Engineering
Last Updated :
12 Jun, 2024
Halstead's Software metrics are a set of measures proposed by Maurice Halstead to evaluate the complexity of a software program. These metrics are based on the number of distinct operators and operands in the program and are used to estimate the effort required to develop and maintain the program. These metrics provide a quantitative assessment of software complexity, aiding in software development, maintenance, and quality assurance processes. They include measures such as program length, vocabulary, volume, difficulty, and effort, calculated based on the number of unique operators and operands in a program. Halstead's metrics help developers understand and manage software complexity, identify potential areas for optimization, and improve overall software quality.
What is Halstead’s Software Metrics?
Halstead's Software Metrics, developed by Maurice Halstead in 1977, are a set of measures used to quantify various aspects of software programs. According to Halstead's, "A computer program is an implementation of an algorithm considered to be a collection of tokens which can be classified as either operators or operand". This means that the program consists of various symbols and data elements that are either performing actions (operators) or upon which actions are performed (operands). This distinction helps in understanding and analyzing the structure and behavior of the program.
Token Count
In Halstead’s Software metrics, a computer program is defined as a collection of tokens that can be described as operators or operands. These tokens are used to analyze the complexity and volume of a program. Operators are symbols that represent actions, while operands are the entities on which the operators act. All software science metrics can be specified using these basic symbols. These symbols are referred to as tokens. By counting and analyzing these tokens, Halstead's metrics provide insights into the complexity, effort, and quality of software code.
In Halstead's Software Metrics:
n1 = Number of distinct operators.
n2 = Number of distinct operands.
N1 = Total number of occurrences of operators.
N2 = Total number of occurrences of operands.
Field of Halstead Metrics
Program length (N):
This is the total number of operator and operand occurrences in the program.
Vocabulary size (n):
This is the total number of distinct operators and operands in the program.
Program volume (V):
This is the product of program length (N) and the logarithm of vocabulary size (n),
i.e., V = N*log2(n)
Program level (L):
This is the ratio of the number of operator occurrences to the number of operand occurrences in the program,
i.e., L = n1/n2
where n1 is the number of operator occurrences and n2 is the number of operand occurrences.
Program difficulty (D):
This is the ratio of the number of unique operators to the total number of operators in the program,
i.e., D = (n1/2) * (N2/n2)
Program effort (E):
This is the product of program volume (V) and program difficulty
(D), i.e., E = V*D
Time to implement (T):
This is the estimated time required to implement the program, based on the program effort (E) and a constant value that depends on the programming language and development environment.
Halstead's software metrics can be used to estimate the size, complexity, and effort required to develop and maintain a software program. However, they have some limitations, such as the assumption that all operators and operands are equally important, and the assumption that the same set of metrics can be used for different programming languages and development environments.
Halstead's Software Metrices
Halstead's Software Metrics are:
Halstead Program Length
Halstead Program Length (N) in Halstead's Software Metrics refers to the total number of tokens in a program. Where tokens are the smallest individual units of code such as operators, operands, keywords, and identifiers.
N = N1 + N2
The estimated program length is denoted by N^ and is given by the formula:
N^ = n1log2n1 + n2log2n2
Several alternative formulas have been proposed to estimate program length, including:
NJ = log2(n1!) + log2(n2!)NB = n1 * log2n2 + n2 * log2n1NC = n1 * sqrt(n1) + n2 * sqrt(n2)NS = (n * log2n) / 2
Halstead's,
Halstead Vocabulary
The total number of unique operators and unique operand occurrences.
n = n1 + n2
Program Volume
Proportional to program size, represents the size, in bits, of space necessary for storing the program. This parameter is dependent on specific algorithm implementation. The properties V, N, and the number of lines in the code are shown to be linearly connected and equally valid for measuring relative program size.
V = Size * (log2 vocabulary) = N * log2(n)
The unit of measurement of volume is the common unit for size “bits”. It is the actual size of a program if a uniform binary encoding for the vocabulary is used. And
error = Volume / 3000
Potential Minimum Volume
The potential minimum volume V* is defined as the volume of the most succinct program in which a problem can be coded.
V* = (2 + n2*) * log2(2 + n2*)
Here, n2* is the count of unique input and output parameters
Program Level
To rank the programming languages, the level of abstraction provided by the programming language, Program Level (L) is considered. The higher the level of a language, the less effort it takes to develop a program using that language.
L = V* / V
The value of L ranges between zero and one, with L=1 representing a program written at the highest possible level (i.e., with minimum size).
And estimated program level is
L^ =2 * (n2) / (n1)(N2)
Program Difficulty
This parameter shows how difficult to handle the program is.
D = (n1 / 2) * (N2 / n2)
D = 1 / L
As the volume of the implementation of a program increases, the program level decreases and the difficulty increases. Thus, programming practices such as redundant usage of operands, or the failure to use higher-level control constructs will tend to increase the volume as well as the difficulty.
Programming Effort
Measures the amount of mental activity needed to translate the existing algorithm into implementation in the specified program language.
E = V / L = D * V = Difficulty * Volume
Language Level
Shows the algorithm implementation program language level. The same algorithm demands additional effort if it is written in a low-level program language. For example, it is easier to program in Pascal than in Assembler.
L' = V / D / D lambda = L * V* = L2 * V
Intelligence Content
Determines the amount of intelligence presented (stated) in the program This parameter provides a measurement of program complexity, independently of the programming language in which it was implemented.
I = V / D
Programming Time
Shows time (in minutes) needed to translate the existing algorithm into implementation in the specified program language.
T = E / (f * S)
The concept of the processing rate of the human brain, developed by psychologist John Stroud, is also used. Stoud defined a moment as the time required by the human brain to carry out the most elementary decision. The Stoud number S is therefore Stoud's moments per second with:
5 <= S <= 20. Halstead uses 18. The value of S has been empirically developed from psychological reasoning, and its recommended value for programming applications is 18.
Stroud number S = 18 moments / second
seconds-to-minutes factor f = 60
Example of Halstead's Software Metrices
Before we look at the example, let's review the counting rules for a C program.
Counting Rules for C Language
- Comments are not considered.
- The identifier and function declarations are not considered
- All the variables and constants are considered operands.
- Global variables used in different modules of the same program are counted as multiple occurrences of the same variable.
- Local variables with the same name in different functions are counted as unique operands.
- Functions calls are considered operators.
- All looping statements e.g., do {…} while ( ), while ( ) {…}, for ( ) {…}, all control statements e.g., if ( ) {…}, if ( ) {…} else {…}, etc. are considered as operators.
- In control construct switch ( ) {case:…}, switch as well as all the case statements are considered as operators.
- The reserve words like return, default, continue, break, size, etc., are considered operators.
- All the brackets, commas, and terminators are considered operators.
- GOTO is counted as an operator and the label is counted as an operand.
- The unary and binary occurrences of “+” and “-” are dealt with separately. Similarly “*” (multiplication operator) is dealt with separately.
- In the array variables such as “array-name [index]” “array-name” and “index” are considered as operands and [ ] is considered as operator.
- In the structure variables such as “struct-name, member-name” or “struct-name -> member-name”, struct-name, and member-name are taken as operands, and ‘.’, ‘->’ are taken as operators. Some names of member elements in different structure variables are counted as unique operands.
- All the hash directives are ignored.
Let's examine the following C program
int sort (int x[ ], int n)
{
int i, j, save, im1;
/*This function sorts array x in ascending order */
If (n< 2) return 1;
for (i=2; i< =n; i++)
{
im1=i-1;
for (j=1; j< =im1; j++)
if (x[i] < x[j])
{
Save = x[i];
x[i] = x[j];
x[j] = save;
}
}
return 0;
}
Explanation
Operators
| Occurrences
| Operands
| Occurrences
|
---|
int
|
4
| sort
|
1
|
()
|
5
| x
|
7
|
,
|
4
| n
|
3
|
[]
|
7
| i
|
8
|
if
|
2
| j
|
7
|
<
|
2
| save
|
3
|
;
|
11
| im1
|
3
|
for
|
2
|
2
|
2
|
=
|
6
|
1
|
3
|
-
|
1
|
0
|
1
|
<=
|
2
|
-
|
-
|
++
|
2
|
-
|
-
|
return
|
2
|
-
|
-
|
{}
|
3
|
-
|
-
|
n1=14
| N1=53
| n2=10
| N2=38
|
Here are the calculated Halstead metrics for the given C program:
Program Length (N) = 91 Vocabulary (n) = 24 Volume (V) = 417.23 bits Estimated Program Length (N^) = 86.51 Unique Operands Used as Both Input and Output (n2* = 3 (x: array holding integer to be sorted. This is used both as input and output) Potential Volume (V*) = 11.6 Program Level (L) = 0.027 Difficulty (D) = 37.03 Estimated Program Level (L^) = 0.038 Effort (T) = 610 seconds
Advantages of Halstead Metrics
- It is simple to calculate.
- It measures the overall quality of the programs.
- It predicts the rate of error.
- It predicts maintenance effort.
- It does not require a full analysis of the programming structure.
- It is useful in scheduling and reporting projects.
- It can be used for any programming language.
- Easy to use: The metrics are simple and easy to understand and can be calculated quickly using automated tools.
- Quantitative measure: The metrics provide a quantitative measure of the complexity and effort required to develop and maintain a software program, which can be useful for project planning and estimation.
- Language independent: The metrics can be used for different programming languages and development environments.
- Standardization: The metrics provide a standardized way to compare and evaluate different software programs.
Disadvantages of Halstead Metrics
- It depends on the complete code.
- It has no use as a predictive estimating model.
- Limited scope: The metrics focus only on the complexity and effort required to develop and maintain a software program, and do not take into account other important factors such as reliability, maintainability, and usability.
- Limited applicability: The metrics may not be applicable to all types of software programs, such as those with a high degree of interactivity or real-time requirements.
- Limited accuracy: The metrics are based on a number of assumptions and simplifications, which may limit their accuracy in certain situations.
Conclusion
Halstead's software metrics offer a quantitative approach to assessing a program's complexity. They include measures like program length (total operators and operands), vocabulary (unique operators and operands), and volume (a measure of the program's size in bits). These metrics help understand the diversity and size of the code, aiding in evaluating its complexity and potential maintainability. Overall, Halstead's metrics provide valuable insights into the code's structure and complexity.
Similar Reads
Software Engineering Tutorial Software Engineering is a subdomain of Engineering in which you learn to develop, design, test, and maintain software using a systematic and structured approach. Software is a collection of programs. And that programs are developed by software engineers In this Software Engineering Tutorial, you wil
7 min read
Software Engineering Basics
Introduction to Software EngineeringSoftware is a program or set of programs containing instructions that provide the desired functionality. Engineering is the process of designing and building something that serves a particular purpose and finds a cost-effective solution to problems. Table of ContentWhat is Software Engineering?Key P
11 min read
Software Development Life Cycle (SDLC)Software development life cycle (SDLC) is a structured process that is used to design, develop, and test good-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
11 min read
Software Quality - Software EngineeringTraditionally, a high-quality product is outlined in terms of its fitness of purpose. That is, a high-quality product will specifically be what the users need to try. For code products, the fitness of purpose is typically taken in terms of satisfaction of the wants arranged down within the SRS docum
5 min read
ISO/IEC 9126 in Software EngineeringThe International Organization for Standardization (ISO) has established a series of ISO and ISO/IEC standards for software quality. Starting with the ISO 9000-3 instructions for implementing the ISO 9001 standard, which is concerned with quality assurance processes, to the creation, supply, install
4 min read
Boehm's Software Quality ModelIn 1978, B.W. Boehm introduced his software quality model, which defines software quality through a hierarchical structure of attributes and metrics. This model is similar to the McCall Quality Model but encompasses a wider range of characteristics, including hardware performance-related ones. Boehm
4 min read
Software Crisis - Software EngineeringThe term "software crisis" refers to the numerous challenges and difficulties faced by the software industry during the 1960s and 1970s. It became clear that old methods of developing software couldn't keep up with the growing complexity and demands of new projects. This led to high costs, delays, a
3 min read
Software Measurement & Metrices
Software Measurement and MetricsSoftware Measurement: A measurement is a manifestation of the size, quantity, amount, or dimension of a particular attribute of a product or process. Software measurement is a titrate impute of a characteristic of a software product or the software process. Table of Content Software Measurement Prin
4 min read
People Metrics and Process Metrics in Software EngineeringPeople Metrics and Process Metrics, both play important roles in software development. People Metrics helps in quantifying the useful attributes whereas Process Metrics creates the body of the software. People metrics focus on how well team members work together and their overall satisfaction, while
8 min read
Halsteadâs Software Metrics - Software EngineeringHalstead's Software metrics are a set of measures proposed by Maurice Halstead to evaluate the complexity of a software program. These metrics are based on the number of distinct operators and operands in the program and are used to estimate the effort required to develop and maintain the program. T
11 min read
Cyclomatic ComplexityCyclomatic complexity, developed by Thomas McCabe, is a metric that measures the complexity of a program by counting its decision points. It measures the number of unique paths through the code, indicating how complex the logic is. Lower complexity suggests simpler, more manageable code, reducing th
6 min read
Functional Point (FP) Analysis - Software EngineeringFunctional Point Analysis (FPA) is a software measurement technique used to assess the size and complexity of a software system based on its functionality. It involves categorizing the functions of the software, such as input screens, output reports, inquiries, files, and interfaces, and assigning w
8 min read
Lines of Code (LOC) in Software EngineeringA 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 consists of all lines containing the declaration of any variable, and executable and non-executable state
4 min read
Software Development Models & Agile Methods
Waterfall Model - Software EngineeringThe Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because
13 min read
What is Spiral Model in Software Engineering?The Spiral Model is one of the most important SDLC model. The Spiral Model is a combination of the waterfall model and the iterative model. It provides support for Risk Handling. The Spiral Model was first proposed by Barry Boehm. This article focuses on discussing the Spiral Model in detail.Table o
9 min read
Prototyping Model - Software EngineeringPrototyping Model is a way of developing software where an early version, or prototype, of the product is created and shared with users for feedback. The Prototyping Model concept is described below: Table of ContentWhat is Prototyping Model?Phases of Prototyping ModelTypes of Prototyping ModelsAdva
7 min read
Incremental Process Model - Software EngineeringThe Incremental model is a software Development approach which is used to breakdown the project into smaller and easily manageable parts. In these, each part passes through Requirement, Design, Testing phases and Implementation phase. The overall process continue until we got the complete System.Inc
6 min read
Rapid Application Development Model (RAD) - Software EngineeringThe RAD model or Rapid Application Development model is a type of software development methodology that emphasizes quick and iterative release cycles, primarily focusing on delivering working software in shorter timelines. Unlike traditional models such as the Waterfall model, RAD is designed to be
9 min read
Coupling and Cohesion - Software EngineeringThe purpose of the Design phase in the Software Development Life Cycle is to produce a solution to a problem given in the SRS(Software Requirement Specification) document. The output of the design phase is a Software Design Document (SDD). Coupling and Cohesion are two key concepts in software engin
10 min read
Agile Software Development - Software EngineeringAgile Software Development is a Software Development Methodology that values flexibility, collaboration, and customer satisfaction. It is based on the Agile Manifesto, a set of principles for software development that prioritize individuals and interactions, working software, customer collaboration,
15+ min read
SRS & SPM
Software Requirement Specification (SRS) FormatIn order to form a good SRS, here you will see some points that can be used and should be considered to form a structure of good Software Requirements Specification (SRS). These are below mentioned in the table of contents and are well explained below. Table of ContentIntroductionGeneral description
5 min read
Software Engineering | Quality Characteristics of a good SRSRelated Article: Writing a good SRS for your project Quality characteristics of a good Software Requirements Specification (SRS) document include:Complete: The SRS should include all the requirements for the software system, including both functional and non-functional requirements.Consistent: The S
7 min read
Software Project Management (SPM) - Software EngineeringSoftware Project Management (SPM) is a proper way of planning and leading software projects. It is a part of project management in which software projects are planned, implemented, monitored, and controlled. In this article, we are discussing Software Project Management (SPM) topics that are useful
8 min read
COCOMO Model - Software EngineeringThe Constructive Cost Model (COCOMO) It was proposed by Barry Boehm in 1981 and is based on the study of 63 projects, which makes it one of the best-documented models. It is a Software Cost Estimation Model that helps predict the effort, cost, and schedule required for a software development project
15+ min read
Capability Maturity Model (CMM) - Software EngineeringThe Capability Maturity Model (CMM) is a tool used to improve and refine software development processes. It provides a structured way for organizations to assess their current practices and identify areas for improvement. CMM consists of five maturity levels: initial, repeatable, defined, managed, a
11 min read
Integrating Risk Management in SDLC | Set 1The Software Development Life Cycle (SDLC) is a conceptual model for defining the tasks performed at each step of the software development process. This model gives you a brief about the life cycle of Software in the development phase. In this particular article, we are going to discuss risk managem
8 min read
Software Maintenance - Software EngineeringSoftware Maintenance refers to the process of modifying and updating a software system after it has been delivered to the customer. This involves fixing bugs, adding new features, and adapting to new hardware or software environments. Effective maintenance is crucial for extending the software's lif
14 min read
Testing & Debugging
What is Software Testing?Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Types of Software TestingSoftware testing is a important of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performance, se
15+ min read
Testing Guidelines - Software EngineeringSoftware testing is an essential component of software development, ensuring that applications function correctly, meet user expectations, and are ready for deployment. Effective software testing involves a structured approach guided by well-defined principles and best practices. This article explor
3 min read
What is Debugging in Software Engineering?Debugging in Software Engineering is the process of identifying and resolving errors or bugs in a software system. It's a critical aspect of software development, ensuring quality, performance, and user satisfaction. Despite being time-consuming, effective debugging is essential for reliable and com
11 min read
Verification & Validation
Practice Questions