Clean Code Tools for .NET Core

Scott Kuhl
3 min readFeb 11, 2019

--

When you create a new ASP.NET Core project in Visual Studio, you’re going to get a little help from the experts to keep your code clean. These come in the form of .NET Compiler Platform analyzers, also known as Roslyn Analyzers.

This is what my ASP.NET Core 2.2 solution looks like right after being created.

Default code analyzers

So what do these do? They watch your code as you type it, looking for bad code smells.

Analyzers look at code style, code quality and maintainability, code design, and other issues. — Overview of .NET Compiler Platform analyzers

When they see something they don’t like, a little squiggly line will appear under the bad part (and they show up in the error list when you build your project). You can use the helpful little lightbulb to tell you more and help you fix it. The funny part is, it finds some stuff immediately wrong with the code Microsoft’s own template just created.

Program has a issue

They are not all knowing, sometimes they might suggest things you really, really want to do. That what that Suppress CA105 is for above. But if you find yourself using that a lot, stop and think.

You can also add more analyzers to your project as NuGet packages. This is nicer than the older way of making sure everyone installs the same third party tools. (It is still possible to install many of these as extensions to Visual Studio for Windows as well, but not the Mac version, I just prefer the NuGet approach.)

Roslynator

Code with me, if you want to live

My personal favorite at the moment is Roslynator.

A collection of 500+ analyzers, refactorings and fixes for C#, powered by Roslyn. — Roslynator

We have replaced our use of ReSharper on my team for new projects because it captures a lot of the same issues and runs much, much, much faster.

SonarLint

SonarLint is another popular code analyzer that has been around longer than Rosylnator. It’s free, but also can be extended with SonarQube to make a more powerful solution.

SonarLint also supports VS Code, where Roslynator still has a long open issue for it.

Code Analyzers can help you write better code. But don’t rely on them for everything and don’t go crazy installing everyone you can find. Pick some favorites.

And remember, these analyzers do not replace the need for a code review from peers, but at least they help find the low hanging fruit.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response