As we develop code, we can enforce our standards with the code review process in our pull requests. While the concept of “Shift left” is often used to describe the benefits of testing, it is also useful for code reviews. Completing automatic code reviews in Visual Studio before create a pull request and ask a human to review code is more efficient and allows developers to spend more time focusing on more complex code, rather than formatting and styles – in fact, some studies have shown that a large percentage of code reviews are focused on formatting and styling.
We can use EditorConfig files in Visual Studio to help achieve this. An EditorConfig file aims to codify these style rules, enabling your team to embrace “shift left” – the idea that the best time to discover and fix a bug is in Visual Studio while developing – not later in QA or production.
An EditorConfig file helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs. EditorConfig files are easily readable and they work nicely with version control systems.
We can either start from scratch with a blank .editorconfig file, or we can use IntelliCode to assist us. Today, we are going to use the IntelliCode option, adding an EditorConfig file by right clicking on our solution, selecting “Add”, and then selecting “New EditorConfig (IntelliCode)” (this is assuming you have IntelliCode already installed – which you should). IntelliCode will generate styles using the standards already present in our code and try to discover the correct style, based on what we are using.
The resultant file is less than a 100 lines, let’s dive into the details a little.
For example, the default style for explicit types in a new Visual Studio project is in the table below. What does this mean? “var” types can be used, and no (“none”) warning or error is shown.
|Rule name||language||Visual Studio |
In our projects, we tend to prefer explicit types for a number of reasons – mostly because we think the code is easier to read. IntelliCode looked at our code and identified this standard. While “var” is still allowed, it is “suggested” that an explicit type be used. We can change suggestion to one of four values:
- “none”: nothing is shown (in the “error list” window)
- “suggestion”: a message is shown
- “warning”: a warning is shown
- “error”: an error is shown
We are tempted to change to error – which will create a build compile error when we use a “var” type, but practically, a warning is going to be more useful for now. This will give us the flexibility to use a “var” type if we really need to, but the warning will help to remind us this needs to be corrected in the longer term. The table below shows our final settings.
|Rule name||language||Visual Studio |
After applying the rules, we add a simple “var number = 0;” line to a file, and observe this displays a warning as expected in the “error list window”.
We setup a few more rules in our EditorConfig file, and are happy with the rules we’ve defined. Here is the result of our new line and brace rules, which will generate a warning if the rule isn’t followed correctly.
IntelliCode uses AI to help develop your projects. To use it, download and install the extension. Then in the Windows menu, select “IntelliCode”, and the following window will display. Here we can “Train on my code”, which will use AI to learn about this projects code and patterns.
After a few minutes, this result is displayed. What does this mean? Now when we code in our project, it helps to suggest commonly used properties, methods and even arguments. The greatest thing about IntelliCode, in our opinion, is that it works so well, you forget that it’s there helping. This is definitely worth your time – the only downside is a few extra seconds to load your Visual Studio projects.
Today we learned a little about applying Shift Left to our projects, using EditorConfig files to set formatting and style standards, and a little about how IntelliCode can help us develop our projects efficiently. We encourage you to seek our more content about IntelliCode and how AI can help you, it’s growing rapidly.
- Shift Left:
- Editor Config Files: https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
- Editor Config Rules:
- Featured image credit: https://media.sproutsocial.com/uploads/2017/12/Social-Media-Optimization.png