一句话:重新检验代码自然性,发现分隔符贡献显著,API 用法高度重复,图表示比 n-gram 更能捕捉代码模式。
既有研究认为源代码比英语文本更重复、更可预测,即更“自然”。本文重新检验该结论,发现表观自然性很大程度来自分号、括号等语法分隔符;在 Java 语料中分隔符占 44% 的 token。按 NLP 惯例去除标点和关键词后,代码仍重复可预测,但程度低于此前认识,故建议代码推荐器过滤 SyntaxTokens 以降低噪声。作者还发现跨项目 API 用法高度重复,将 n-gram 限制在 Java API 后更明显;由于代码常由含控制/数据流的 AST 表示,n-gram 表征不佳,与统计图表示相比,图更重复且含更高层模式。作者建议未来关注能准确捕捉复杂编码模式的统计代码图模型,并公开复现包。
Recent works have concluded that software code is more repetitive and predictable, i.e. more natural, than English texts. On re-examination, we find that much of the apparent "naturalness" of source code is due to the presence of language specific syntax, especially separators, such as semi-colons and brackets. For example, separators account for 44% of all tokens in our Java corpus. When we follow the NLP practices of eliminating punctuation (e.g., separators) and stopwords (e.g., keywords), we find that code is still repetitive and predictable, but to a lesser degree than previously thought. We suggest that SyntaxTokens be filtered to reduce noise in code recommenders. Unlike the code written for a particular project, API code usage is similar across projects: a file is opened and closed in the same manner regardless of domain. When we restrict our n-grams to those contained in the Java API, we find that API usages are highly repetitive. Since API calls are common across programs, researchers have made reliable statistical models to recommend sophisticated API call sequences. Sequential n-gram models were developed for natural languages. Code is usually represented by an AST which contains control and data flow, making n-grams models a poor representation of code. Comparing n-grams to statistical graph representations of the same codebase, we find that graphs are more repetitive and contain higherlevel patterns than n-grams. We suggest that future work focus on statistical code graphs models that accurately capture complex coding patterns. Our replication package makes our scripts and data available to future researchers[1].