# Code Metrics: Measuring LoC in .NET applications

My previous posting gave quick overview of code metric called Lines of Code (LoC). In this posting I will introduce you how to measure LoC in Visual Studio projects using Visual Studio Code Analysis and NDepend.

### LoC difference in Visual Studio and NDepend

LoC in .NET projects can be measured by Visual Studio and NDepend. Visual Studio and NDepend give different results. How they define their LoC-s?

• Visual Studio – Indicates the approximate number of lines in the code. The count is based on the IL (Intermediate Language) code and is therefore not the exact number of lines in the source code file. The calculation excludes white space, comments, braces and the declarations of members, types and namespaces (taken from MSDN library article Code Metrics Overview).
• NDepend – NDepend computes this metric directly from the info provided in PDB files. The LOC for a method is equals to the number of sequence point found for this method in the PDB file. A sequence point is used to mark a spot in the IL code that corresponds to a specific location in the original source (taken from NDepend metrics definiton page). To get better idea about sequence point read Rick Byers blog postingDebuggingModes.IgnoreSymbolStoreSequencePoints.

As Visual Studio and NDepend measure LoC differently they also get different results. Visual Studio measures count of IL instructions while NDepend measures sequence points. So basically Visual Studio gives you greater result than NDepend because on the level of IL instructions it is not possible to know if it was some executable unit of code or was it generated automatically.

### Example project

I have simple project to test how Visual Studio and NDepend calculate LoC. My project contains one class. There are only couple of lines of code but it is enough to demonstrate differences.

public class Person{    public int Id { get; set; }    public string FirstName { get; set; }    public string LastName { get; set; }     public string FullName    {        get        {            return FirstName + " " + LastName;        }    }}

Before digging deeper I will tell you the results. Visual Studio LoC is 9 and NDepend LoC is 1. This difference looks huge but you will see that it depends directly on different measuring methods.

### Visual Studio LoC

When I run Visual Studio code analysis I get the following table.

Why are automatic properties counted as lines of code? Well… take a look at my blog postingIs Automatic Property Same as Property? This posting shows you clearly how automatic properties work. Behind the curtains they are compiled as usual properties that use attribute to hold property value. FullName uses string concatenation and return – this is why it is handled as two lines of code.

### NDepend LoC

The results table generated by NDepend for Person class is shown on right. You can see that the number of LoC is 1. This is because there is one sequence point generated and it is for FullName property.

NDepend tells us also little bit more. If you look at the results window you can see that methods count for this class is 8 and fields count is 3. It is possible to get results similar to Visual Studio but you have to keep in mind that Visual Studio gives you approximate number, not exact one.

If you have a lot of automatic properties in your classes then the number of methods may be interesting when making first estimates (read my previous entry and warnings about using LoC as estimation method).

NDepend lets you also query analysis results. It has special query language called CQL that is similar to SQL. By example, this query returns all methods that have LoC greater than 15.

SELECT METHODS WHERE NbLinesOfCode > 15

We can also write query for über-bloat methods.

SELECT METHODS WHERE NbLinesOfCode > 100

As you can see syntax is very simple and after playing with queries for a while you are able to write them on the fly. You just have to know what code metrics are and how they are measured.

### Conclusion

Measuring LoC with Visual Studio Code Analysis and NDpened are both very simple tasks to do. Visual Studio gives you a different results than NDepend due to different measuring method. If you need complete analysis of your code then NDepend is better way to go because it gives you lot more information than Visual Studio does. NDepend lets you also write queries based on LoC and it makes much easier to analyze your code.

### One thought on “Code Metrics: Measuring LoC in .NET applications”

• […] my previous posting about code metrics I introduced how to measure LoC (Lines of Code) in .NET applications. Now let’s take a step further and let’s take a look how to measure compiled code. This way we […]