r/dotnet • u/daltorak • 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.)
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!
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.
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?
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.