Computer Engineering and Applications ›› 2024, Vol. 60 ›› Issue (3): 138-147.DOI: 10.3778/j.issn.1002-8331.2305-0218

• Theory, Research and Development • Previous Articles     Next Articles

Bagging Heterogeneous Ensemble Code Smell Detection and Refactoring Priority Division

WU Haitao, CAI Yongqi, GAO Jianhua   

  1. College of Information, Mechanical and Electrical Engineering, Shanghai Normal University, Shanghai 200234, China
  • Online:2024-02-01 Published:2024-02-01



  1. 上海师范大学 信息与机电工程学院,上海 200234

Abstract: Code smells are symptoms of poor design and implementation choices that may hinder code comprehension and possibly increase change-and fault-proneness. Previous research has focused on the detection of code smells by a single model and failed to provide developers with refactoring recommendations. Aiming at the above problems, a code smell detection and refactoring priority division method based on Bagging heterogeneous ensemble model is proposed. The method exploits the heterogeneity between classifiers to detect three code smells such as Complex Class, Long Method, and Spaghetti Code through the F1 integration strategy, and converts the smell probability by the model into a possibility distribution to provide developers with refactoring recommendations. The experimental results verify and evaluate:(1) the stability and relationship among base classifiers in code smells; (2) the performance of Bagging heterogeneous ensemble model to detect the code smells mentioned above; (3) the validity of the refactoring priority when transforming smell probability into possibility distribution, on 32 versions of 6 open-source systems. The experimental results show that the best base classifier varies with code smell types. At the same time, compared with the base classifiers, Bagging heterogeneous ensemble model has a 0.06~40.51 percentage points increase in F1 and a 0.45~28.37 percentage points increase in AUC. Finally, Cohen’s Kappa test is conducted between refactoring priorities of Bagging heterogeneous ensemble model and six respondents, which are highly consistent.

Key words: code smell, machine learning, ensemble learning, software refactoring, possibility distribution

摘要: 代码异味是不良的设计和代码实现的症状,可能阻碍代码理解、增加代码更改和出错的可能性。以前的研究专注于单一模型在代码异味上的检测,并且无法为开发人员提供重构建议。针对上述问题,提出一种基于Bagging异构集成模型的代码异味检测与重构优先级划分方法,该方法利用分类器间的异质性,通过F1集成策略来检测Complex Class、Long Method、Spaghetti Code等三种代码异味,并将模型输出的异味概率转化为可能性分布后,为开发人员提供重构意见。实验在6个开源系统的32个版本上验证、评估:(1)基分类器的稳定性以及与代码异味的关系;(2)Bagging异构集成模型检测上述代码异味的性能;(3)将异味概率转化为可能性分布并作为重构优先级的有效性。实验结果表明,最佳基分类器因代码异味类型而异。同时,Bagging异构集成模型相较于基分类器,F1提高0.06~40.51个百分点,AUC提高0.45~28.37个百分点。最后将Bagging异构集成模型的重构优先级与6名受访者的重构优先级进行Kappa一致性检验,两者具有高度一致性。

关键词: 代码异味, 机器学习, 集成学习, 软件重构, 可能性分布