Creating functions is at the center of writing computer programs. But there has been little empirical research on how this is done and what are the considerations that developers use. We design an experiment in which we can compare the decisions made by multiple developers under exactly the same conditions. The experiment is based on taking existing production code, "flattening" it into a single monolithic function, and then charging developers with the task of refactoring it to achieve a better design by extracting functions. The results indicate that developers tend to extract functions based on structural cues, such as 'if' or 'try' blocks. And while there are significant correlations between the refactorings performed by different developers, there are also significant differences in the magnitude of refactoring done. For example, the number of functions that were extracted was between 3 and 10, and the amount of code extracted into functions ranged from 37% to 95%.
翻译:创建函数是写入计算机程序的核心。 但是对于如何做到这一点以及开发者使用的考虑是什么, 几乎没有经验性的研究。 我们设计了一个实验, 我们可以比较多个开发者在完全相同的条件下做出的决定。 实验的基础是使用现有的生产代码, 将其“ 缩放” 成一个单一的单体函数, 然后将重构它的任务委托开发者通过提取函数实现更好的设计。 结果显示开发者倾向于根据结构提示提取函数, 如“ if ” 或“ try” 区块。 虽然不同的开发者在进行重构时有着重要的关联, 但重构的大小也有很大差异。 例如, 提取的函数数量在3到 10 之间, 被提取到函数的代码数量在37%到95%之间 。