Chapter 8. Analyzing the code


This chapter covers

  • Using FxCop and StyleCop for static code analysis
  • Integrating FxCop and StyleCop into the CI process
  • Extending the CI process with custom rules
  • Using NDepend

Have you ever taken over a project to fix a bug or code a feature? In such a situation, did you feel uncertain, lost, or upset? How many times have you wanted to find the person who originally wrote the piece of code you’re working on and simply ... thank them warmly? If you’re living in the same world we are, this has happened to you at least a few times (see figure 8.1).

Figure 8.1. Always code as if the person who will maintain your code is a violent psychopath who knows where you live (picture courtesy of

Did you ever wonder why you felt like this? Was it because the code didn’t do what it was supposed to do? If it wasn’t a bug, it was probably something else: the software smelled. And nothing is worse than smelly code. In this chapter, we’ll discuss what you can do about smelly code and how to enforce clean code using CI. We’ll look at a few tools that make this possible: FxCop, StyleCop, NDepend, and a TeamCity tool that checks for code duplication.

8.1. Analyzing object code with FxCop

8.2. Analyzing C# with StyleCop

8.3. Custom FxCop and StyleCop rules

8.4. Extending code analysis

8.5. Summary