Best pratices?
Hi,
I work in a team doing Unity 3D development and I want to enforce good practices for developers. By altering the auto generated .csproj from Unity I can "inject" my .editorconfig and ruleset file and use it seamlessly which I am pretty happy about it. I found out that dotnet format is a good tool for this as this cover the appearance of the code but also more structural code issues (notably with analyzers especially since we can write our own). As such we have a way to enforce code consistency and raise code quality without much efforts.
The part where I am not sure to go is how to integrate it for the team. Specifically I wonder about the following:
Should everybody run before submitting a PR? If yes, how can I make sure that every formatter would give the same output on everybody computers?
Should a robot do it automatically for everyone, or at least check that you have done it? If so, do you have some example on how dotnet format is integrated in some build pipeline? What would be the best way to make sure that no change are needed? Using --verify-no-changes or dumping a report (using --report) and parsing it?
And of course, if you have general advice or can share how you use it for your projects I would gladly take those hints :)
With formatters and analyzers the best practice is to
- Format/run analyzers on the entire code base to get things following the rules you want to enforce.
- When a PR is submitted, an action should validate that all the new changes follow those rules. If the rules aren't followed, prevent the PR from merging.
This keeps your main branch clean, and forces all devs to follow the rules. How they follow the rules is up to them. They can wait til their PR is ready, or clean things up as they go. You can also set up pre-commit hooks to run some of those tools automatically. Husky.net is one way of doing it. Applying rules on save can also be nice, depending on the type of rule. Blocking a debug build for not following the rules is a pain in the ass though.