The Wayback Machine - https://web.archive.org/web/20240916200023/https://www.geeksforgeeks.org/software-engineering-halsteads-software-metrics/
Open In App

Halstead’s Software Metrics – Software Engineering

Last Updated : 12 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

  1. Comments are not considered.
  2. The identifier and function declarations are not considered
  3. All the variables and constants are considered operands.
  4. Global variables used in different modules of the same program are counted as multiple occurrences of the same variable.
  5. Local variables with the same name in different functions are counted as unique operands.
  6. Functions calls are considered operators.
  7. All looping statements e.g., do {…} while ( ), while ( ) {…}, for ( ) {…}, all control statements e.g., if ( ) {…}, if ( ) {…} else {…}, etc. are considered as operators.
  8. In control construct switch ( ) {case:…}, switch as well as all the case statements are considered as operators.
  9. The reserve words like return, default, continue, break, size, etc., are considered operators.
  10. All the brackets, commas, and terminators are considered operators.
  11. GOTO is counted as an operator and the label is counted as an operand.
  12. The unary and binary occurrences of “+” and “-” are dealt with separately. Similarly “*” (multiplication operator) is dealt with separately.
  13. In the array variables such as “array-name [index]” “array-name” and “index” are considered as operands and [ ] is considered as operator.
  14. 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.
  15. 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.

Frequently Asked Questions on Halstead’s Software Metrices

1. What is Halstead’s Effort?

It is simply a series of metrics that are used to determine the necessary efforts required for maintaining a program.

2. How we can calculate Halstead’s metrics?

Halstead Length: N = N1+N2

Halstead Calculated Length (Nx): Nx = N1 * log2(n1) + n2* log2(n2)

Halstead Volume(V): V = N*log2(n)

Halstead Effort(E): E=V*D



Previous Article
Next Article

Similar Reads

