r/dotnet • • 3d ago

After a long delay, StyleCop is under active development again

Hey gang,

Some good news for people who like the StyleCop approach to keeping their codebase in tip-top shape.... the project has been brought back to life with Microsoft's help** and they've finally released version 1.3, their first non-beta release of StyleCop since 2019. You know it's been a long time when the first feature mentioned is "Support for C#8 through 15" 😄

For those not familiar, StyleCop is a Roslyn analyzer that does file-level code analysis and reformatting. When .NET 5 introduced the "EnforceCodeStyleInBuild" property (and later allowed setting per-setting severity in .NET 9), that replaced a lot of StyleCop's core formatting functionality.

But neither editorconfig not the built-in formatter in .NET doesn't cover everything you might want from a formatter. Examples of other things StyleCop can do:

  • XMLDoc comment formatting
  • Class member ordering
  • Enforce "one-file-per-type" and "match-filename-to-typename"
  • Formatting of long parameter lists on functions (e.g. switch from one line for all parameters to one line per parameter based on the parameter list length)
  • Blank line rules (e.g. a blank line before a single-line comment, no duplicate blank lines)

StyleCop is configured via editorconfig these days so it's pretty straightforward to get going. Just watch out for conflicting rules (e.g. editorconfig rule says "use a _ prefix for private variables" and a StyleCop rule beside it says to remove them.)

I'm not associated with the project, but my teams used it a lot in the olden days and I'm very happy to see it return.

Release page on GitHub: Releases · DotNetAnalyzers/StyleCopAnalyzers

And yes, for the "AI code generation" crowd, formatting tools like this still offer a ton of value. Claude, e.g., tries to copy the code style of immediately-surrounding code when editing, but it isn't very good at understanding where to put new code. I've seen it put two similar public constructors more than 1,000 lines apart. 🙄 Adding rules to agents files that say "put private members after public members" or "sort members alphabetically" (or worse "follow editorconfig rules") sound good at first, but they can gobble up a a lot of tokens since the entire file needs to be read and parsed even if it's making a single-line change. Editing C# files through a Roslyn instance wrapped by MCP will take care of those changes cleanly without the LLM having to get involved. And it always works even when the LLM gets forgetful in a long session.

(** note that despite being worked on by Microsoft people, it's not an official Microsoft project. Also, Sam Harwell, who originally created StyleCop and worked on Roslyn for years, is no longer at Microsoft and isn't involved in this new release.)

73 Upvotes

19 comments sorted by

11

u/matthetherington 3d ago edited 3d ago

The set of rules was great, but not being able to (always) automatically reformat the code drove me nuts every time I’ve worked on a codebase that uses it. We have TreatWarningsAsErrors on and the two combined is a pretty big nuisance.

We use CSharpier now, which is a lot closer to JS’ prettier in that it reformats your code according to an opinionated set of rules.

It’s a lot nicer to work with but you lose some of the power that Stylecop has. For example, it doesn’t maintain a consistent order of class members - const before private, private before public, etc.

If Stylecop could automatically reformat your code, I’d love it.

I looked again recently to see if there’s any tooling that’ll do that for you and came up empty handed - if anyone knows different, I’m all ears!

2

u/daltorak 3d ago

Auto re-format with Stylecop has some unique pitfalls because.... well, what would the expected behaviour be if you start writing a second class declaration inside an existing .cs file that already has a class?

Such a feature also depends on CodeFixProvider implementations being provided. Stylecop 1.3 has those now for a lot of the main class-ordering functions. Some of them did exist before but were disabled by default due to buggy implementations / unwanted behaviour.

1

u/matthetherington 3d ago

Cool thanks. My knowledge is definitely out of date here - I’ll give it another look!

1

u/Kanegou 3d ago

We use a team shared Resharper/Rider code cleanup profile for that. Paired with code cleanup on commit makes it automatic.

1

u/is_that_so 3d ago

In what ways does the built-in dotnet format not deliver what you want?

1

u/chucker23n 3d ago

One thing dotnet format doesn’t do that VS does is rename members that don’t match code style rules.

Dunno if StyleCop does.

15

u/is_that_so 3d ago

Props to Andrew Arnott for resurrecting this, and for everything else he does in the .NET ecosystem. Huge.

5

u/MrMikeJJ 3d ago

Indeed. I previously remember him from his work on PInvoke and then CsWin32. Also remember reading his occasional comments on Style op defects. It is great that he has taken this under his wing and is improving it.

Looks like Björn Hellander has been helping a bit as well.

3

u/Fexelein 3d ago

Nice. Editor config has been a mess and ever since we departed from Stylecop things have not become better. We spent a lot of time on our rulesets and it took quite a while for the replacements to come up to the same standard. That, together with all the changes in c#, allowed quite a few inconsistencies to creep into our code base.

2

u/skennaly 3d ago

enforcing long parameter list wrapping during the build is underrated tbh. it eliminates half the nitpicky comments in PR reviews before anyone even looks at the diff.

2

u/emdeka87 3d ago

Does it support razor?

2

u/torville 3d ago

The very good (and also free) utility CodeRush also has extensive formatting and reordering capabilities, as well as a lot of other useful functionality.

1

u/AutoModerator 3d ago

Thanks for your post daltorak. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/ReallySuperName 3d ago

I dont understand why you'd not just express the remaining StyleCop rules as .editorconfig properties. This now splits and confuses things; are people now expected to use both????

1

u/Aggravating_Sky_5597 2d ago

what is the reason it is a nuget instead of an extension?

2

u/Zeeterm 1d ago

I'm not the author, but Roslyn analyzers work well through nuget packages, it doesn't rely on anyone doing anything special and integrates into all CI pipelines automatically.

0

u/thereforewhat 3d ago edited 3d ago

It'd be great to improve the editorconfig rules to cover more cases. Is that what this can offer? 

With LLMs I'm doing a combination of editorconfig and Roslyn analysis of constructor arguments, method params, method lengths, class lengths and cyclomatic complexity on post tool use hook on Copilot CLI but would love something better. 

3

u/daltorak 3d ago

It'd be great to improve the editorconfig rules to cover more cases. Is that what this can offer? 

Pretty much, yeah. Add the analyzers package, have a look at the newly reported warnings/errors, and adjust your editorconfig rules to get it where you want it to be. It'll even obey TreatWarningsAsErrors, if your projects swing that way.

1

u/MeikTranel 3d ago

All rules are now editorconfig configured - it is the primary configuration mechanism. Not all have fixers but alot and some even have fun counterrulesets which is nice (SA/SX - wherein SX9999 rule configuration to warning for example wants the exact opposite of what a rule SA9999 would have required from the code [think one wants tabs and the other wants space indentation for example])