Stepping into Static Analysis Tooling
Tags: opensource, csharp, git, github
Published: 2021-11-06 06:53:30

## Got an Item Crossed off of My Bucket List of Wonders
This time I finally got an opportunity to learn about Static Analysis Tooling. Before when I encountered those, I wouldn't even know what they are officially called,  but always thought that they were pretty cool. These tools always make a great coding experience by automating things like formatting (Code Formatter) and clean up  of the code (Linter). 

I also learned that README.md is not the only .md file out there. Apparently, there is also a CONTRIBUTING.md file that would hold all the development and setup related info. 

## Code Formatter

### Why dotnet-format?
For the Code Formatter, I decided to go for [dotnet-format](https://github.com/dotnet/format) over [ReSharper](https://www.jetbrains.com/resharper/) and [CodeFormatter](https://github.com/dotnet/codeformatter). The main reason why I didn't choose CodeFormatter was because the last contribution to it was about 8 months ago, so to me, it just looked kind of suspicious... (is it an abandoned project now??) At first, I was going to choose ReSharper extension, but it seemed like it wasn't for free, so I started using dotnet-format, which was actively maintained and recommended by my classmate. After trying out for a while, I couldn't use dotnet-format to interact nicely with my linter [StyleCop.Analyzer](https://github.com/DotNetAnalyzers/StyleCopAnalyzers), so I decided to work with the ReSharper command line tools, which were for free. After running it, I noticed that it wasn't as good as I thought, so I kept researching and asking questions on how to make dotnet-format work with StyleCop. After a few hours of research and trying out stuff, dotnet-format finally worked with StyleCop.

### Setup Process

Before installing dotnet-format, I had to install the SDK for .NET 6. Thus, I downloaded the .NET 6 SDK installer from the [here](https://dotnet.microsoft.com/download/dotnet/6.0). Although this is just a release candidate (rc) version, so be mindful that a stable version might be released in a year. After installing the SDK, I ran the command:

```
dotnet tool install -g dotnet-format --version "7.0.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json
```

TaDa!! now, you have your dotnet-format installed. 
 
## Linter

### Why [StyleCopAnalyzers](https://github.com/DotNetAnalyzers/StyleCopAnalyzers)?  

Why? Because that's the only Linter I knew 😅😳😳

### Setup Process
1. Open a terminal in Visual Studio.

2. Type in the following command:

```
Install-Package StyleCop.Analyzers
```
Note that once you install this Linter, you don't have to use any commands to make use of it. It is already integrated with the IDE. It will automatically start showing warnings for each file you open in your solution. 

## Issues Linter and Formatter Fixed in My Code

Once I run the 'dotnet format', it started fixing everything it could automatically. It changed almost all the files I had in my repo. I didn't have to fix anything manually. 

For the Linter, however, it threw a lot of warning at me. However, after I run the dotnet-format, most of the warnings have disappeared. I still had to fix some of them manually though. The leftover warnings are mostly related to documentation of the code, which I have to type in manually. 

## Outcomes

Setting up tools is not as easy as in just downloading something. Sometimes the Linter you chose won't work with the Code Formatter you use, or vice versa. 

Also, you would have to do a lot configuration and setting up. I would expect to get stuck and to go back and forth between some options, especially if you are just being introduced to Static Analysis Tooling. 

However, at the end of the day, it is all worth it. It will help you to maintain your code a lot. It is like an insurance  that makes sure that there won't be silly bugs and poorly formatted code in your repo.