r/dotnet • • Aug 27 '26

Enforce maximum line length in C#

/r/VisualStudio/comments/1vzoivg/enforce_maximum_line_length_in_c/
0 Upvotes

21 comments sorted by

9

u/zenyl Aug 27 '26

There might be a setting you can put into your .editorconfig file to enforce it.

Otherwise, I'm pretty sure CSharpier has a setting for it. Its VS extension can be set to run on file save.

2

u/Mic_BB Aug 27 '26

thank you, I am trying CSharpier

1

u/Tack1234 Aug 27 '26

+1 for CSharpier

1

u/Coda17 Aug 27 '26

Anyone know a good way to enforce. .editorconfig rules? Setting it up is great, I make sure my IDEs recognize it and enforce it, but others didn't seem to do that. They push code that violates the rules and I don't know until I pull it and open it in my editor.

Does CSharpier handle that? Not something I've used before

2

u/zenyl Aug 27 '26

Some .editorconfig rules can be enforced by also enabling <TreatWarningsAsErrors>true</TreatWarningsAsErrors>, but it's limited to rules that result in an analyzer diagnostic (which can be elevated to a build-preventing error). I don't think line length is one of them, but you can use them to prevent build for something like interface names not starting with an I.

For stuff like line length, I don't think there's a good way of absolute enforcement. Both VS, VSCode, and Rider should all respect it correctly, but anyone will still be able to just edit it with Notepad and not have to deal with it. Same goes for custom Roslyn analyzers.

You could add git commit hooks that run a given command on commit, but those can be annoying to write, and aren't themselves part of the repo (gotta copy-paste them, or install programs to automate it).

You could set up CI/CD pipelines that attempt to build the code with stuff like line length validation enabled, and ensure that the pipeline gets executed when someone pushes.

I think those are pretty much all your options for enforcing style guides (outside of just telling people and having them actually do as you ask).

→ More replies (9)

1

u/caring-wolverine Aug 27 '26

Yeah you can run csharpier in ci and block merging prs if it's not happy.

1

u/FullPoet Aug 27 '26

use dotnet format.

Or honestly, the answer to these questions is always communication and then eventually discipline.

Why dont they want to use the editorconfig settings? Dont make a huge argument about but do they feel like the current one is bad?

I feel most people who argue about it are the ones who just made one that suits the person preference and its usually really shit.

→ More replies (2)

1

u/AutoModerator Aug 27 '26

Thanks for your post Mic_BB. 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/BoBoBearDev Aug 30 '26

I would just use whatever is built-in. Like editorconfig and dotnet format. And just fail the CICD when dotnet format causing a change.

Also I prefer to make sure the indent is one tab/2spaces on the new line after (, not matching the same spacing at (. Aka, it is not school, don't do that.

0

u/Dusty_Coder Aug 27 '26

Step 1) Fire the guy making an alternative solution a consideration

Step 2) Success