AI-Assisted Engineering

AI-Assisted Programming: Can Machine Learning Help Developers Write Code?

Machine learning has transformed image recognition and translation. Can it do the same for programming? Research models like CodeBERT and products like TabNine and IntelliCode suggest it can already help. We look at what ML for code does today, why code is a promising target, and what remains hard.

By EthicalHat Research · · 3 min read
Infographic: What machine learning can do for developers in 2020

For decades, the tools developers use to write code have been built on explicit rules: compilers, type checkers, linters and IDE autocompletion driven by syntax and type information. Meanwhile, machine learning has transformed fields like computer vision, speech recognition and machine translation.

A natural question follows: can machine learning help developers write code?

The early answer, in 2020, is a cautious yes.

Why code is a promising target

In 2012, researchers published a paper titled On the Naturalness of Software, showing that source code, like natural language, is highly repetitive and predictable. Developers reuse the same idioms, API call patterns and naming conventions over and over. Statistical language models, it turned out, predict the next token of code even better than the next word of English.

Two trends have made that insight practical:

  • Massive amounts of public code on GitHub and other platforms to learn from.
  • Better models, particularly neural networks and transformers, that have revolutionised natural language processing over the past three years.

What ML for code does today

Smarter completion ranking

Microsoft's IntelliCode, launched in 2018, uses models trained on thousands of open-source repositories to put the most likely completions at the top of Visual Studio and VS Code suggestion lists, instead of alphabetical order. It sounds small, and it saves many keystrokes over a day.

Whole-line and multi-token completion

TabNine made waves in 2019 with "Deep TabNine", a completion engine based on OpenAI's GPT-2 architecture, trained on millions of files. It suggests longer completions, sometimes whole lines, across many languages. Kite offers similar ML-powered completions for Python.

python
# Typical of what ML completion engines predict well: common idioms
with open(path, "r") as f:
    data = json.load(f)          # <- suggested after "data = "

for i, item in enumerate(items):  # <- suggested after "for i, "
    ...

Instead of matching keywords, ML models can learn representations that let you search code by meaning, such as "read a CSV file and compute the average of a column". GitHub's CodeSearchNet challenge, released last year, provides a benchmark for exactly this.

Joint models of code and language

Last month, Microsoft Research published CodeBERT, a transformer pre-trained on both programming languages and natural language (code paired with documentation). It improves results on tasks like code search and generating documentation from code, and points towards general-purpose models for code, much as BERT did for text.

Bug detection and code review

Several tools use ML to flag unusual code patterns that often indicate bugs, learned from millions of past fixes in open-source history. Facebook has described internal systems that suggest fixes for common issues found by static analysis.

What remains hard

  • Understanding intent. Models learn what code usually looks like, not what this code is supposed to do.
  • Correctness. A statistically likely completion can be wrong: an off-by-one error, the wrong variable, a subtle logic bug.
  • Long-range context. Current models see a limited window of code, not the whole project, its architecture or its requirements.
  • Rare and novel code. The less common the pattern, the less helpful the model.
  • Security. Models learn from all public code, including insecure code, and can reproduce vulnerable patterns.

Privacy and intellectual property

Some ML completion tools send code snippets to cloud servers for inference. For proprietary or regulated code, check where processing happens; several vendors offer local models for this reason. Questions about training on public code with various licences are also beginning to be asked.

What to expect next

Given the pace of progress in natural language processing, where larger transformers trained on more data keep improving, it seems likely that code models will follow a similar path: bigger models, trained on more code, handling longer context and producing longer, more accurate suggestions. The gap between "complete this line" and "write this function from a description" may close faster than many developers expect.

The bottom line

Machine learning can already help developers write code, mainly by predicting common patterns, ranking suggestions and finding relevant examples. It does not understand what software should do, and it will not replace careful design, testing and review. But for the repetitive parts of programming, which are a larger share than we might like to admit, ML assistance is becoming genuinely useful.

Our secure SDLC team evaluates new developer tooling continuously and adopts what makes our engineers faster without compromising quality or security.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.