How to write only good code

Original author: Mark Wilson-Thomas
  • Transfer
In May, at Build 2018, we introduced the Visual Studio IntelliCode extension. It helps to write code faster, avoid mistakes and offers developers hints, anticipating their goals. A few days ago there was an update, significantly aimed at improving interaction with C #. More under the cut!

By the way, the extension is already working with Python .

Following the announcement of the release of Visual Studio IntelliCode at the Build 2018 conference, we are pleased to present an updated extension of Visual Studio IntelliCode that supports the C # code design standards. It will help you and your colleagues get more readable and consistent code. If you are not familiar with the Intellicode extension, then IntelliSense technology with AI support is already built into it, which is referred to in the original announcement . If the extension is installed, then it probably has already been updated automatically. If not, you can install the extension right now by clicking on this link.

Entering standards for code design and formatting in the database

If the code is consistent, it is more convenient to maintain and read. A recent study by Microsoft Research in this area found that 18 percent of comments asking for help with code problems were due to design standards errors.

The updated IntelliCode extension allows you to generate an .editorconfig file on demand that perfectly matches the design standards used in your code. It is no longer necessary for the whole team to decide which standards to follow!

Having created this file once, you can use it to maintain the uniformity of the source code base for all members of the group. You can also quickly and easily fix formatting problems using the Quick Action menu Ctrl (Ctrl + Alt + Enter) or the new custom Format Document function (Ctrl + K, D) in Visual Studio 2017 version 15.8 Preview 3 and above.

How to use .editorconfig

IntelliCode creates the .editorconfig file by scanning the code files below your chosen point to create the most appropriate set of standards. When it is created, you will see messages on violations of code style standards appear in the error list; An example is shown below:

Default style violations are of “Message” importance. This shows the “suggestion” suffix in each line of the rule in the .editorconfig file. You can customize the file if another execution level is required, for example, “Warning” or “Error”. Please note that format violations do not appear in the list of errors, but can be easily corrected (see our guide below).

You can start the generation of EditorConfig through the Solution Explorer. Just right-click on the folder with the solution, project or subfolder to which you want to apply the standard, click add (“add”), and then EditorConfig file (IntelliCode) (File EditorConfig (IntelliCode)). In the case of a project, you can also click add (“add”), then new item (“new item”) and EditorConfig file (IntelliCode) (File EditorConfig (IntelliCode)).

Note that the EditorConfig files are enforced in a hierarchical order to all the files under them. For example, if you need to apply a set of standards to the entire solution, place the .editorconfig in the solution directory. If you have different standards or projects, create a new EditorConfig file for these subfolders / projects to overwrite higher standards.

How to fix the code to meet the new standards

There are several ways to clean up the code to meet the new standards, depending on your goals.

  1. Using the quick action menu of Lightbulb (Ctrl + or Alt + Enter)

    If there is a style problem, a light will appear (as well as a list of errors) so that you can automatically correct any inconsistencies. In the Lightbulb menu, you can select the area of ​​correction: instance, document, project or solution.
  2. Using the Format Document function (Ctrl + K, D)

    If there are problems with the format or style and you are using Visual Studio 2017 15.8 Preview 3 and higher, then refer to the extended Format Document function to further clear the code of the current document. It will help correct the code in this document, and it will meet the standards you have just created, and you will be able to clean the files right away as you work with them, ensuring consistency.

    Please note that the additional option of formatting a document only affects a specific set of problems that can be changed in the Tools> Options> Text Editor> C #> Code Style> Formatting> General> Format Document Settings (Experiment) menu (Tools> Options> Text Editor> C # > Code Style> Formatting> General> Document Format Settings (Experimental)):

And note for yourself that the default settings do not fix some standards of style. You can add them using the tool settings (for example, the “Apply hidden / shown types” setting will trigger style rules related to the use of variables in the code).

What you need to know when working with EditorConfig

There are several known issues to consider when working with EditorConfig:

  1. You need to close and reopen the files so that the applied changes to the standards in version 15.8 Preview 2 and earlier are displayed.

    In versions of Visual Studio 2017 that were released earlier than 15.8 Preview 3, you should close and reopen all open documents so that the changes that occurred after creating standards in the EditorConfig file are displayed. This is fixed in version 15.8 of Preview 3.
  2. When you create a file using the File-New Item dialog box, “1” is added to the file name.

    If you use the path to the File-New Item dialog box in Visual Studio versions released before 15.8 Preview 3 to create the EditorConfig file, you must manually remove the extra prefix "1" in the EditorConfig file name. This is fixed in version 15.8 of Preview 4.

Thank you for your interest in IntelliCode. We will be happy to receive your feedback and improve IntelliCode as the project evolves. Use the suggestions page to give your opinion. If you find any problems with the extension, use the Report a Problem function built into Visual Studio , write in your report that this is IntelliCode. This will help us gather the necessary information to solve the problem.

Make a contribution

As we expand our capabilities to cover more use cases and programming languages, we will provide limited preliminary access to IntelliCode.

Sign up to get access to a limited trial version to become a participant in the test and learn about the changes first.

Also popular now: