Shifting left with IntelliCode and EditorConfig files

Posted by

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.

EditorConfig files

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 namelanguageVisual Studio
default
csharp_style_var_for_built_in_typesC#true:none
csharp_style_var_when_type_is_apparentC#true:none
csharp_style_var_elsewhereC#true:none

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 namelanguageVisual Studio
default
csharp_style_var_for_built_in_typesC#false:warning
csharp_style_var_when_type_is_apparentC#false:warning
csharp_style_var_elsewhereC#false:warning

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

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.

Wrapup

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.

References

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s