Prettier your C# code with DotNet Format
Published: 2021-11-06 16:36:24

This week, I learned about Static Analysis Tool, and I practice applying it to my [Shinny-SSG]() application. The two tools I used are [Dotnet Format](https://github.com/dotnet/format) and [StyleCopAnalyzers](https://github.com/DotNetAnalyzers/StyleCopAnalyzers). I chose them because they are widely used and they are both analyzing source code based on the *.editorconfig* file. 
## Installation

### EditorConfig
In the first step, I generated the *.editorconfig* file for my project. This [Editorconfig](https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019) file is where you define your coding style in Visual Studio, such as Indentation and Spacing or Wrapping Preference. 

### DotnetFormat 
The second step is installing DotnetFormat tools in my local machine by this command:
```dotnet tool install -g dotnet-format``` 
This tool will format my code based on the style preferences on the *.editorconfig* file. 

### StyleCop.Analyzers
The third step is installing StyleCop Analyzers. As specified in the documentation, I followed the preferable way is to add the NuGet package StyleCop.Analyzers. I also found this [blog](https://dev.to/srmagura/c-linting-and-formatting-tools-in-2021-bna) is helping, and I followed step by step which the author recommended. In the Project File extension, I added this line to the project so it can trigger StyleCop at the build time:
```XML
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" Condition="$(MSBuildProjectExtension)=='.csproj'">
	<PrivateAssets>all</PrivateAssets>
	<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
```
The last step is to edit my *.editorconfig* file to disable the rule that I don't want to my project, such as: Using directive should appear within a namespace declaration.
```
dotnet_diagnostic.SA1200.severity = None
```
However, at the build time of my project, it still showed a warning for this rule, so maybe I should research more about it.

## Build Event
I was struggling to find a way to fix the warning from StyleCop for my project automatically. I had 360 warnings, and it will be time-consuming to fix them manually. Luckily, I read the [blog](https://dev.to/mkim219/formatter-linter-for-c-cb0) of my classmate, Minsu Kim, he mentioned that dotnet format can be used with third-party analyzers. It is a great find, and I can use this command to fix all the warnings from style-cop:
```dotnet-format -a warn ./shinny-ssg.sln```
By using this command, I did not need to modify my Project file extension to trigger StyleCop.Analyzers at the build time and I can leave all the works to dotnet-format.

Microsoft Visual Studio has [build events configuration](https://docs.microsoft.com/en-us/visualstudio/ide/how-to-specify-build-events-csharp?view=vs-2019) for projects, and I used it to trigger auto-format code action at the time of building project. I have set up my project as follows:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r94htcmp2d85roq1rgur.png)
After I ran the _dotnet build_ in my project, two dotnet-format commands have fixed six formatting errors, and 360 warnings.

## Editor/IDE Integration
Visual Studio provides a great way to replicate the development environment through a _vsssetings_ file. New contributors to my project can have the same development environment as me by importing the ShinnySSG-2021-11-06.vssettings, which is available in the Project repository. 

## Conclusion
Formatting code is the first way to improve code quality, and it makes easier for reviewers to spot bugs and give suggestions. I am happy that I have found great tools to make this task much more easier.