People Metrics and Process Metrics in Software Engineering
People 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 process metrics measure how smoothly tasks are co
8 min read
Identifying Software Development Metrics - Software Engineering
Companies invest a fair proportion of their resources in the expensive staff that drive the development of technology on which an organization runs. It is therefore imperative to explore and quantitatively measure the performance of such investments by empowering teams with the ability to track their efficiency. This task is delegated to a designat
4 min read
Object Oriented Metrics in Software Engineering
These are used to determine success or failure of a person also to quantify the improvements in the software throughout its process. These metrics can be used to reinforce good OO programming technique which lead to more reliable code. Object-oriented software engineering metrics are units of measurement that are used to characterize: object-orient
3 min read
Introduction Of People Metrics in Software Engineering
The concept of people metrics originated in the area of HR management. It provides a quantifiable measure of people's activity. These metrics are helpful to provide evidence of performance against things like objectives and goals. People's metrics have become important for Balanced Scorecards and other performance measurement systems. The reason is
8 min read
Most Important People Metrics in Software Engineering
Several important people metrics in software engineering can help measure the performance and effectiveness of a software development team. These metrics include: Productivity: This metric measures the amount of work that a team can complete within a given period. It can be measured by the number of lines of code written, the number of bugs fixed,
6 min read
Inspection Metrics in Software Engineering
Inspection metrics in software engineering measure the effectiveness of code reviews, design reviews, and other forms of inspections in identifying and resolving faults in the software. These metrics can help measure the efficiency and quality of the inspection process and provide insights into areas for improvement. Some common inspection metrics
6 min read
Product Metrics in Software Engineering
In software engineering, product metrics are quantitative measures used to assess the characteristics of software products. These metrics help in evaluating various aspects such as quality, performance, maintainability, and complexity of the software. By providing objective data, product metrics enable developers and managers to make informed decis
8 min read
Introduction to Software Engineering - Software Engineering
Software 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 Content What is Software Engineering?Key Principles of Software EngineeringMain Attributes
9 min read
Measuring Software Quality using Quality Metrics
In Software Engineering, Software Measurement is done based on some Software Metrics where these software metrics are referred to as the measure of various characteristics of a Software. In Software engineering Software Quality Assurance (SAQ) assures the quality of the software. A set of activities in SAQ is continuously applied throughout the sof
3 min read
Software Measurement and Metrics
Software 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 PrinciplesNeed for Software MeasurementClassification
4 min read
Reverse Engineering - Software Engineering
Software Reverse Engineering is a process of recovering the design, requirement specifications, and functions of a product from an analysis of its code. It builds a program database and generates information from this. This article focuses on discussing reverse engineering in detail. What is Reverse Engineering?Reverse engineering can extract desig
6 min read
Difference between Software Engineering process and Conventional Engineering Process
Software Engineering Process and Conventional Engineering Process, both are processes related to computers and development. In this article, we will see the similarities as well as differences between both, that is Software Engineering Process and the Conventional Engineering Process. Table of Content Software Engineering ProcessConventional Engine
4 min read
Re-engineering - Software Engineering
Software Re-engineering is a process of software development that is done to improve the maintainability of a software system. Re-engineering is the examination and alteration of a system to reconstitute it in a new form. This process encompasses a combination of sub-processes like reverse engineering, forward engineering, reconstructing, etc.  Tab
8 min read
Difference between Software Engineering and Computer Engineering
Software engineering and Computer engineering are two distinct disciplines that focus on different aspects of computer systems. While both fields require a strong foundation in computer science and mathematics, software engineering is focused on software development processes, while computer engineering is focused on the physical components and sys
6 min read
Evolution of Software Engineering: From an Art To Engineering Discipline
Software Engineering is a systematic and cost-effective technique for software development. It is an engineering approach to developing software. For example: If someone wants to travel from Punjab to Delhi. There are two approaches one can follow to achieve the same result: The normal approach is to go out and catch the bus/train that is available
13 min read
Requirements Engineering Process in Software Engineering
Requirements Engineering is the process of identifying, eliciting, analyzing, specifying, validating, and managing the needs and expectations of stakeholders for a software system. Table of Content What is Requirements Engineering?Requirements Engineering ProcessTools Involved in Requirement EngineeringAdvantages of Requirements Engineering Process
13 min read
Extended Function Point (EFP) Metrics
Function Point (FP) measure was inadequate for many engineering and embedded systems. To overcome this, A number of extensions to the basic function point measure have been proposed. These are as follows: Feature Points: Feature Points are computed by counting the information domain values. It can be used in those areas where there is a level of co
3 min read
Complexity Metrics
The productivity, if measured only in terms of lines of code per unit of time, can vary a lot depending on the complexity of the system to be developed. A programmer will produce a lesser amount of code for highly complex system programs, as compared to a simple application program. Similarly, complexity has a great impact on the cost of maintainin
7 min read
Short Note on Size-Oriented Metrics
Size-oriented metrics play a fundamental role in software development by measuring and comparing software project sizes based on various factors. This article explores the concept of size-oriented metrics, their advantages, and disadvantages, and provides an example of how they are applied in software organizations. What is Size Oriented Metrics?Si
3 min read
Core Metrics to Manage Modern Process
Project management metrics are very necessary and essential for implementing practical and sustainable project management practices and processes in an organization. These metrics enable us to improve our understanding by simply removing uncertainty so that we can form well-informed decisions. These metrics are key to improving how projects are man
5 min read
Metrics for the Design Model of the Product
Metrics measures quantitative assessment that focuses on countable values most commonly used for comparing and tracking the performance of a system. Metrics are used in different scenarios like analyzing models, designing models, source code, testing, and maintenance. This article focuses on discussing metrics for the design model of the product. T
12 min read
Short Note on Function-Oriented Metrics
Function-oriented metrics are used to measure software characteristics based on the functionality provided to the user. These metrics focus on the functional aspects of software systems rather than on the implementation details or the code itself. This article focuses on discussing function-oriented metrics in detail. Table of Content What are Func
8 min read
Difference between Forward Engineering and Reverse Engineering
Forward engineering and reverse engineering are two approaches to software development, with different goals and processes. Forward engineering involves creating new software systems from scratch using given requirements and design specifications. It focuses on building new applications through a structured process of analysis, design, implementati
6 min read
Software Engineering | Schick-Wolverton software reliability model
Prerequisite - Jelinski Moranda software reliability model The Schick-Wolverton (S-W) model is a modification to the J-M model. It is similar to the J-M model except that it further assumes that the failure rate at the ith time interval increases with time ti since the last debugging. In the model, the program failure rate function between the (i-1
4 min read
Software Engineering | Responsibilities of Software Project Manager
Software Project Management (SPM) is a sub-field of Project Management in which software projects are planned, implemented, monitored and controlled. It consists of three terms: Software, Project and Management. So, let us understand each term separately. Software includes a set of programs, documentation and user manual for a particular software p
3 min read
Software Engineering - Hardware Reliability vs Software Reliability
Reliability in software is software that has no failure and works in a special time period with a special environment. Hardware reliability is the probability of the absence of any hardware-related system malfunction for a given mission on the other hand software reliability is the probability that the software will provide a failure-free operation
3 min read
Failure Curve for Software in Software Engineering
Pre-requisites: Software Engineering In Software Engineering, the concept of a software failure curve is part of the hardware bathtub curve. According to the diagram, the failure rate is a function of time for Hardware. This curve shows that hardware exhibits a relatively high failure rate early in its life (these failures are often design or manuf
4 min read
Software Business and Development - Software Engineering
Software Business means selling software for the business or sometimes it is referred to as selling information about various software made for business purposes that are so-called business software. This article focuses on discussing Software Business and Development in detail. What is a Software Business? Software business is one of the commercia
3 min read
Software Quality Framework - Software Engineering
Software Quality Framework is a model for software quality that ensures quality by connecting and integrating the different views of software quality. This article focuses on discussing the Software Quality Framework. What is a Software Quality Framework?Software Quality Framework connects the customer view with the developer's view of software qua
4 min read
Software Evolution - Software Engineering
Software Evolution is a term that refers to the process of developing software initially, and then timely updating it for various reasons, i.e., to add new features or to remove obsolete functionalities, etc. This article focuses on discussing Software Evolution in detail. What is Software Evolution?The software evolution process includes fundament
3 min read
Article Tags :