Understand the complexity and maintainability of your code using Code Metrics in Visual Studio– Line of Code (LoC)

By | December 14, 2015

In the previous post you have learned the basics of Code Metrics and how to get started with it. Now, we will start looking into the individual metrics in details. To start with, we will pick the Line of Code first.  Lines of Code (LoC) – Indicates the approximate number of lines in the code.  We should write our method in such a way that must have less line of code.  If the value of LoC is  high for an individual methods you may need to consider refactoring them and bringing back to below threshold value.

More lines of code indicates that the specific method may doing a lot of task and may violate the Single Responsibility principle. This may also indicates, your logic of the code is not well written or not optimized. So you must try to break it down as much as you can so that it become more reusable, more cleaner and less complex. More line of code also indicate that the unit or method or project might be hard to maintain in future.

Must Read :  Understand the complexity and maintainability of your code using Code Metrics in Visual Studio

However, sometimes it is necessary to have long methods depends on the nature of application and type of operation we are doing. In such cases we need to make sure you are re-verifying the code, and ensure no further optimization can’t be done and call out why this needs an exception of LoC rules.

Following screenshot show the code metrics results for “CodeMetricsSampleApp” applications, and a drilled down view of  “Method3()” and it’s associated Line of Code Metrics inside the “Code Metrics Results” window.


As a general guideline, a method with less than 40 lines of code is good and will be easy to maintain.  (and this may depends.. there are many recommend for  2o line.. or so.. ) ..

It is very important to understand that, the LOC Metrics is not the exact line of code we writes in C#, it is actually based upon the line number of IL (intermediate language) code.  The calculation does not includes comments, white space, line break etc.

You can open the executable in the ILDASM and verify the instruction and LOC for your line of code for the IL that is executable as per instruction. Following screenshot shows the executable and IL instruction for same Method3().



Thus Line of Code may vary based on the type of measure you are using; and you can easily classify them as follows for better understanding.

  • Logic LoC– Code that contain only executable. (Whatever we have seen so far can be considered as Logical LoC)
  • Physical LoC : Contains all the lines of code ( including comments , braces, declaration etc. )

As I said, in Visual Studio Code metrics, we always see the Logical LOC, here is another example that will clear your doubt.

Consider the below example, where you have an interface IBase, with different set of properties.  If you see the Physical Line of Code, in the Visual Studio Editor it is 9 – 14 , that is  total 6 LoC. However when we calculate the actual code metrics for the same interface the Line of Code for the same interface is 0, because there is nothing executable from this instance of interface. To be really consider it as LoC, it has to be implemented and it will consider as LoC for the implemented class.


Finally to summarize,  Lines of Code Metrics  indicates how many lines of source code you have in your application; which are nothing but the IL code rather raw C# code written in Visual Studio editor. LoC can be used to:

Measure the size of code unit / module / methods/ project

Measure the  complexity in terms of length, cleanness and single responsibility

Hope this helps !

Author: Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.