人们更喜欢“自然”代码吗?
Do People Prefer "Natural" code?
摘要:
众所周知,自然代码是非常重复的(比自然语言语料库更重复);此外,即使在考虑了代码的简单语法之后,这种重复性仍然存在。然而,程序设计语言是非常有表现力的,允许很多不同的方式(都是清晰和明确的)来表达甚至非常简单的计算。为什么自然代码是重复的?我们假设这是因为代码是双峰的:它由机器执行,但也由人类阅读。我们认为,这种双峰性导致开发人员以代码读者熟悉的某些首选方式编写代码。为了测试这一理论,我们1)使用在大型训练语料库中估计的语言模型对熟悉度进行建模,2)在不同的测试语料库中对Java和Python表达式进行几种意义保持转换,以查看读者更熟悉的形式(如语言模型所预测的)是否实际上是实际编写的形式。我们发现,这些转换通常产生的程序结构,在实践中不太常见,支持的理论,即在代码中的高重复性是一个故意的偏好。最后,3)我们使用人类受试者研究来显示语言模型得分和人类偏好之间的对齐,第一次在代码中,提供支持使用此措施来改进代码。
英文摘要:
Natural code is known to be very repetitive (much more so than natural language corpora); furthermore, this repetitiveness persists, even after accounting for the simpler syntax of code. However, programming languages are very expressive, allowing a great many different ways (all clear and unambiguous) to express even very simple computations. So why is natural code repetitive? We hypothesize that the reasons for this lie in fact that code is bimodal: it is executed by machines, but also read by humans. This bimodality, we argue, leads developers to write code in certain preferred ways that would be familiar to code readers. To test this theory, we 1) model familiarity using a language model estimated over a large training corpus and 2) run an experiment applying several meaning preserving transformations to Java and Python expressions in a distinct test corpus to see if forms more familiar to readers (as predicted by the language models) are in fact the ones actually written. We find that these transformations generally produce program structures that are less common in practice, supporting the theory that the high repetitiveness in code is a matter of deliberate preference. Finally, 3) we use a human subject study to show alignment between language model score and human preference for the first time in code, providing support for using this measure to improve code.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。