If you are programming in .Net best choice is to follow .Net coding standard which MS uses internally. This coding standard is documented in a book Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries and later also on MSDN. Event better news is there are 2 tools that implement this standard:
- StyleCop - checks source code files
- Code Analysis (ex FxCop, now integrated in VS) - checks compiled code
Below are detailed steps on how to configure these tools so they run on solution build.
Solution StyleCop configuration
In order to define Style Cop for project first solution must have StyleCop rule settings and StyleCop executable files.
Below are the rules I disable from default rule set in StyleCop:
- Documentation rules
- SA 1600 – Elements must be documented
- SA 1633 – File must have header
- SA 1634 – File header must show copyright
- SA 1635 – File header must have copyright text
- SA 1637 – File header must contain file name
- SA 1638 – File Header file name documentation must match file name
- SA 1640 – File header must have valid Company text
- Ordering rules
- SA 1200 - Using directives must be placed within namespace
- Spacing rules
- SA 1027 – Tabs must not be used
Solution Code Analysis configuration
Add Code analysis rule set and dictionary to solution root.
I use recommended rule set is “Microsoft All Rules” without rules:
- CA2210 Assemblies should have valid strong names (Design)
- CA1062: Validate arguments of public methods (Design)
- CA1303: Do not pass literals as localized parameters (Globalization)
- CA2233: Operations should not overflow (Usage)
- CA2204: Literals should be spelled correctly (Naming) – useful but it doesn’t work correctly.
See more about Code Analysis Dictionary.
Project manual configuration
To integrate Code Analysis Dictionary and StyleCop in build, unload and edit project and add following tags. Note that paths might be different depending on Solution configuration.
Hide Copy Code
<ItemGroup>
<CodeAnalysisDictionary Include="$(SolutionDir)\CodeAnalysisDictionary.xml" />
</ItemGroup>
<Import Project="$(SolutionDir)\ExternalDlls\StyleCop 4.7\StyleCop.targets" />
Project build configuration
Configure compiler warning level on Project properties to 4 (most strict).
Check XML documentation files (unless it’s a test project and not documentation is needed because tests are self-documented).
Project Code Analysis configuration
Configure project Debug configuration to use Code Analysis rules in Solution root. Do the same for the Release configuration. Only difference is that in debug mode Code Analysis should not be run because it slows down the build. We keep CA running in Release to get error report from continues integration and to allow easily turning CA on by altering solution mode from Debug to Release.
No comments:
Post a Comment