14

Probably, the basic situation is familiar to everyone. You have agreed on certain coding standards in your team and now it is time to make sure that everybody follows them.

Some do it via heavy paired programming, others maintain a list of coding standards and do a manual review. Others even use the static code analysis tool from Visual Studio to make sure that coding standards are enforced.

What are your best practices to ensure coding standards? Which tools do you use - are there some essential tools every dev should know? How do you integrate the standardisation of your code in your development tools?

0

5 Answers 5

15

StyleCop (now also StyleCop for Resharper) is commonly used to enforce coding style rules within C# code bases.

It can be integrated into your CI build and cause build failures if any violations have been introduced.

5
  • Any more info on how to use it? I would like to run style checking before each commit. So I need cli for it. Is there any. I could not find any relevant info. Nov 23, 2018 at 13:05
  • 1
    This might help blog.submain.com/stylecop-detailed-guide
    – z-boss
    Feb 26, 2019 at 14:46
  • @R. - if only one could customize StyleCop rules. Oh, wait...
    – Oded
    Jul 17, 2019 at 16:39
  • @R. - you can also write your own rules.
    – Oded
    Jul 17, 2019 at 16:42
  • FWIW, from the readme on github: "While pull requests will continue to be accepted, it is unlikely that any major development will be done on this project. It is increasingly difficult and inefficient to maintain the custom C# parser used by StyleCop."
    – rusty
    Feb 20, 2020 at 21:23
7

First, make sure you have a proper build server that continously builds your code. TFS or Jenkins/Hudson+Msbuild are good options. On this build server you of course run tests, and you can also run static code analysis as well as syntax checkers.

I recommend Using FxCop or Gendarme for static code analysis. Both can be used side by side.

One important key to success: don't deploy them with a massive ruleset and expect it to work well. You will have millions of warnings and noone will care.

Rules of thumb are:

  • Always have zero warnings. Reduce the set of checks initially if you have to, then add some new checks every sprint and fix those. Make sure every check performed has actual value to you and is consistent with your standards.

  • If you have domain specific rules you need to adhere to, don't be afraid to implement your own rules.

For syntax check, use StyleCop or StyleCop for Resharper if you are using Resharper (which I highly recommend as well).

2

StyleCop and FxCop are my light weight tools that remind code styles to my developers. Resharper is also good one, but it makes VS very slow.

I recommend FxCop because of its' rich rules library, with great documentation, classifications, examples and many other features.

There's also StyleCop Fixer extension for visual studio available, helps to fix ordinary mistakes.

Code Metrics is also affected sometimes by code styles. You can monitor maintainability of codes with this tool.

2

I would like to emphasise getting the automatic code analysis already mentioned (FxCop, StyleCopy, CI builds etc) working first, in preference to manual reviews. The automatic checks will pick up far more things than a manual review is likely to, and is normally much more rigorous, consistent and is self documenting. Manual reviews can be good for more high level design decisions etc, but in my experience they can be very subjective and haphazard.

0

NDepend can be used to enforce various coding standards on a .NET Code Base. It has around 200 default code rules that can be browsed here. Also it is easy to customize existing rules or create your own rules since with NDepend a rule is just a C# LINQ query.

Disclaimer: I work for NDepend

Not the answer you're looking for? Browse other questions tagged or ask your own question.