Deep Code Search with Naming-Agnostic Contrastive Multi-View Learning

Jiadong Feng, Wei Li, Suhuang Wu, Zhao Wei, Yong Xu, Juhong Wang, Hui LiACM TKDD 2024

Abstract

Traditional deep code search models often rely heavily on explicit variable names to infer semantics, making them fragile when faced with inconsistent naming conventions or obfuscated code. We present NACS (Naming-Agnostic Code Search), a framework designed to prioritize the structural "skeleton" of code over identifier labels. By employing contrastive multi-view learning on Abstract Syntax Trees (ASTs), NACS captures code intent through its topology and logic flow. Our evaluation shows that NACS achieves state-of-the-art performance on standard benchmarks and maintains high accuracy even when variable names are completely randomized—a scenario where traditional models suffer significant performance degradation.

1. Introduction

Deep learning has significantly advanced code search, enabling developers to retrieve relevant code snippets using natural language queries. However, a persistent challenge is the "naming issue": models often over-fit to specific variable names (e.g., user_id vs. uid). In real-world development, naming conventions vary widely. If a model associates a logic pattern solely with the name counter, it may fail to recognize the same logic when variables are named i or idx.

Figure 1: Illustration of variable name changes and the corresponding invariant Abstract Syntax Tree (AST) structure.

As shown in the figure above, while variable names can change, the underlying AST structure remains constant. NACS aims to exploit this structural invariance to create a truly naming-agnostic search method.

2. Methodology

The NACS framework utilizes a multi-view contrastive learning approach that explicitly ignores variable-bound information during structural modeling.

2.1 Naming-Agnostic Representation

NACS strips variable names from the AST, focusing exclusively on node types and tree topology. This ensures the model learns the functional "shape" of the code rather than its labels.

2.2 Multi-View Learning

To capture complex semantics, NACS models the AST from two distinct perspectives:

  • Graph View: Employs a Graph Isomorphism Network (GIN) to capture the global topological structure of the AST.
  • Path View: Uses a Bi-LSTM to model paths from the root to leaf nodes, capturing granular sequential logic flow.

2.3 Contrastive Augmentation

The model is trained using two types of data augmentation to identify intrinsic properties:

  • Program Transformation (PT): Semantic-preserving changes such as inserting dead code or converting for loops to while loops.
  • Structure Transformation (ST): Graph-level changes like subtree dropping to force the model to recognize core structures despite noise.

2.4 Hybrid Scoring

At inference, NACS combines its structural score with a token-based score from CodeBERT. This approach balances "what the code says" (tokens) with "what the code does" (structure).

3. Experimental Evaluation

NACS was evaluated on standard benchmarks including CodeSearchNet and CoSQA. To specifically test robustness against naming variations, the authors introduced CoSQA-Var, a "stress test" dataset where variable names in the original CoSQA code were randomly replaced with other names from the dataset.

4. Results and Discussion

NACS consistently outperformed state-of-the-art (SOTA) baselines in terms of Mean Reciprocal Rank (MRR).

  • Superior Performance: On the CoSQA dataset, NACS achieved an MRR of 0.708, surpassing GraphCodeBERT (0.648) and CodeBERT (0.652).
  • Robustness to Naming: On the randomized CoSQA-Var dataset, NACS maintained an MRR of 0.704, showing negligible drop. In contrast, GraphCodeBERT’s performance plummeted by over 42%.
  • Model Boosting: Integrating NACS components into existing models like DeepCS or GraphCodeBERT significantly reduced their sensitivity to naming changes, demonstrating the framework's versatility.

5. Conclusion

NACS demonstrates that the structural "shape" of an AST provides sufficient signal for identifying code functionality. By decoupling logic from labels through multi-view contrastive learning, NACS provides a robust solution to the naming issue in code search. Future work will focus on reducing computational overhead and expanding the framework to support a wider variety of programming languages.

Summary updated

BibTeX
@article{feng2024deep,
  author = {Jiadong Feng and Wei Li and Suhuang Wu and Zhao Wei and Yong Xu and Juhong Wang and Hui Li},
  title = {{Deep Code Search with Naming-Agnostic Contrastive Multi-View Learning}},
  journal = {ACM Trans. Knowl. Discov. Data},
  year = {2024},
}
Figure