There are possible benefits and drawbacks to chaining methods together, as is often done in fluent APIs. A prior study investigated how Java developers chain methods in over 2.7k open-source projects. That study observed, for the dataset analyzed, that the use of method chaining in Java is popular and seems to be increasing over time. That study however was limited to a smaller sample of Java projects, and it is also not clear if the results generalize to other languages. In this work, we first replicate the prior results by building a similar dataset and our own analysis scripts. We then extend those results by analyzing a much larger dataset of 89k Java projects and generalizing to other programming languages by analyzing 26k Kotlin projects and 98k Python projects. The results show chaining is more popular in Java and Kotlin than Python, chaining use in Kotlin is not growing, and Python sees more use in non-testing code.
翻译:方法链式调用在流畅API中常被使用,但其可能带来利弊。先前一项研究调查了Java开发者在超过2700个开源项目中对方法链的使用情况。该研究基于所分析的数据集观察到,Java中方法链的使用较为普遍,且随时间推移呈现增长趋势。然而,该研究仅局限于较小规模的Java项目样本,且尚未明确其结论是否可推广至其他编程语言。本研究首先通过构建相似数据集及自研分析脚本复现了先前结论,随后进一步扩展研究范围:分析包含8.9万个Java项目的更大规模数据集,并通过分析2.6万个Kotlin项目及9.8万个Python项目,将研究推广至其他编程语言。结果表明,方法链在Java和Kotlin中的使用频率高于Python,Kotlin中方法链的使用并未呈现增长趋势,而Python中方法链更多出现在非测试代码中。