NDepend Blog

Improve your .NET code quality with NDepend

Visual Studio IntelliCode : AI Assisted Coding

January 19, 2021 5 minutes read

We are all amazed by recent progresses made possible thanks to Artificial Intelligence (AI). In 2017 Microsoft announced Visual Studio IntelliCode which used Machine Learning (ML) to predict code completions, and Programming By Examples (PBE) to find repeated editing patterns. Intellicode was originally used to infer which language or library feature was likely to be intended at every keystroke. Nowadays Visual Studio IntelliCode supports several languages, more scenarios than just intellisense are proposed and it can learn from your code. This article introduces Intellicode actual capabilities (2021  Q1) that are:

  • AI-assisted IntelliSense
  • Context-aware code argument completions
  • Code Refactoring – IntelliCode provides better tailored repeated edit suggestions and refactor the codes accordingly.
  • Help following the code and style patterns. Patterns get created from your codebase itself.

Clearly the goal of IntelliCode is to improve developer productivity thanks to an AI based system. They try to add some sort of common-sense to existing Visual Studio tools!

Arguably one of the reasons Microsoft acquired github.com in 2018 was to have access to thousands of high-quality open-source projects. This way the software giant can train its IntelliCode AI to provide smart contextual recommendations. Actually only github projects that earned more than 100 stars are used to train the Intellicode AI.


Edit 20 january 2021: Mark Wilson-Thomas, a friend and a Microsoft Program Managers on Intellicode would like to clarify these points:

1) “We only use public GitHub repo access for the public models we ship. We want to make sure folks are clear that we aren’t doing anything untoward there. It is possible for users to set up a Team Completions model by granting access to their own code, see more details here – but those models are never made public, they are only for the team whose code they were created upon.”

2) Only IntelliCode Suggestions uses the Programming By Examples (PROSE/PBE) methodology. Our completions are AI/ML based models, with the newest using deep learning – see this article for more info.


Additionally, Intellicode provides added capability by enabling and analyzing your organization-specific project codebase and then provides a project-specific recommendation while you write new code or existing one.

Certainly more use-cases will come later. Progress and breakthrough in AI are notoriously hard to predict.

Getting Started

Language Support

  • C#, C++, Visual Basics, XAML, JavaScript, TypeScript, Java, Python, SQL

C# being the main Microsoft language nowadays, all Intellicode features are first previewed on C# code. As a consequence we can expect that Intellicode support for C# is more polished.

Prerequisites tools

  • Visual Studio 2019 (16.1 and above) – IntelliCode comes as a built-in feature if you install a supported workload.
  • or Visual Studio 2017 (15.8 and above) – Install IntelliCode from Marketplace or from VS Extension https://visualstudio.microsoft.com/services/intellicode/
  • or Visual Studio Code (1.29.1 and above)

Once you have prerequisites, you are all set to use IntelliCode using few configuration changes as explained below. I shall be explaining how to use IntelliCode tools for various features using VS2019.

Verify you IntelliCode settings by going to Visual Studio > Tools->Option->IntelliCode.

AI-Assisted IntelliSense support

IntelliCode saves developer time by providing smart contextual suggestions as developer type their code. The suggestions are smart in the sense that they are those that developers are more likely to be use based on the coding pattern and context of code written.

Once Intellicode installed successfully, you shall get to see IntelliSense support right in your IDE with the supported recommendation as you type your code.

C# code support

Intellicode CSharp Suggestion

TypeScript/JavaScript support

If you are using TypeScript or JavaScript languages then IntelliCode support will work in Visual Studio Code IDE providing recommendations as you type your code:

IntelliCode enable for Typescript code VSCode

To enable IntelliCode support for JavaScript/Typescript make sure to set Tools->Option->IntelliCode > Javascript/Typescript base model > Enabled in Visual Studio 2019:

AI-Assisted Refactoring support

IntelliCode understands your code edit using the Programming by demonstration/examples (PBD/PBE) systems that results from the PROSE Microsoft project team researches. PBE tracks your code edits and puts in perspective the “before” state against the “after” state. Then IntelliCode suggestions offer to apply that same edit in other similar places by making your repeated edits faster and more accurate.

Refactoring easy with IntelliCode

@ Line 49 code edit was detected by IntelliCode.

@ Line 56 IntelliCode suggested using the same refactoring based on the previous edit. You get a lightbulb suggestion along with a tab showing you possible edits for the code.

Refactoring with IntelliCode

To make use of the repetitive feature, just enable “Suggestion” setting as below:

AI-Assisted Refactoring with IntelliCode

Argument Completion

IntelliCode makes argument recommendations to help you choose the right argument quickly.

Intellicode argument completion

Here again, to make use of the argument completion feature, just enable “C# argument suggestions” setting.

Team Completions: Recommendations based on your code

As mentioned already, IntelliCode’s default recommendations are based on patterns learned from open-source GitHub repos.

When working with new code, these recommendations might be useful but not effective to you. In such case, IntelliCode can provide recommendations based on your code itself. Using your codebase, you can build a team model to provide recommendations like the method used by your code, utility classes, or domain-specific library. It is recommended that Intellicode gets trained a clean code base, else it will also learn and repeat the code-smell!

See below how to enable Team Completions:

IntelliCode team completions AI-assisted IntelliSense based on your code

Inferring code style and formatting conventions with Intellicode

IntelliCode lets you define code styles and formats using a .editorconfig file. For C# developers, IntelliCode can infer your code style and formatting conventions to dynamically create such .editorconfig file.

Enabling IntelliCode-generated EditorConfig file

Just go to the Visual Studio Solution Explorer and choose Add > New EditorConfig:

IntelliCode Generate an EditorConfig file

Visual Studio IntelliCode then dynamically populates the .editorconfig file from patterns spotted in your codebase.

.editorconfig file in Visual Studio Solution Explorer

Notice that Intellicode inferring EditorConfig on a large code base can take a little while:

Intellicode inferring EditorConfig on a large code base can take a few minutes

Sample .editorconfig file

See below a sample .editorconfig file. Note that C# formatting rule settings are modeled by fixed identifiers like csharp_new_line_before_open_brace that can take fixed values like csharp_space_after_colon_in_inheritance_clause or control_blocks.

Summary

IntelliCode is great to endeavor AI-based on the concept of programming by examples (PBE) and natural language. It looks to be one of the finest additions to the existing IDE feature considering the productivity of Developers. It helps developers on code refactoring, contextual suggestion, following common code patterns, smart IntelliSense and more. In an upcoming article I’ll try to predict what AI could bring to software development and developer productivity within this decade.