Skip to main content
QUICK REVIEW

[Paper Review] SCC: Automatic Classification of Code Snippets

Kamel Alreshedy, Dhanush Dharmaretnam|arXiv (Cornell University)|Sep 21, 2018
Software Engineering ResearchComputer Science5 references6 citations
TL;DR

This paper introduces SCC, a Multinomial Naive Bayes classifier trained on Stack Overflow code snippets to automatically classify programming languages in small code blocks. SCC achieves 75% accuracy and outperforms the proprietary PLI tool, which only reaches 55.5% accuracy on the same task, while also distinguishing between similar languages like C, C++, and C# and identifying C# versions with notable precision.

ABSTRACT

Determining the programming language of a source code file has been considered in the research community; it has been shown that Machine Learning (ML) and Natural Language Processing (NLP) algorithms can be effective in identifying the programming language of source code files. However, determining the programming language of a code snippet or a few lines of source code is still a challenging task. Online forums such as Stack Overflow and code repositories such as GitHub contain a large number of code snippets. In this paper, we describe Source Code Classification (SCC), a classifier that can identify the programming language of code snippets written in 21 different programming languages. A Multinomial Naive Bayes (MNB) classifier is employed which is trained using Stack Overflow posts. It is shown to achieve an accuracy of 75% which is higher than that with Programming Languages Identification (PLI a proprietary online classifier of snippets) whose accuracy is only 55.5%. The average score for precision, recall and the F1 score with the proposed tool are 0.76, 0.75 and 0.75, respectively. In addition, it can distinguish between code snippets from a family of programming languages such as C, C++ and C#, and can also identify the programming language version such as C# 3.0, C# 4.0 and C# 5.0.

Motivation & Objective

  • To address the challenge of accurately identifying programming languages in short code snippets, which is more difficult than classifying full source files.
  • To develop an open, accessible tool for classifying code snippets in online forums and code-sharing platforms where language tags are often missing.
  • To outperform existing proprietary solutions like PLI, which show poor performance on small code snippets despite high accuracy on full files.
  • To enable fine-grained classification, such as distinguishing between C, C#, and C++ or identifying C# versions (3.0, 4.0, 5.0).
  • To provide a reproducible, transparent alternative to closed-source tools for code snippet language classification.

Proposed method

  • A Multinomial Naive Bayes (MNB) classifier is trained on a dataset of code snippets extracted from Stack Overflow posts.
  • Features are derived from tokenized code snippets, focusing on keywords and syntactic patterns unique to each language.
  • The model is evaluated on 21 programming languages, including family languages (C, C++, C#) and versions (C# 3.0, 4.0, 5.0).
  • Performance is measured using precision, recall, F1-score, and accuracy across multiple language categories.
  • Comparison is conducted against the proprietary PLI tool using the same test set of 150 Stack Overflow snippets.
  • Top-10 features per language (e.g., 'class', 'console' for C#) are extracted to analyze model decision-making.

Experimental results

Research questions

  • RQ1Can a machine learning model achieve high accuracy in classifying programming languages from short code snippets, as opposed to full source files?
  • RQ2How does the performance of an open-source MNB-based classifier (SCC) compare to a proprietary tool (PLI) on code snippet classification?
  • RQ3Can SCC distinguish between closely related languages such as C, C++, and C#?
  • RQ4Can SCC identify specific versions of a programming language, such as C# 3.0, 4.0, and 5.0?
  • RQ5What are the most discriminative features for differentiating programming languages in small code snippets?

Key findings

  • SCC achieves an overall accuracy of 75% when classifying 21 programming languages from code snippets.
  • The average F1-score across all languages is 0.75, with precision and recall both at 0.76 and 0.75 respectively.
  • SCC significantly outperforms PLI, which achieves only 55.5% accuracy on the same test set of code snippets.
  • For distinguishing between C, C#, and C++, SCC achieves 80% accuracy, demonstrating strong performance on language family classification.
  • SCC identifies C# versions (3.0, 4.0, 5.0) with an accuracy of 61%, showing feasibility for version-level classification.
  • The model identifies language-specific keywords such as 'console' for C#, 'cout' for C++, and 'class' for C as top discriminative features.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.