Use StyleCop. Analyzers for code review and how to use stylecop
Why is code review required?
StyleCop. Analyzers Introduction
There are many methods of code review, which are divided into manual review and tool review. Here we only talk about tool review.
StyleCop is a Microsoft code review tool that can be used to check various types of static programming specification errors in the Code, from code comments, code layout, maintainability, and naming conventions, readability and other parties should check the code standardization.
It is important that its rules can be customized to block rules that are not applicable to specific projects, or even custom development rules that apply to their respective projects. There are three ways to use them:
Due to technical reasons, parsing the new C # syntax becomes more and more difficult. Therefore, StyleCop will only perform stability maintenance functions without major upgrades.
If you use VS2015 and C #6 or above, we recommend using StyleCop. Analyzers, Which is rewritten based on the Roslyn compiling platform.
Use StyleCop. Analyzers
This document uses the ASP. NET Core project as an example. Other projects are similar.
The simplest way to use StyleCop. Analyzers is to add a NuGet Package. You can Install StyleCop. Analyzers directly in VS or run the Install-Package StyleCop. Analyzers command to Install
After the configuration is completed, you need to configure it. There are two configuration files
The following is an example of a rule set file:
<?xml version="1.0" encoding="utf-8"?><RuleSet Name="Rules for StyleCop.Analyzers" Description="Code analysis rules for StyleCop.Analyzers.csproj." ToolsVersion="14.0"> <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.CSharp.NamingRules"> <Rule Id="SA1300" Action="Error" /> </Rules></RuleSet>
Where SA1300 represents the rule ID, you can find all the rule IDs built in StyleCop. Analyzers here: https://github.com/DotNetAnalyzers/StyleCopAnalyzers/tree/master/documentation
Action = "Error" indicates that the compiler produces an Error when this rule is met. The effect is as follows:
Action can be set to None | Error | Warning | Info | Hidden
For more information about rule set definitions, see: https://msdn.microsoft.com/zh-cn/library/dd264996.aspx
The following is an example of a stylecop. json file:
{ "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", "settings": { "documentationRules": { "documentInternalElements": false, "documentExposedElements": false } }}
Among them, documentInternalElements indicates whether to write comments for interfaces or classes with internal visibility. The default value is true. Here we change it to false.
For more information, see: https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/Configuration.md
Next, you need to associate the Rule Set and stylecop. json files with the project, and add additionalArguments to buildOptions of the project. json file, for example:
"buildOptions": { "emitEntryPoint": true, "xmlDoc": true, "additionalArguments": [ "/ruleset:../../StyleCop/rules.ruleset", "/additionalfile:../../StyleCop/stylecop.json" ] }
If the built-in rules cannot meet your needs, you can also customize the audit rules and configure them in the rule set. Here, we will bury a hole in the next article and discuss it in detail.