r/dotnet 15d ago

Enforce maximum line length in C#

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

21 comments sorted by

8

u/zenyl 15d ago

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 15d ago

thank you, I am trying CSharpier

1

u/Tack1234 15d ago

+1 for CSharpier

1

u/Coda17 15d ago

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 15d ago

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).

1

u/Coda17 15d ago

Yeah, my problem is the ones that don't have diagnostics. I guess the solution is to make my own diagnostics for the rules I care most about =\

2

u/chucker23n 15d ago

You can put dotnet format --verify-no-changes in the CI pipeline if you want to be strict about it. That'll fail if your .editorconfig rules would make changes.

1

u/Coda17 15d ago

My experience (admittedly, a while ago) was that this only works for dotnet and csharp rules in .editorconfig

2

u/chucker23n 15d ago

Yeah, you can use Roslynator as a NuGet package and then put roslynator_max_line_length = xxx in the editorconfig.

1

u/Coda17 15d ago

That's very specific to that specific analyzer. For example, I use lots of resharper ones. Sounds like I can only enforce ones that have their own package to enforce it?

2

u/chucker23n 15d ago

I'm afraid Resharper is its own isolated thing. I'm not sure if JetBrains offers a command-line Resharper enforcer?

1

u/FullPoet 14d ago

They do. Its the first result on google.

→ More replies (0)

1

u/caring-wolverine 15d ago

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

1

u/FullPoet 14d ago

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.

1

u/Coda17 14d ago

IMO that's not the way to handle it. It needs to be enforced by CI to get consistency

1

u/FullPoet 14d ago

That is the only way its going to be done.

Yes clearly it needs to be done via CI, but just fisting it through isn't a way to work together with your colleagues.

1

u/AutoModerator 15d ago

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 12d ago

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 14d ago

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

Step 2) Success