I’m not saying that lines of code is an appropriate way of measuring how much work someone’s done, but it can be nice to see just how much work you’ve produced when you’ve had your head down coding for a while. I needed to find out how many lines of code there were in my Visual Studio solution including both the main project, and the unit test project. Note that the solution described in the post is available from Visual Studio 2019 version 16.4 and Microsoft.CodeAnalysis.Metrics (2.9.5).
How to find how many lines of code in Visual Studio project
To count the lines of code in Visual Studio 2019 you need to :
- Right click on the Solution
- Click on the Analyze and Code Cleanup menu option
- Click on the Calculate Code Metrics menu option
Seeing the lines of code counts
Once you’ve followed the steps described above you should see a panel appear which will contain the line counts not only for the overall solution but for each project in the solution too.
More about Code Metrics
Now you’ve seen how easy it is see the, ‘lines of code’ counts it’s worth digging a bit deeper into the Code Metrics functionality that Visual Studio offers. The metrics available are:
- Maintainability Index – the relative ease of maintaining the code, a high value is good.
- Cyclomatic Complexity – the structural complexity of the code.
- Depth of Inheritance – indicates the number of classes that inherit from one another, a low value is good.
- Class Coupling – a high coupling indicates a design that is difficult to reuse and maintain because of its many interdependencies on other types.
- Lines of Source Code – this is the literal count of lines, including blank lines.
- Lines of Executable Code – this is the count of real lines of executable code.
Show the line numbers in Visual Studio
To show the line numbers in Visual Studio follow these steps.
- Click the Tools menu
- Click the Options button
- In the left hand pane select All languages, or a specific language. In the right hand pane check the line numbers checkbox