Enforce code cleanup on build
Tags: csharp, dotnet, codequality, programming
Published: 2021-05-10 19:57:30

#### .NET CODE ANALYSIS

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

[![](https://cdn-images-1.medium.com/max/800/1*ukabRI8tiLjAfQ7JMXLIZA.jpeg)](https://cdn-images-1.medium.com/max/800/1*ukabRI8tiLjAfQ7JMXLIZA.jpeg)<figcaption>Photo by <a href="https://unsplash.com/@blakeconnally?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Blake Connally</a> on <a href="https://unsplash.com/s/photos/programming?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption>

#### **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**

- Install latest visual studio [community edition](https://visualstudio.microsoft.com/downloads/).

### 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.”

[![](https://cdn-images-1.medium.com/max/800/1*HPdiaGUz-xto4p0JhZsjFQ.png)](https://cdn-images-1.medium.com/max/800/1*HPdiaGUz-xto4p0JhZsjFQ.png)

#### 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.

[![](https://cdn-images-1.medium.com/max/800/1*pgRgXwPVaPMprFT-iPBfxA.png)](https://cdn-images-1.medium.com/max/800/1*pgRgXwPVaPMprFT-iPBfxA.png)

#### **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.

[![](https://cdn-images-1.medium.com/max/800/1*zB4lIdCjOvQO3NzJxXiT-w.png)](https://cdn-images-1.medium.com/max/800/1*zB4lIdCjOvQO3NzJxXiT-w.png)

#### **Step 4: Run profile automatically**

[.NET code style analysis](https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview#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>
```

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);
```

#### **With braces**

```
if (condition){ Console.WriteLine(text);}
```

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](https://www.linkedin.com/in/sukhpinder-singh-532284a2/) [Instagram](https://www.instagram.com/sukhpindersukh/) [Facebook](https://www.facebook.com/sukhpinder.singh.52/) [Twitter](https://twitter.com/sukhsukhpinder)

#### Stay tuned on C#

[C Sharp Programming](https://medium.com/c-sharp-progarmming "https://medium.com/c-sharp-progarmming")