Method Name Prediction

Keywords: method name prediction, code ai

Method Name Prediction is the code AI task of automatically generating or predicting the name of a method or function given its body — learning the conventions by which developers translate code intent into identifiers, enabling automated code naming assistance, detecting inconsistently named methods (whose name mismatches their implementation), and providing a well-defined benchmark for code understanding models.

What Is Method Name Prediction?

- Task Definition: Given a method body (with its original name masked or removed), predict the method's name.
- Input: Function body — parameter names, local variable names, return statements, called methods, control flow.
- Output: A predicted method name, typically a sequence of sub-word tokens forming a camelCase or snake_case identifier. "calculate_total_price" or "calculateTotalPrice."
- Key Benchmarks: code2vec (Alon et al. 2019, Java), code2seq (500k Java/Python/C# methods), JAVA-small/medium/large (350K/700K/4M methods from GitHub Java projects).
- Evaluation Metrics: F1 score over sub-tokens (treating "calculateAverageScore" as ["calculate", "Average", "Score"] and comparing to reference sub-tokens), Precision@1, ROUGE-2.

Why Method Names Contain Semantic Information

Good developers encode rich semantic information in method names:
- calculateMonthlyInterest() → multiplication, division, time-period calculation.
- validateUserCredentials() → comparison, lookup, boolean return.
- parseCSVToDataFrame() → file I/O, string splitting, data transformation.
- sendEmailNotification() → network call, template formatting, side effect.

Method name prediction forces a model to compress this semantic understanding into a concise identifier — making it a rigorous code comprehension evaluation.

The code2vec Model (Alon et al. 2019)

The landmark method name prediction paper introduced:
- AST Path Representation: Decompose code into (leaf, path, leaf) path triples through the Abstract Syntax Tree.
- Path Attention: Aggregate path embeddings with learned attention weights.
- Finding: Developers can intuit the correct method name from code over 90% of the time — models initially achieved ~54% F1, validating the task's challenge.

Progress in Model Performance

| Model | Java-large F1 | Python F1 |
|-------|------------|---------|
| code2vec | 54.4% | — |
| code2seq | 60.7% | 55.1% |
| GGNN (Graph NN) | 58.9% | 53.2% |
| CodeBERT | 67.3% | 62.4% |
| UniXcoder | 70.8% | 66.2% |
| GPT-4 (zero-shot) | ~68% F1 | ~64% |
| Human developer | ~90%+ | — |

The Name Consistency Problem

Method name prediction enables a more commercially valuable variant: name consistency checking.

Given a method named calculateDiscount() whose body actually computes a total price, the model predicts "calculateTotalPrice" — flagging the inconsistency. This detects:
- Refactoring Decay: Method behavior changed during a refactor but the name was not updated.
- Copy-Paste Naming Errors: A method was copied and its body modified but name left unchanged.
- Misleading Names: Names that pass code review but mislead future maintainers.

Studies show ~8-15% of method names in large codebases are inconsistent with their implementation — a significant source of bugs and maintenance confusion.

Why Method Name Prediction Matters

- Code Quality Enforcement: Automated inconsistency detection in CI/CD pipelines catches misleading method names before they reach the main branch.
- IDE Rename Suggestions: When a developer changes a method's behavior during refactoring, an AI suggestion "consider renaming this method to 'processPaymentRefund'" based on the updated body improves code readability.
- Code Generation Context: Code generation models (Copilot) use method name prediction logic in reverse — given a method stub and its name, predict the implementation that correctly fulfills the name's semantic promise.
- Benchmark for Code Understanding: Method name prediction requires a model to demonstrate that it has understood what a piece of code does — making it one of the most direct code comprehension evaluations.
- Naming Convention Transfer: Models trained on well-named codebases can suggest canonical names for functions in code that violates naming conventions.

Method Name Prediction is the semantic code naming intelligence — learning the deep relationship between what code does and what it should be called, enabling tools that enforce naming consistency, suggest meaningful identifiers, and measure whether AI systems have genuinely understood the semantic content of arbitrary code functions.

Want to learn more?

Search 13,225+ semiconductor and AI topics or chat with our AI assistant.

Search Topics Chat with CFSGPT