A Validation of Cognitive Complexity As a Measure of Source Code Understandability
Total Page:16
File Type:pdf, Size:1020Kb
Institute of Software Technology University of Stuttgart Universitätsstraße 38 D–70569 Stuttgart Bachelorarbeit A Validation of Cognitive Complexity as a Measure of Source Code Understandability Marvin Muñoz Barón Course of Study: Softwaretechnik Examiner: Prof. Dr. Stefan Wagner Supervisor: Marvin Wyrich, M.Sc. Commenced: April 30, 2019 Completed: October 30, 2019 Abstract Understanding source code is an integral part of the software development process. There have been numerous attempts to describe source code metrics that correlate with understandability, but few are empirically evaluated and those that are, show no meaningful support for their purpose. A recent effort is cognitive complexity, a measure specifically described as a metric for understand- ability. The primary goal of this study was to find evidence towards determining the validity of cognitive complexity as a measure of source code understandability. To achieve this, we planned and executed a systematic literature search to find source code snippets that were evaluated in terms of understandability in previous studies. The cognitive complexity of these snippets was then correlated with the measures of understandability used in the studies. The literature search identified data from 14 studies spanning over 324 code snippets and approximately 24,400 individual human evaluations, which were used in the correlation analysis. The results show that for most of the measures from existing studies, cognitive complexity significantly correlates with source code understandability. The mean of the significant correlations was 0.654 for comprehension time and 0.411 for subjective ratings of understandability, readability, and confidence by the study participants. The correlation with the correctness of the comprehension tasks showed mixed results, with some significant positive and some significant negative correlations. Overall, the evidence gathered in this study shows significant support for the validity of cognitive complexity. As faras we know, cognitive complexity is the first solely code-based metric that correlates with source code understandability in a meaningful way. 3 Contents 1 Introduction 15 1.1 Source Code Understandability .......................... 15 1.1.1 Definitions ................................. 16 1.1.2 Measuring Understandability ........................ 17 1.1.3 Related Work ................................ 18 1.2 Cognitive Complexity ............................... 19 2 Methods 23 2.1 Systematic Literature Search ........................... 23 2.1.1 Search Strategy ............................... 24 2.1.2 Search Methods ............................... 27 2.1.3 Search Results ............................... 32 2.2 Data Analysis ................................... 39 2.2.1 Preparation ................................. 39 2.2.2 Correlation ................................. 41 3 Results 43 3.1 Time ........................................ 45 3.2 Correctness .................................... 46 3.3 Ratings ....................................... 47 3.4 Physiological Factors ............................... 48 3.5 Other Variables .................................. 49 4 Discussion 51 4.1 Systematic Literature Search ........................... 51 4.2 Findings ...................................... 51 4.3 Threats to Validity ................................. 53 5 Conclusion 55 Bibliography 57 5 List of Figures 1.1 Understandability and related attributes as described by Boehm et al. [BBL76] .. 16 2.1 Summarized overview of the literature search process ............... 25 2.2 Google Scholar search process ........................... 28 2.3 ACM Digital Library search process ........................ 29 2.4 IEEE Xplore search process ............................ 30 2.5 Elsevier ScienceDirect search process ....................... 30 2.6 Flowchart with the general search process steps and the corresponding data artifacts 31 2.7 Sankey diagram showing the results of the literature search ............ 33 2.8 Sankey diagram showing the results of snowballing ................ 33 2.9 Pie chart showing the type of comprehension task used in the studies ....... 34 2.10 Pie chart with the programming languages used in the studies ........... 35 2.11 Histogram showing the number of published understandability studies by year .. 35 2.12 Area chart showing the types of measures used in studies over time ........ 36 2.13 Histogram showing the programming languages used in studies over time .... 36 2.14 Point chart showing the aggregation of understandability values .......... 41 3.1 Interpreted Pearson correlation for time ...................... 45 3.2 Interpreted Spearman correlation for time ..................... 45 3.3 Interpreted Pearson correlation for correctness ................... 46 3.4 Interpreted Spearman correlation for correctness .................. 46 3.5 Interpreted Pearson correlation for ratings ..................... 47 3.6 Interpreted Spearman correlation for ratings .................... 47 3.7 Interpreted Pearson correlation for physiological factors .............. 48 3.8 Interpreted Spearman correlation for physiological factors ............. 48 3.9 Interpreted Pearson correlation for other variables ................. 49 3.10 Interpreted Spearman correlation for other variables ................ 49 7 List of Tables 2.1 Data sources included in the literature search and their URLs ........... 25 2.2 Inclusion and exclusion criteria ........................... 26 2.3 Data points for extraction from literature search results .............. 27 2.4 Relevant studies as identified by the literature search ................ 37 2.5 Data sets extracted from relevant studies ...................... 38 3.1 Descriptive measures of cognitive complexity for each of the data sets ...... 43 3.2 Raw correlation results for comprehension time. (*p<.05) ............. 45 3.3 Raw correlation results for correctness. (*p<.05) ................. 46 3.4 Raw correlation results for subjective ratings. (*p<.05) .............. 47 3.5 Raw correlation results for physiological measures. (*p<.05) ........... 48 3.6 Raw correlation results for other variables. (*p<.05) ................ 49 9 List of Code Snippets 1.1 Java greeting example with cyclomatic complexity calculation .......... 19 1.2 Sum of primes example ............................... 21 1.3 Get words example ................................. 21 2.1 Unaltered code snippet ............................... 40 2.2 Modified code snippet ................................ 40 2.3 SAV data conversion using R ............................ 41 11 Acronyms ACM Association for Computer Machinery. 26 DID data set identifier. 37 EDA electrodermal. 18 EEG electroencephalogram. 18 EiPE explain in plain English. 18 fMRI functional magnetic resonance imaging. 17 IEEE Institute of Electrical and Electronics Engineers. 26 LOC lines of code. 19 MCC McCabe’s Cyclomatic Complexity. 19 PL programming language. 37 PNo number of participants. 37 SNo number of code snippets. 37 13 1 Introduction Ever since the first computer programs were written, understanding what the purpose of asnippet of source code is has been an integral part of software development. Thirty years ago, program understanding was called the challenge for the 1990s [Cor89]. But even nowadays professional devel- opers spend more than 55% of their time on activities related to program comprehension [MML15; XBL+18]. Through static code analysis and code-oriented software measures, developers always seek to optimize ways to read and work with source code. Numerous metrics exist to assess software quality but none seem to significantly correlate with code understandability [FALK11; SBV+19]. A recent effort to find this missing link between software measures and understandability isthe newly introduced cognitive complexity measure, described by Campbell et al. as a metric for understandability [Cam18]. The goal of this work is to evaluate whether cognitive complexity correlates with measures for understandability from existing studies. Through a systematic literature search, code snippets of which the understandability was previously measured, were identified. Cognitive complexity was then calculated for these snippets and their values were correlated with the measured variables for understandability. To summarize, the main contributions of this work are: • The planning, execution, and results of a systematic literature search for studies that measure program comprehension from a human developer’s point of view. • A comprehensive analysis of cognitive complexity as a measure of source code understand- ability. To this end, 14 studies spanning over 324 code snippets and approximately 24,400 individual human evaluations were used to investigate the correlation of cognitive complexity with measures for understandability. Structure: This report is organized as follows. Chapter 1 introduces the core concepts of source code understandability, gives an overview of related works in the field, and describes the core metric of this work, cognitive complexity. In Chapter 2, the methodology of the literature search and correlation analysis is presented. Chapter 3 shows the results of the correlation analysis. A detailed discussion of these results and an explanation of any threats to the validity to this work follows in Chapter 4. Finally, Chapter 5 summarizes the contents of the thesis, provides a conclusion and directions for future research. 1.1 Source Code Understandability There have