![Page 1: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/1.jpg)
James Hare – Application Architect, Scottrade
![Page 2: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/2.jpg)
What are Coding Standards?A set of rules or guidelines used when writing
the source code of a computer program.Generally dictates:
Safety mandates to avoid introducing errors.Style mandates to increase maintainability.Security mandates to avoid vulnerabilities.Efficiency mandates to help increase
performance.Standards may be enforced through code
reviews or may simply be “suggestions”.
![Page 3: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/3.jpg)
Standards: Like or Dislike?
![Page 4: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/4.jpg)
But, Isn’t Programming Art?This always has been an interesting point of
contention.On one extreme development can be thought of
as a work of art and any source that reaches a logically correct result is acceptable and everything else is just “style.”
The other extreme believes that programming is purely a mechanical process and there is only a limited number of correct answers.
Which is correct?
![Page 5: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/5.jpg)
Reality Lies In BetweenIt may be more accurate to say developers
are more like artisans (crafters) than artists, though containing elements of both.An artist has a wide range of forms they can
adhere to and much is dependent on the interpretation of the viewer.
In contrast, artisans tend to construct or design for a purpose, and while there are some elements of style in construction, if it fails to achieve its purpose effectively, it is a failure.
![Page 6: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/6.jpg)
The “Art” of SortingTake sorting, for example.
Both Bubble sort and Quick sort are valid sorts on a set of data.
Bubble sort has a complexity of O(n2) and Quick sort is O(n log n).
Assuming sorting 1 million elements and each check takes 1 µs, roughly this would be: Bubble Sort: 11 days Quick Sort: 19 seconds
Both sort data, but one is clearly more useful.
![Page 7: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/7.jpg)
Standardizing an “Art”While there are many ways to solve a given
problem, there should be guidelines for effective construction.
These guidelines are similar to building codes used in building construction to ensure safety and quality.
These guidelines form the basis for coding standards and are best compiled from group consensus and industry best practices.
![Page 8: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/8.jpg)
Enforcing StandardsStandards should be enforced to promote
safety, efficiency, and maintainability.Standards can be enforced through Code
Reviews, but these tend to be applied with varying levels of adherence.
It’s much better to attempt to automate as much of your standards as possible so that the code is judged more objectively.
![Page 9: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/9.jpg)
Benefits of AutomationStandards are applied objectively since only
analyzes the source or assembly.Just plain faster than trying to catch
standards violations manually.Code authors don’t feel personally attacked.Frees more reviewer time since won’t have to
waste as much time in code reviews.Frees more time for developers since code
spends less time and iterations in review.
![Page 10: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/10.jpg)
Standards Automation ToolsThere are two primary tools from Microsoft:
StyleCop – Analyzes source files to determine if source code is correctly formatted.
FxCop (Static Code Analysis)– Analyzes assemblies to determine if code is constructed safely and optimally.
These tools overlap in some of their base rules but both have their strengths.
Other third party and Microsoft tools exist, but beyond this presentation’s scope.
![Page 11: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/11.jpg)
StyleCopAnalyzes source files and not compiled code.Great for checking elements such as:
SpacingCommentsFile compositionNaming
Cannot easily check type hierarchies or program structure.
Available at http://stylecop.codeplex.com/
![Page 12: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/12.jpg)
Configuring StyleCopIf you have StyleCop installed, you can have
Settings.StyleCop files for each project if you want to vary styles per project.
Will take the first Settings.StyleCop file it finds from working directory on up the path.
Default will be the Settings.StyleCop file in c:\program files\Microsoft StyleCop…
Various configurations can make harder to enforce uniform rules, though, so use with caution.
![Page 13: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/13.jpg)
Configuring StyleCopYou can configure which base rules you want
active by using StyleCopSettingsEditor.exe.
Let’s take a minute to look at the rules…
![Page 14: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/14.jpg)
Configuring StyleCopYou can also get to StyleCop settings in Visual
Studio directly by right-clicking a project.
This creates local copy of rules, use cautiously.
![Page 15: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/15.jpg)
Running StyleCopYou can run StyleCop from VS or MSBuild.
Has no native command-line interface, but one exists at sourceforge called StyleCopCmd.
![Page 16: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/16.jpg)
StyleCop ResultsShows in Error List window, can turn on
“Warnings as Errors” in VS if you want to break builds on violations.
![Page 17: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/17.jpg)
Suppressing a RuleMost rules are good all the time, sometimes
not.
![Page 18: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/18.jpg)
On Suppressing RulesIt’s better to keep a rule even if it only
applies 95% of the time and force developers to suppress the rule for the one-off exceptions.
This puts a SuppressMessage attribute in code which must be justified and prevents viewing the exception to the rule as a precedent for ignoring the rule.
If code reviewer disagrees, can be debated.Turning off rules should be avoided unless
the rule is invalid most or all of the time.
![Page 19: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/19.jpg)
Custom StyleCop RulesStyleCop rules are fairly easy to write.Create class library that references the
StyleCop assemblies:Located in c:\program files\Microsoft
StyleCop… Microsoft.StyleCop.dll Microsoft.StyleCop.Csharp.dll
Add a CS (C# source file) for new analyzer.Add an XML file for rule configuration.
![Page 20: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/20.jpg)
Custom StyleCop RulesIn the CS file, create an analyzer that inherits
from SourceAnalyzer and has class attribute also named SourceAnalyzer for C# files.s
![Page 21: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/21.jpg)
Custom StyleCop RulesIn the CS file, override AnalyzeDocument
and perform your checks.
![Page 22: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/22.jpg)
Custom StyleCop RulesWhen you see your violation, call the method
AddViolation and give it a rule name and args:
![Page 23: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/23.jpg)
Custom Style Cop RulesThen, in the XML file, define the rule and
message. Make sure XML file has same name as class name and is Embedded Resource.
![Page 24: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/24.jpg)
Custom StyleCop RulesThen, build the custom assembly.Place custom assembly in:
C:\Program Files\Microsoft StyleCop …You should now see custom rules in the
StyleCopSettingsEditor.If you don’t see custom rules, check that the
XML file:Is an embedded resourceHas same filename as the class name (minus
extensions)Let’s look at the code more closely…
![Page 25: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/25.jpg)
StyleCop for ReSharperJetBrains’s ReSharper is an Visual Studio IDE
plug-in that adds a lot of refactoring and aids.StyleCop for ReSharper is a ReSharper plug-
in that allows for dynamic checking of StyleCop rules as you type.
Will highlight rule violations with squiggle just like other ReSharper hints.
http://stylecopforresharper.codeplex.com/ Let’s look at how this appears in the IDE.
![Page 26: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/26.jpg)
FxCop (aka VS Code Analysis)Great for checking elements such as:
Non-spacing style issues (naming, etc).Code safety and performance issuesType hierarchy issuesAnalysis of database objects
Cannot check source style such as spacing.Already baked into Visual Studio 2008/10.Can also be used as a stand-alone.
![Page 27: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/27.jpg)
Running FxCop Stand-AloneStart Programs Microsoft FxCopCreate new project, add targets, and Analyze!
![Page 28: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/28.jpg)
Running FxCop From Visual StudioRight click on project or solution and choose
Run Code Analysis:
Let’s look at an example analysis.
![Page 29: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/29.jpg)
Suppressing FxCop ErrorsJust like in StyleCop, you can suppress one-
off exceptions to the rules.
Can insert manually or automatically from the error list in Visual Studio.
![Page 30: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/30.jpg)
Custom FxCop RulesCreate a Class Library in Visual Studio.Add references to FxCop assemblies:
From C:\Program Files\Microsoft FxCop… FxCopCommon.dll FxCopSdk.dll Microsoft.Cci.dll Microsoft.VisualStudio.CodeAnalysis
Add a CS file for the new rule.Add an XML file for the rule definition.
![Page 31: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/31.jpg)
Custom FxCop RulesIn CS file create class that inherits from
BaseIntrospectionRule:
![Page 32: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/32.jpg)
Custom FxCop RulesIn CS File, override Check to check rule.
![Page 33: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/33.jpg)
Custom FxCop RuleXML file is Embedded and contains rule detail:
Remember filename must be same as passed to base constructor of BaseIntrospectionRule.
![Page 34: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/34.jpg)
Custom FxCop RulesTo use custom rule, use CTRL+R or Project
Add Rules in FxCop.You can verify by clicking on rules tab:
![Page 35: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/35.jpg)
SummaryAutomating code standards can be very
useful for getting rid of a lot of the “noise” in code reviews and allowing reviewers to concentrate on logic bugs.
Automated code standards take the personal side out of enforcing style, safety, and performance.
Custom rules can be used in FxCop and StyleCop to allow for your own rules.
![Page 36: Automating C# Coding Standards using StyleCop and FxCop](https://reader036.vdocuments.us/reader036/viewer/2022081417/554ebe4db4c905de468b4972/html5/thumbnails/36.jpg)
Questions?Blog:
http://www.BlackRabbitCoder.net
Email:[email protected]
Twitter:http://twitter.com/BlkRabbitCoder