11

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

Thanks.

2

3 Answers 3

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.

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.