Context: Dependency Injection (DI) is a commonly applied mechanism to decouple classes from their dependencies in order to provide higher modularization. However, bad DI practices often lead to negative consequences, such as increasing coupling. Although white literature conjectures about the existence of DI anti-patterns, there is no evidence on their practical relevance, usefulness, and generality. Objective: The objective of this study is to propose and evaluate a catalog of Java DI anti-patterns. Methodology: We reviewed the reported DI anti-patterns in order to analyze their completeness and propose a catalog containing 12 Java DI anti-patterns. We developed a tool to statically analyze the occurrence level of the candidate DI anti-patterns in open-source and industry projects. Next, we survey practitioners to assess their perception on the relevance, usefulness, and their willingness on refactoring anti-pattern instances of the catalog. Results: Our static code analyzer tool showed a relative recall of 92.19% and high average precision. It revealed that at least 9 different DI anti-patterns appeared frequently in the analyzed projects. Besides, our survey confirmed the perceived relevance of the catalog and developers expressed their willingness to refactor instances of anti-patterns from source code. Conclusion: The catalog contains Java DI anti-patterns that occur in practice and that are perceived as useful. Sharing it with practitioners may help them to avoid such anti-patterns, thus improving source-code quality.
翻译:背景:依赖性注射(DI)是一个常用的机制,用来将类别与其依赖性脱钩,以提供更高的模块化。然而,不良的DI做法往往会导致负面后果,例如增加组合。虽然白色文献推测存在DI反胸针的存在,但没有证据表明它们的实际相关性、有用性和普遍性。目标:本研究的目的是提议和评估一份Java DI反胸针目录。结果:我们审查了报告的DI反胸针来源,以便分析其完整性,并提出了含有12 Java DI反胸针的目录。我们开发了一个工具,静态分析候选人DI反胸针在开放源和行业项目中的发生程度。我们调查人员评估其对相关性、有用性和是否愿意重新定义反胸针目录。结果:我们静态代码分析工具显示相对回收了92.19 % 和较高的平均精确度。它揭示了在最少9种不同的Dava 数据目录中,我们所认识的愿意性数据库显示的是它们的相关性。