10

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

Thanks.

lex87
  • 1,276
  • 1
  • 15
  • 33
  • Code analysis was previously called FxCop, this answer should give you what you need http://stackoverflow.com/questions/1884522/stylecop-vs-fxcop – madd0 Jan 25 '12 at 16:19
  • Code Analysis should be compared to FxCop, not StyleCop. – nawfal Jun 05 '15 at 05:32

3 Answers3

14

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.

Erik Dietrich
  • 6,080
  • 6
  • 26
  • 37
9

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.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
ken2k
  • 48,145
  • 10
  • 116
  • 176
8

FxCop checks what is written. It works over the compiled assembly.

StyleCop checks how it is written. It works over the parsed source file, even without trying to compile it.

This leads to all the differences. For example, FxCop cannot check indentations, cause they are absent in a compiled assembly. And StyleCop cannot perform code-flow checks cause it doesn't know how your code is really being executed.

Oleg Shuruev
  • 1,339
  • 8
  • 10