DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Enforce code cleanup on build
Sukhpinder Singh
Sukhpinder Singh

Posted on

Enforce code cleanup on build

.NET CODE ANALYSIS

Create code cleanup profiles and run on the build-in Visual Studio.

Photo by Blake Connally on Unsplash

The article demonstrates how

  • To create code cleanup profiles in the Visual Studio solution explorer.

  • Run code cleanup profile manually or automatically on the build.

  • How some of the Visual Studio 2019 code analysis features can improve the quality of our C# code and save us a lot of time cleaning up and organizing code.

Prerequisites

Getting Started

Open any .Net Framework or .Net project in the Visual Studio.

Step 1: Open Code Cleanup Profiles

Right-click on solution or project and go to β€œAnalyze and Code Cleanup” and click β€œConfigure Code Cleanup.”

Step 2: Configure Profiles

Move available fixers from the bottom panel to the β€œIncluded Fixers” panel and click β€œOK.”

Most commonly used fixers:

  • Remove unnecessary casts , i.e., to remove where typecasting is not required.

  • Apply expression/block body preference

  • Remove unused variables , i.e., remove the variables that are declared but never used.

  • Remove unnecessary using , i.e., to remove the imports that are no longer used in the β€œ.cs” files.

Step 3: Run profile manually

Right-click on solution or project and go to β€œAnalyze and Code Cleanup” and click β€œRun Code Cleanup (Profile 1)” or whichever profile is configured on Step 2. As shown in below screenshot.

Step 4: Run profile automatically

.NET code style analysis is disabled by default, on build for all .NET projects. You can enable code style analysis for .NET projects by setting the EnforceCodeStyleInBuild property to true.

<PropertyGroup> <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild></PropertyGroup>
Enter fullscreen mode Exit fullscreen mode

After this project code cleanup will run automatically on the project or solution build.

Customize Visual Studio code analysis settings

Every company has specific code styling guidelines, and it may be that the default Visual Studio settings for code analyzing don’t meet those preferences. For example, some companies favor using braces even when they implement them to a single line, while others prefer not to use them.

No braces

if (condition) Console.WriteLine(text);
Enter fullscreen mode Exit fullscreen mode

With braces

if (condition){ Console.WriteLine(text);}
Enter fullscreen mode Exit fullscreen mode

Configuration path in Visual Studio: Select _ Tools > Options > Text Editor > C#. _

Thank you for reading and hope you liked the article. Follow me on LinkedIn Instagram Facebook Twitter

Stay tuned on C

C Sharp Programming

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.