Skip to main page content
U.S. flag

An official website of the United States government

Dot gov

The .gov means it’s official.
Federal government websites often end in .gov or .mil. Before sharing sensitive information, make sure you’re on a federal government site.

Https

The site is secure.
The https:// ensures that you are connecting to the official website and that any information you provide is encrypted and transmitted securely.

Access keys NCBI Homepage MyNCBI Homepage Main Content Main Navigation
. 2023 Feb 7:16:1065366.
doi: 10.3389/fnins.2022.1065366. eCollection 2022.

On the accuracy of code complexity metrics: A neuroscience-based guideline for improvement

Affiliations

On the accuracy of code complexity metrics: A neuroscience-based guideline for improvement

Gao Hao et al. Front Neurosci. .

Abstract

Complexity is the key element of software quality. This article investigates the problem of measuring code complexity and discusses the results of a controlled experiment to compare different views and methods to measure code complexity. Participants (27 programmers) were asked to read and (try to) understand a set of programs, while the complexity of such programs is assessed through different methods and perspectives: (a) classic code complexity metrics such as McCabe and Halstead metrics, (b) cognitive complexity metrics based on scored code constructs, (c) cognitive complexity metrics from state-of-the-art tools such as SonarQube, (d) human-centered metrics relying on the direct assessment of programmers' behavioral features (e.g., reading time, and revisits) using eye tracking, and (e) cognitive load/mental effort assessed using electroencephalography (EEG). The human-centered perspective was complemented by the subjective evaluation of participants on the mental effort required to understand the programs using the NASA Task Load Index (TLX). Additionally, the evaluation of the code complexity is measured at both the program level and, whenever possible, at the very low level of code constructs/code regions, to identify the actual code elements and the code context that may trigger a complexity surge in the programmers' perception of code comprehension difficulty. The programmers' cognitive load measured using EEG was used as a reference to evaluate how the different metrics can express the (human) difficulty in comprehending the code. Extensive experimental results show that popular metrics such as V(g) and the complexity metric from SonarSource tools deviate considerably from the programmers' perception of code complexity and often do not show the expected monotonic behavior. The article summarizes the findings in a set of guidelines to improve existing code complexity metrics, particularly state-of-the-art metrics such as cognitive complexity from SonarSource tools.

Keywords: EEG; code complexity metrics; code comprehension; code constructs; code refactoring; cognitive load; mental effort.

PubMed Disclaimer

Conflict of interest statement

The authors declare that the research was conducted in the absence of any commercial or financial relationships that could be construed as a potential conflict of interest.

Figures

FIGURE 1
FIGURE 1
Sequence of steps for each trial of program comprehension.
FIGURE 2
FIGURE 2
An illustrative example of the two EEG features (fEEG1 and fEEG2) values over the time for a given code comprehension task, with two specific regions of interest A and B.
FIGURE 3
FIGURE 3
NASA-TLX average results considering all participants.
FIGURE 4
FIGURE 4
Eye tracker data for Participant 2 in the comprehension task for program C2.

References

    1. Ajami S., Woodbridge Y., Feitelson D. (2017). “Syntax, predicates, idioms what really affects code complexity?,” in Proceedings of the IEEE international conference on program comprehension (ICPC), Buenos Aires. 10.1109/ICPC.2017.39 - DOI
    1. Ammann P., Offutt J. (2016). Introduction to software testing, 2nd Edn. Cambridge: Cambridge University Press. 10.1017/9781316771273 - DOI
    1. Ammar H., Nikzadeh T., Dugan J. (2001). Risk assessment of software-system specifications. IEEE Trans. Reliab. 50 171–183. 10.1109/24.963125 - DOI
    1. Campbell A. (2021). Webinar: Refactoring with cognitive complexity. Available online at: https://community.sonarsource.com/t/webinar-refactoring-with-cognitive-c... (accessed September 29, 2022).
    1. Campbell G. (2017). Cognitive complexity–a new way of measuring understandability, Technical Report. Geneva: SonarSource.

LinkOut - more resources