Last Updated: February 25, 2016
· carleeto

How to keep your code simple on Visual Studio

So, the problem is simple - you want the quality of your code base to increase over time.

Yeah, the solution is to measure code complexity before it gets to the code base. But the question is, "measure when?" After you commit? Too late. its already in your repository. Before you commit? Now you're getting warmer. Yes, but when? After the build has succeeded as part of a commit hook? Yeah, that's good, but you can do better, particularly when you're coding in a language that takes long to build. For a language like C++, that's way to much lag in the feedback loop.

How about when you're writing the code itself? Wo...but wait a minute. How do you know if the code's gotten worse or better as you're modifying it? Well, let's use cyclomatic complexity as a metric. It seems to be the industry standard for measuring maintainability. So the question becomes, how do you get Visual Studio to display metrics while you're editing the source file?

Simple..install Code Maid and once you're in Visual Studio, turn on the "Spade" panel (CodeMaid > Spade). This is a view that will show you the cyclomatic complexity of the file you're working on in real time, broken down by methods. It may take a little while to initialize, just be patient. Once it up and running, just follow basic cyclomatic complexity rules to interpret the number - below 3 is acceptable, above 9 is a red flag and between 5 and 9 needs to be worked on.

The key thing is that now, you can see the complexity of your code as you write it. Which means, being a software engineer who wants to do it right, you will try to write better code immediately.