DEV Community

Michał
Michał Subscriber

Posted on

Exploring Code Search with CodeBERT – First Impressions

Recently, I’ve been exploring AI models that aim to solve the code search problem, and I came across CodeBERT from Microsoft. The repository can be found here: https://github.com/microsoft/CodeBERT/tree/master.

The project approaches the code search task in two ways, but today I want to focus on the first approach I looked into: using the basic CodeBERT model.

In the paper "CodeBERT: A Pre-Trained Model for Programming and Natural Languages," the authors highlight their achievements, claiming state-of-the-art results for code search tasks. Naturally, I was curious to see how it works.

The approach is based on binary classification:

The model takes two inputs: a natural language query as the first part of the vector and a code snippet as the second.

It outputs either 0 (no match) or 1 (match).

For this to work in a code search software:

  1. The code needs to be split into smaller fragments, such as functions or methods.

  2. A user provides a query describing the function they’re looking for.

  3. The algorithm iterates through all code fragments, combining the query with each fragment to create input vectors.

  4. These vectors are passed through the model, which determines whether the query matches a particular fragment.

The output is a list of code fragments that align with the user’s query.


While this approach works conceptually, it’s not particularly efficient for larger repositories, nor practical for real-world problems. Iterating over each fragment and classifying it one by one can be time-consuming and impractical at scale. It might be a helpful solution for smaller projects, but I don’t see much value in implementing a code search engine for small repositories where traditional search methods often suffice.
I wonder if there are more advanced methods out there.

Next, I plan to take a closer look at GraphCodeBERT, hoping it might offer a different perspective on the problem.

I’d love to hear from you:

Are there any tools or models you’ve used for code search that integrate well into real-world workflows?

Are there solutions you’ve been curious to explore but haven’t had the time to test yet?

Any suggestions or experiences you’re willing to share would be greatly appreciated.

Top comments (0)