Background. Code understandability is fundamental. Developers need to clearly understand the code they are modifying. A low understandability can increase the amount of coding effort and misinterpretation of code has impact on the entire development process. Ideally, developers should write clear and understandable code with the least possible effort. Objective. The goal of this work is to investigate if the McCabe Cyclomatic Complexity or the Cognitive Complexity can be a good predictor for the developers' perceived code understandability to understand which of the two complexities can be used as criteria to evaluate if a piece of code is understandable. Method. We designed and conducted an empirical study among 216 junior developers with professional experience ranging from one to four years. We asked them to manually inspect and rate the understandability of 12 Java classes that exhibit different levels of Cyclomatic and Cognitive Complexity. Results. Cognitive Complexity slightly outperforms the Cyclomatic Complexity to predict the developers' perceived understandability. Conclusion. The identification of a clear and validated measure for Code Complexity is still an open issue. Neither the old fashioned McCabe Cyclomatic Complexity and the most recent Cognitive Complexity are good predictors for code understandability, at least when considering the complexity perceived by junior developers.
翻译:背景。代码可理解性是基础。开发者需要清晰理解他们正在修改的代码。低可理解性会增加编码工作量,而代码误解会影响整个开发过程。理想情况下,开发者应以最小努力编写清晰易懂的代码。目标。本研究旨在探究McCabe圈复杂度或认知复杂度是否能有效预测开发者感知的代码可理解性,以明确两种复杂度指标中哪一种可作为评估代码是否易于理解的标准。方法。我们设计并实施了一项针对216名具有1至4年专业经验的初级开发人员的实证研究。要求他们手动检查并评估12个具有不同圈复杂度和认知复杂度的Java类的可理解性。结果。认知复杂度在预测开发者感知的可理解性方面略优于圈复杂度。结论。明确且经验证的代码复杂度度量指标的识别仍是一个未解决的问题。无论是传统的McCabe圈复杂度还是较新的认知复杂度,均不能有效预测代码可理解性——至少在考虑初级开发者感知的复杂度时如此。