一句话:提出DAN,利用程序依赖图抽取代码行序列以整体度量代码自然度,并验证依赖信息能提升区分与下游任务效果。
代码自然度刻画程序语言中的重复性与可预测性,但现有方法多逐行度量,忽略程序依赖等跨行深层语义关系,影响精度。本文首次实证研究将代码依赖引入自然度度量能否提升精度,并提出DAN:遍历程序依赖图,抽取由依赖连接的多个代码行序列,再以序列为单位度量自然度,从而捕获依赖信息。作者在三种新兴应用场景下与最先进方法比较,包括区分自然/非自然代码、识别有缺陷代码行以及为训练更好的代码模型进行数据清洗。结果表明,DAN不仅能更好区分自然与非自然代码,还能显著提升上述下游任务,说明代码依赖对代码自然度度量具有重要意义。
Code naturalness, which captures repetitiveness and predictability in programming languages, has proven valuable for various code-related tasks in software engineering. However, precisely measuring code naturalness remains a fundamental challenge. Existing methods measure code naturalness over individual lines of code while ignoring the deep semantic relations among different lines, e.g., program dependency, which may negatively affect the precision of the measure. Despite the intuitive appeal of extending the code naturalness measure to the code dependency domain (as there are some work that have initiated the utilization of code dependency for diverse code-related tasks), this assumption remains unexplored and warrants direct investigation. In this study, we aim to perform the first empirical study to investigate whether incorporating code dependency, instead of analyzing individual lines, can enhance the precision of measuring code naturalness. To achieve that, we first propose a new method named DAN for measuring code naturalness by incorporating the rich dependency information in the code. Specifically, DAN extracts multiple sequences of code lines by traversing the program dependency graph, where different code lines are connected by dependencies in each sequence, and then the code naturalness will be measured by taking each sequence as a whole. In this way, the dependency information can be well captured. Finally, we have conducted an extensive study to evaluate the influence of code dependency for measuring code naturalness with DAN, and compared it with the state-of-the-art methods under three emerging application scenarios of code naturalness. The results demonstrate that DAN can not only better distinguish natural and unnatural code, but also substantially boost two important downstream applications of code naturalness, i.e., distinguishing buggy and non-buggy code lines and data cleansing for training better code models, reflecting the significance of code dependency in measuring code naturalness.
DOI 原文 · arXiv · PDF(开放获取) · DBLP
@article{Yang0JH24,
title = {Dependency-Aware Code Naturalness},
author = {Chen Yang and Junjie Chen and Jiajun Jiang and Yuliang Huang},
journal = {Proceedings of the ACM on Programming Languages},
volume = {8},
number = {OOPSLA2},
year = {2024},
doi = {10.1145/3689794}
}