Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which are the differences between StyleCop and Code Analysis when talking about the rules of each?

Tags:

c#

.net

stylecop

Could you please tell me which are the differences between rules of StyleCop and Code Analysis ? Should it be used together or not ?

Thanks.

like image 730
lex87 Avatar asked Jan 25 '12 16:01

lex87


People also ask

What is StyleCop used for?

StyleCop is an open-source static code analysis tool from Microsoft that checks C# code for conformance to StyleCop's recommended coding styles and a subset of Microsoft's . NET Framework Design Guidelines.

What is FxCop and StyleCop?

StyleCop is a source code analysis tool that provides developers with an effective way to follow C# coding standards. FxCop runs against the compiled binaries as a way to understand and enforce the.NET Framework Guidelines for managed code assemblies.

How do you change rules on StyleCop?

In your StyleCop install, there's a Settings. StyleCop file. You can edit this to turn off rules globally. Drag that file onto the Settings Editor executable in that file to edit it.

Is StyleCop still used?

StyleCop used to be a Visual Studio plugin and a NuGet package. You can still use this in Visual Studio 2019, but the current recommended way to use StyleCop is to use the Roslyn-based analyzers.


2 Answers

Style cop essentially parses the file looking for formatting issues and other things that you could think of as "cosmetic". Code analysis actually builds your code and inspects the compiled runtime IL for characteristics about how it behaves when it runs and flag potential runtime problems.

So, they are complimentary, and you are perfectly fine to use them together.

like image 198
Erik Dietrich Avatar answered Oct 18 '22 22:10

Erik Dietrich


Short answer:

  • stylecop: takes your source code as input and checks for potential code style issues. For instance: using directives are not alphabetically ordered...etc.
  • fxcop (now code analysis): takes a compiled assembly as input and checks for potential issues related to the executable/dll itself when it'll be executed. For instance: in your class you have a member of type IDisposable that is not disposed properly.

However, there are some rules that are common to both tools, for instance rules related to naming convention for public exposed types.

Anyway, using both is a good idea.

like image 35
ken2k Avatar answered Oct 18 '22 20:10

ken2k