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.
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.
- 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
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.
@ 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.
To make use of the repetitive feature, just enable “Suggestion” setting as below:
IntelliCode makes argument recommendations to help you choose the right argument quickly.
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:
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:
Visual Studio IntelliCode then dynamically populates the .editorconfig file from patterns spotted in your codebase.
Notice that Intellicode inferring EditorConfig on a large code base can take a little while:
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.
#use soft tabs (spaces) for indentation
indent_style = space
#Formatting - new line options
#place else statements on a new line
csharp_new_line_before_else = true
#require members of object intializers to be on separate lines
csharp_new_line_before_members_in_object_initializers = true
#require braces to be on a new line for methods, object_collection_array_initializers, control_blocks, types, and lambdas (also known as "Allman" style)
csharp_new_line_before_open_brace = methods, object_collection_array_initializers, control_blocks, types, lambdas
#Formatting - organize using options
#sort System.* using directives alphabetically, and place them before other usings
dotnet_sort_system_directives_first = true
#Formatting - spacing options
#require NO space between a cast and the value
csharp_space_after_cast = false
#require a space before the colon for bases or interfaces in a type declaration
csharp_space_after_colon_in_inheritance_clause = true
#require a space after a keyword in a control flow statement such as a for loop
csharp_space_after_keywords_in_control_flow_statements = true
#require a space before the colon for bases or interfaces in a type declaration
csharp_space_before_colon_in_inheritance_clause = true
#remove space within empty argument list parentheses
csharp_space_between_method_call_empty_parameter_list_parentheses = false
#remove space between method call name and opening parenthesis
csharp_space_between_method_call_name_and_opening_parenthesis = false
#do not place space characters after the opening parenthesis and before the closing parenthesis of a method call
csharp_space_between_method_call_parameter_list_parentheses = false
#remove space within empty parameter list parentheses for a method declaration
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
#place a space character after the opening parenthesis and before the closing parenthesis of a method declaration parameter list.
csharp_space_between_method_declaration_parameter_list_parentheses = false
#Formatting - wrapping options
#leave code block on single line
csharp_preserve_single_line_blocks = true
#Style - Code block preferences
#prefer curly braces even for one line of code
csharp_prefer_braces = true:suggestion
#Style - expression bodied member options
#prefer block bodies for constructors
csharp_style_expression_bodied_constructors = false:suggestion
#prefer block bodies for methods
csharp_style_expression_bodied_methods = false:suggestion
#prefer expression-bodied members for properties
csharp_style_expression_bodied_properties = true:suggestion
#Style - expression level options
#prefer out variables to be declared inline in the argument list of a method call when possible
csharp_style_inlined_variable_declaration = true:suggestion
#Style - Expression-level preferences
#prefer objects to be initialized using object initializers when possible
dotnet_style_object_initializer = true:suggestion
#Style - implicit and explicit types
#prefer var over explicit type in all cases, unless overridden by another code style rule
csharp_style_var_elsewhere = true:suggestion
#prefer var is used to declare variables with built-in system types such as int
csharp_style_var_for_built_in_types = true:suggestion
#prefer var when the type is already mentioned on the right-hand side of a declaration expression
csharp_style_var_when_type_is_apparent = true:suggestion
#Style - language keyword and framework type options
#prefer the language keyword for local variables, method parameters, and class members, instead of the type name, for types that have a keyword to represent them
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
#Style - modifier options
#prefer accessibility modifiers to be declared except for public interface members. This will currently not differ from always and will act as future proofing for if C# adds default interface methods.
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion
#Style - Modifier preferences
#when this rule is set to a list of modifiers, prefer the specified ordering.
csharp_preferred_modifier_order = public,private,static,readonly:suggestion
#Style - qualification options
#prefer fields not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_field = false:suggestion
#prefer methods not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_method = false:suggestion
#prefer properties not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_property = false:suggestion
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.