Abstract
Deep learning has significantly advanced the field of Code Intelligence, yet the optimal way to represent source code for machine learning models remains a subject of intense debate. Unlike natural language, source code possesses rigid syntax and complex logical dependencies. This study provides a comprehensive empirical evaluation of four major code representation strategies—Feature-based, Sequence-based, Tree-based, and Graph-based—across three critical software engineering tasks: code classification, vulnerability detection, and clone detection. Our findings demonstrate that graph-based representations, particularly those utilizing Code Property Graphs (CPG), consistently outperform other methods. Furthermore, we reveal that the textual content within code nodes provides a more potent signal for learning semantics than structural node types alone.
Introduction
As software systems grow in complexity, the need for automated tools to classify, secure, and refactor code has become paramount. Researchers have proposed various abstractions to "teach" code to AI, ranging from treating code as a flat sequence of tokens to constructing multi-layered graph structures. However, the field has lacked a systematic, large-scale comparison to determine which representations are most effective and why.
This paper addresses this gap by evaluating mainstream techniques within a unified framework. We investigate whether a single representation dominates across different tasks, the relative importance of textual versus structural information, and the specific contributions of semantic layers like data and control flow.
Methodology
We categorize existing code representation techniques into four distinct groups:
- Feature-based: Utilizes handcrafted expert metrics and statistical measures (e.g., TF-IDF) paired with traditional machine learning algorithms like Support Vector Machines (SVM) or XGBoost.
- Sequence-based: Treats source code as a linear string of tokens, processed by architectures designed for natural language, such as Long Short-Term Memory (LSTM) networks or Transformers.
- Tree-based: Leverages the hierarchical structure of the Abstract Syntax Tree (AST), employing models like Tree-LSTM and Code2Vec to capture nesting and syntax.
- Graph-based: Employs Code Property Graphs (CPG), which integrate ASTs with Control Flow Graphs (CFG), Control Dependency Graphs (CDG), and Data Dependency Graphs (DDG). These are processed using Graph Neural Networks (GNNs), specifically Gated Graph Neural Networks (GGNN), Graph Convolutional Networks (GCN), and Graph Attention Networks (GAT).
To understand the drivers of performance, we also conducted an ablation study on node embeddings, comparing the efficacy of node types (e.g., IfStatement), node text (e.g., the actual code snippet), and hybrid combinations.
Experimental Setup
The study utilized three large-scale public benchmarks focused on the C programming language:
- Online Judge (OJ): 52,000 programs for Code Classification, aimed at identifying the functional purpose of a program.
- Devign: 66,067 labeled functions for Vulnerability Detection, identifying potential security flaws.
- OJClone: 50,000 program pairs for Clone Detection, determining if two distinct snippets are functionally equivalent.
Key Findings
The Superiority of Graph Representations
Graph-based models, particularly the GGNN, emerged as the top performers across all evaluated tasks. By explicitly modeling how data and control move through a program, these models capture deep semantic relationships that sequence-based and tree-based models often overlook. For instance, in code classification, the GGNN achieved an accuracy of 92.04%, significantly outperforming the best sequence model (BiLSTM at 83.82%).
Textual Content vs. Structural Type
A pivotal discovery of this study is that textual information within a node is a more critical feature than the node type. In vulnerability detection, using only textual embeddings in a GGNN yielded an F1-score of 73.62%, whereas using only node types resulted in a drop to 66.02%. This suggests that while structure is important, the specific identifiers and logic contained in the text provide the most granular and useful signals for the model.
Task-Specific Semantic Layers
Different tasks benefit from different semantic abstractions. For vulnerability detection, the Data Dependency Graph (DDG) proved to be the most influential component. This aligns with the nature of security bugs, such as buffer overflows, which are fundamentally rooted in how data flows through memory and variables.
Robustness to Semantic-Preserving Changes
Sequence-based models were found to be fragile when faced with "noise," such as variable renaming or statement reordering. In contrast, graph and tree models demonstrated high robustness. Because these representations focus on the underlying logical structure, they remain invariant to changes that do not alter the program's actual behavior.
Discussion and Limitations
While graph-based models offer superior performance, they involve a "construction tax." Generating Code Property Graphs is computationally intensive and requires specialized compiler-front-end tools like Joern. Furthermore, the current study was limited to the C language; the generalizability of these findings to dynamic languages like Python or Java remains an area for future exploration.
Additionally, even the most advanced graph models struggle with complex logical constructs such as deep nested loops and intricate pointer operations. Statistical analysis showed that error rates increase as the nesting depth of a program grows, indicating that current GNN architectures still have limitations in capturing extremely long-range dependencies.
Conclusion
This empirical study establishes graph-based representations as the current gold standard for learning program semantics. The integration of textual information with structural graph layers provides a robust and high-performing foundation for code intelligence. Future research should focus on optimizing the computational efficiency of graph construction and developing more sophisticated fusion techniques for combining structural and textual data.
Summary updated