Exploring Visual Studio 15: Code Styles

2016-05-02

Styling your code is important. You are reading your code more often then you’re writing it. Making sure that reading is as easy as possible, especially when you work with multiple developers on a project, can be a challenge.

Fortunately, Microsoft is investing in Visual Studio to help you with styling your code.

Code Styles in Visual Studio “15” Preview

How do you style your switch statements? Do you use indentations for your curly braces { } ? Where do you place your open brace? Does everyone on your team agree? How do you enforce these kinds of rules?

Visual Studio “15” helps you. If you go to the options menu in Visual Studio “15”, you will find a new group named Code Styles in Text Editor —> C#.

Code Styles - General

It’s now really easy to configure styles for your liking. The Options dialog box shows automatically updated sample code that follow the styles you configure. Figure 2 shows your preferences for indenting the content of a case statement in a switch.

Code Styles - Switch formatting

You can also apply coding styles to the naming of your elements. For example, you can enforce PascalStyling in the names of your symbols. If you go to Options → Text Editor → C# → Code Style → Naming, you can create a new naming rule. The following configuration enforces PascalStyle naming:

First, you select which symbols you want to apply the naming rule to.

MatchAllElements

Then you define the naming rule. In this scenario, only Pascal Case Name is chosen for capitalization.

PascalCaseStyle

Then finally, you configure the severity of the rule. In this case, I made it an error.

ConfigureNamingRule

Now, if you create a method that doesn’t start with an Uppercase letter, you get an error and an automatic code fix in the editor.

PascalCasing_UppercaseB

Some things that I would like to see for Code Styles is the ability to configure styling as a part of your project in Visual Studio. That way, you can bring your Code Styles under source control and share them with your team. Maybe we can even move the Code Styles to a NuGet package allowing us to share rule sets over multiple projects.

What do you think of Code Styles? Any feedback or comments?