“You cannot control what you cannot measure.”

Modern software tends to get more effective in problem-solving in all spheres of human activity, including the aim of increasing demand for the product. Together with this desire the product internal design is changing, as a rule towards more complex logic. Such changes lead to an increase in the number of lines in the program source code which in its turn produces more errors in the program.

Over the years, many metrics have been devised for the analysis of software source code. They range from the simplest, program length, or lines of code, to more complex measures like cyclomatic complexity and maintainability index.

Let’s get started

What is a software metric?

A software metric is a measure of software characteristics that are countable or measurable.

Why do we need software metrics?

The increasing complexity of modern software systems also raises the challenge of keeping the code reliable and maintainable. These metrics provide developers with a better understanding of the code they are developing. By using the code metrics, developers can understand which types or methods should be reworked or more thoroughly tested.

Introduction to metrics

The following are the metrics used to analyze the maintainability and complexity of the python code files:

  • Raw Metrics
  • Cyclomatic Complexity
  • Halstead Metrics
  • Maintainability Index

Raw Metrics

In Metric Check, the following raw metrics have been formulated for python code files:

generated raw metrics for test.py file
  1. LOC ( Lines of Code ): The total number of lines of code. It does not necessarily correspond to the number of lines in the file.
  2. LLOC ( Logical Lines of Code ): The number of logical lines of code. Every logical line of code contains exactly one statement.
  3. SLOC ( Source Lines of Code ): The number of source lines of code — not necessarily corresponding to the LLOC.
  4. Comment Lines: The number of comment lines. Multiline strings are not counted as comments since they are just strings to the python interpreter.
  5. Multi: The number of lines that represent multi-line strings.

6. Blanks: The number of blank lines or whitespace-only lines.

The following equation should always be true:

LOC = SLOC + Multi + Comments + Blanks

Cyclomatic Complexity

Cyclomatic Complexity is a software metric used to indicate the complexity of a program. It actually measures the structural complexity of the code. Using CC early in the development cycles reduces more risk of the program.

A program that has complex control flow is less maintainable and requires more tests to achieve good code coverage.

The following statements cause an effect on cyclomatic complexity:

Halstead Metrics

These metrics are statically computed from the source code:

The above variables are used to calculate the following:
1. Program Vocabulary: n = n1 + n2
2. Program Length: N = N1 + N2
3. Calculated Program Length: N^ = n1 log2 n1 + n2 log2 n2
4. Volume: V = N log2 n
5. Difficulty: D = (n1/2) * (N2/n2)
6. Effort: E = D*V
7. Time required to program: T = E/18 secs
8. Number of delivered bugs: B = V/3000

Maintainability Index ( MI )

MI is a software metric that measures how maintainable the source code is. The formula for MI requires SLOC, Cyclomatic complexity, and Halstead volume.

MI = max[ 0, (100* (171−5.2 log2 V−0.23CC−16.2 log2 L + 50sin(math.sqrt(2.4C)/ 171))]

Where:

  • V is the Halstead Volume (see below);
  • CC is the total Cyclomatic Complexity;
  • L is the number of Source Lines of Code (SLOC);
  • C is the percent of comment lines

Metric Check is an online web-based application that is built using the Django web framework.
Aim: to extract necessary information from the code file using various code metrics mentioned above to get a better insight into the source code.

It consists of two modules:
1. User:
The users are allowed to upload their code files on the website by logging in
( only on approval from the admin. )

The user has an extra facility to write a code on the website for various popular questions and check the metrics.
The user has an extra facility to write a code on the website for various popular questions and check the metrics.

The user has an extra facility to write a code on the website for various popular questions and check the metrics.

Trinket was used to implement Python IDE on Metric Check.

As shown above, the user can click on “Find” to get a detailed report of the code file.

2. Admin
The admin has to activate a user’s registration.

Requirements

  1. Windows 8 / Windows 9 / Windows 10
  2. SQL Lite or MySQL
  3. PyCharm

Developers can use Metric Check to generate code metrics data that measure the complexity and maintainability index of their uploaded code file.

Github Repo: https://github.com/shaikhsadaf/MetricCheck
LinkedIn: https://www.linkedin.com/in/sadafshaikh7/

Thank you for reading!
Please write back to sadaf.shaik1@gmail.com for any queries.