Coding C# with quality and style

Back in the Java world I was very much used to relying on static code analysis tools. And yet, it took me a good while until I finally set up similar tools for my C# development. But here it finally is.

The tools

I use Rider for my IDE, so legacy analyzers from Visual Studio are not even an option. They wouldn’t be an option anyway as they’re not available for newer .NET Core and .NET Standard projects.

Rider comes with its proprietary Code Inspections, which apply about 2500 rules. I think this includes both code quality and code style checks. Code style can be configured with both IDE settings and EditorConfig.

Up next comes a set of Roslyn based analyzers from Microsoft, bundled in FxCop analyzers package. They look much deeper for code quality issues than Rider’s code inspections. As far as I understand, this is the closest to ‘standard quality rules’ you can get for .NET Core. I think it is possible to configure those analyzers with XML ruleset files but I’m happy with the defaults.

And that’s it. Nice and easy. I looked into hugely popular StyleCop, but I’ve found some of their rules too strict and not sufficiently configurable. I like to set my own order of class members and I don’t feel like putting comment headers in every file. I also tried Roslynator, but it didn’t come up with any suggestions for my projects so apparently there is some overlap with FXCop analyzers.

The config

I use EditorConfig for applying code style fixes upon code cleanup in Rider. It’s mostly Rider’s defaults, with many suggestions elevated to Warnings. Besides that, I do allow brace-free single line constructs like:

if (condition) continue;

Or expression bodied properties:

public Length => Max - Min;

But I will add braces as soon as the expression is multi-lined.

I also add breaks for long lists of method parameters or method call arguments. I like when those line up nicely in a column. For example:

public GridBoundingBox SetPosition(
int x,
int y,
IntervalAnchor xAnchor,
IntervalAnchor yAnchor,
int xOffset = 0,
int yOffset = 0)
{
return new GridBoundingBox(
XInterval.SetPosition(x, xAnchor, xOffset),
YInterval.SetPosition(y, yAnchor, yOffset));
}

It’s on my github if you want to try it out.

I also use Rider’s File Layout Patterns engine to keep the structure of my source files nice and tidy. The pattern is written in XAML and can be applied upon code cleanup as well. This functionality is also available in ReSharper and they even got a visual editor for this. Visual editor in Rider is in the works.

Here I kept Rider’s defaults for XUnit code which puts Setup/Teardown methods on the top, then other methods and tests on the bottom.

My layout for other classes is trying to follow potential call order. Effectively it puts static code on the top, then constructors, then instance code. Internal types go last.

The XML pattern you can also find here. Put it in Settings->Editor->CodeStyle->C#, File Layout tab. To apply it you need also to run code cleanup profile on which Apply file layout is enabled.

The missing parts

Currently my code style settings from editorconfig file are only applied by IDE. Same goes for file layout, I need to run code cleanup in Rider to get that. What I’d like to do is to also include some editorconfig checks on build time, so that I can run style checks on build server.

I’ll get to that some day.

Leave a Reply

Your email address will not be published. Required fields are marked *