Over the years, one of the things I’ve come to love about the .NET ecosystem is the absolute abundance of tools to help you. It’s an embarrassment of riches. I enjoy writing code in C# because the language itself is great. But C# tools take the experience to a whole other level.
I know, I know. Some of you out there might argue that you get all of this goodness only by using heavyweight, resource-intensive tooling. I’ll just go ahead and concede the point while saying that I don’t care. I’m happy to work on a powerful development rig, outfitted with powerful tools, to write code in a highly productive language.
Today, I’d like to talk about some of these C# tools. Or I should say I’d like to talk about some of the many C# tools you can use that are generally oriented toward the subject of code quality.
So, if you’re a C# developer, what are some tools you can use to improve the quality of your code?
First Among C# Tools: Visual Studio, Out of the Box
Before you roll your eyes, stop and think for a moment about everything that Visual Studio actually gives you. Compare this to, say, a text editor, or a lightweight IDE for some other stack. Granted, some of the features depend on the specific version of Visual Studio, but here’s some of the functionality that Visual Studio 2017 has to offer relating to code quality.
- Automated refactoring capabilities.
- Simple code metrics.
- Code quality analysis (formerly FXCop).
- A unit test framework and test runner.
- Unit/characterization test generation.
Imagine using a code-oriented text editor for your day-to-day development. Wouldn’t you consider each of these to be seriously powerful add-on tools that you could leverage? Visual Studio gives you this out of the box, so I list it here.
Generally speaking, it’s worth poking around this powerful IDE to look for features that can help your efforts. There are so many that you’ve probably missed one or two over the years.
Let’s now move beyond Visual Studio and take a look at something called StyleCop. If you’ve spent significant time in the .NET world, you’ve probably heard of this one. Its place among C# tools and quality trends toward what I’ll call the cosmetic.
You use StyleCop to enforce a standard set of code formatting rules. In essence, it helps you administer your coding standard.
For you code-formatting cynics out there, rest assured that I count myself among your ranks. If I’m writing code on my own, I really don’t think the formatting matters at all. But if you write code as part of a group, normalizing around your formatting matters. It helps you conform to the principle of least astonishment in terms of maintenance.
While I would argue that which standards you pick matter very little, having a common approach helps a lot with code readability (and thus quality).
Let’s now expand beyond the realm of the cosmetic with a tool that can do that and more. ReSharper (often fondly called “R#”), offers a lot of functionality aimed at helping you write good code, efficiently.
Like StyleCop, it can help you enforce a coding standard. But it does a lot of other stuff as well:
- Automated refactorings (beyond those that come with Visual Studio)
- Code analysis (beyond what comes with Visual Studio)
- Inline code issue highlighting as you type
- Code generation
And it has other features besides. ReShaper is what has generally been known as a “productivity tool” for developers, and it comes in the form of a Visual Studio plugin. Install it, and it’ll add a lot of ways to make you faster with your development while simultaneously helping with the quality of your code.
Speaking of faster, let’s look at another tool called CodeRush (emphasis on the “rush”). CodeRush and ReSharper are sort of the Coke and Pepsi of .NET productivity add-ins, and they have been for a long time.
This means that CodeRush has a similar set of features, aiming to help you develop faster and more effectively but while also highlighting code issues and automating fixes for them. Look for it to call out questionable or deprecated use of the language.
In my personal experience, CodeRush is a bit more visually oriented and optimized for customization, while ReSharper makes issue fixing and team usage a bit more turnkey. But really, you’re much better off with either one than you would be otherwise.
As I mentioned earlier, you get an automated test runner and a test framework out of the box with Visual Studio. Why mention that in a post about C# tools that contribute to code quality? Well, as you might infer, I’m a big proponent of unit testing. (In fact, it’s a subject I mention frequently on this blog).
Unit testing — and specifically, TDD — help you write better quality code. So things that help you do better with automated unit testing will help you write better code. And I can think of no tool that satisfies that description more than NCrunch. NCrunch compiles your code and executes your unit test suite in the background, as you type. It then paints the results as red and green dots, right in line with the code it covers.
The end effect? Your unit test suite runs constantly, and you get live, visual feedback right in your code as you go. Imagine this. You’re in your well tested codebase and you mischievously set a variable to null somewhere, randomly. Even as you finish typing, you see a series of dots corresponding to lines in the method turn red. You know when you’re breaking unit tests as quickly as the red squiggly tells you when your code isn’t compiling.
Last and certainly not least for C# tools to help with code quality, I’ll talk about NDepend. And I say this because NDepend is the only tool in the list devoted entirely to code quality. That’s its raison d’etre.
With all of these other tools, you have something that serves another purpose, primarily (code editing, formatting, productivity, unit testing). It’s only in the process that they help with code quality. NDepend provides you with metrics, warnings, statistics, diagrams, and trend lines all specifically aimed at keeping you informed about code quality.
And if you prefer a different definition than NDepend’s? That’s fine. Not only can you turn its warnings on and off and tweak thresholds, but you can also create your own metrics and measurements out of whole cloth. NDepend focuses entirely on quality and is insanely customizable.
I make part of my living assessing client codebases for quality and offering strategic advice. And NDepend is the most indispensable tool in my tool belt as I do this.
Weigh in Yourself!
I promise you that I forgot someone’s favorite tool. You’re probably reading this and getting indignant, thinking, “How could you possibly not mention _____?!” Well, blog posts are only supposed to be so long, and I’ve favored mentioning the tools that I use most commonly to give my own code quality a boost. This is, obviously, not an exhaustive list.
So please, by all means, add your favorite to the comments! What C# tool do you use to help with code quality? There are a lot of them out there, and people are working on new ones all the time. The more of them that we’re aware of and catalog, the better all of our collective code will be.