一句话:提出首个基于500名开发者评分的标识符语义表示基准,发现现有嵌入能表示语义相关性但难以表示语义相似性。
标识符名称蕴含代码意图的语义信息,基于名称的程序分析(如缺陷检测、类型预测、可读性改进)依赖标识符的语义表示(如学习嵌入)。但这些表示在多大程度上符合开发者感知的语义相关性与相似性尚不清楚。本文提出 IdBench:首个以500名开发者对数千标识符对的评分为真值的基准,并评估自然语言嵌入、代码专用嵌入及词汇字符串距离函数。结果显示,各技术效果差异显著:最佳嵌入能表示语义相关性,却无法令人满意地表示语义相似性,甚至将含义相反的标识符判为相似,可能导致重构工具等严重错误。不同技术具有互补性,作者据此提出集成模型,明显优于最佳单一表示。
Identifier names convey useful information about the intended semantics of code. Name-based program analyses use this information, e.g., to detect bugs, to predict types, and to improve the readability of code. At the core of name-based analyses are semantic representations of identifiers, e.g., in the form of learned embeddings. The high-level goal of such a representation is to encode whether two identifiers, e.g., len and size, are semantically similar. Unfortunately, it is currently unclear to what extent semantic representations match the semantic relatedness and similarity perceived by developers. This paper presents IdBench, the first benchmark for evaluating semantic representations against a ground truth created from thousands of ratings by 500 software developers. We use IdBench to study state-of-the-art embedding techniques proposed for natural language, an embedding technique specifically designed for source code, and lexical string distance functions. Our results show that the effectiveness of semantic representations varies significantly and that the best available embeddings successfully represent semantic relatedness. On the downside, no existing technique provides a satisfactory representation of semantic similarities, among other reasons because identifiers with opposing meanings are incorrectly considered to be similar, which may lead to fatal mistakes, e.g., in a refactoring tool. Studying the strengths and weaknesses of the different techniques shows that they complement each other. As a first step toward exploiting this complementarity, we present an ensemble model that combines existing techniques and that clearly outperforms the best available semantic representation.