Patrick Smacchia [MVP C#]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • NDepend v3 is fully integrated in Visual Studio, and is now available for download! Software dependencies visualization, 82 .NET software metrics, continuous rule validations, assembly version diff, declarative code queries and more ! http://ndepend.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
How do you count your number of Lines Of Code (LOC) ?

Do you count method signature declaration? Do you count lines with only bracket? Do you count several lines when a single method call is written on several lines because of a high number of parameters? Do you count ‘namespaces’ and ‘using namespace’ declaration? Do you count interface and abstract methods declaration? Do you count fields assignment when they are declared? Do you count blank line?

Depending on the coding style of each of developer and depending on the language choose (C#, VB.NET…) there can be significant difference by measuring the LOC.

Apparently measuring the LOC from parsing source files looks like a complex subject. Thanks to an astute there exists a simple way to measure exactly what is called the logical LOC. The logical LOC has 2 significant advantages over the physical LOC (the LOC that is inferred from parsing source files):

  • Coding style doesn’t interfere with logical LOC. For example the LOC won’t change because a method call is spawn on several lines because of a high number of arguments.
  • Logical LOC is independent from the language. Values obtained from assemblies written with different languages are comparable and can be summed.

In the .NET world, the logical LOC can be computed from the PDB files, the files that are used by the debugger to link the IL code with the source code. The tool NDepend computes the logical LOC for a method this way: it is equals to the number of sequence point found for a 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. More info about sequence points here. Notice that sequence points which correspond to C# braces‘{‘ and ‘}’ are not taken account.

Obviously, the LOC for a type is the sum of its methods’ LOC, the LOC for a namespace is the sum of its types’ LOC, the LOC for an assembly is the sum of its namespaces’ LOC and the LOC for an application is the sum of its assemblies LOC. Here are some observations:

  • Interfaces, abstract methods and enumerations have a LOC equals to 0. Only concrete code that is effectively executed is considered when computing LOC.
  • Namespaces, types, fields and methods declarations are not considered as line of code because they don’t have corresponding sequence points.
  • When the C# or VB.NET compiler faces an inline instance fields initialization, it generates a sequence point for each of the instance constructor (the same remark applies for inline static fields initialization and static constructor).
  • LOC computed from an anonymous method doesn’t interfere with the LOC of its outer declaring methods.
  • The overall ratio between NbILInstructions and LOC (in C# and VB.NET) is usually around 7.

Posted Wed, Oct 3 2007 11:51 AM by Patrick Smacchia

[Advertisement]

Comments

Tim B wrote re: How do you count your number of Lines Of Code (LOC) ?
on Thu, Oct 4 2007 5:47 PM

I follow this easy rule of thumb: don't count them.  And if it's required of me, I make up a reasonable number and begin looking for a new employer that is interested in code quality rather than meaningless metrics.

keith wrote re: How do you count your number of Lines Of Code (LOC) ?
on Thu, Oct 4 2007 7:12 PM

tim took my answer :[

Patrick Smacchia wrote re: How do you count your number of Lines Of Code (LOC) ?
on Fri, Oct 5 2007 1:19 AM

I don't agree, LOC is far from being meanlingless.

Yes the LOC is not directly related to productivity ans should never be used as a yardstick.

However it is a the simpler way to coarsly measure the overall endeavour put in an application and to compare them. For example the codebase I am working on is 50K LOC while the .NET framework is around 500K LOC. Then, I have a pretty clear idea of the overall size of the framework.

Also LOC is useful when you have to plan some refactoring, or some migration because it will helps estimate the delay. If your team need a month to refactor X lines, it'll take around 2 months to refactor 2X LOC.

Patrick Smacchia [MVP C#] wrote Why is it useful to count the number of Lines Of Code (LOC) ?
on Fri, Oct 5 2007 2:25 AM

My previous post explained How to count the LOC of your .NET application and Tim B and Keith answered

Jeff B wrote re: How do you count your number of Lines Of Code (LOC) ?
on Sat, Oct 6 2007 3:31 PM

Tim / Keith,

Predictable egotistical and immature responses from typical hackers who always insist that any measure of productivity (what other profession gets away with this attitude?), even if used to justify better tools, environments and processes to your benefit, would be an insult to your so-called craft (which is being commoditized every day by offshore developers).  If you acted like you knew or cared anything about business you might actually have something to contribute to society 5-10 years from now when programming is a thing of the past.

Counting LOCs, defects, story cards, hours, and other measures are the only way to tell a good developer from a bad and/or lazy one.

Cheers.

Patrick Smacchia [MVP C#] wrote Code Size is - not - the Enemy
on Tue, Dec 25 2007 2:15 PM

There has been recently some discussion about Code size is the enemy starting from (in order of publication

Code Metrics with Visual Studio 2008 « SYNDATO GmbH wrote Code Metrics with Visual Studio 2008 « SYNDATO GmbH
on Fri, May 16 2008 6:43 AM

Pingback from  Code Metrics with Visual Studio 2008 « SYNDATO GmbH

Patrick Smacchia [MVP C#] wrote Source Files Rebasing
on Thu, Aug 21 2008 1:30 PM

I would like to detail here a recent featurette we added to NDepend v2.9.1: Source files rebasing . Source

Community Blogs wrote Source Files Rebasing
on Thu, Aug 21 2008 1:48 PM

I would like to detail here a recent featurette we added to NDepend v2.9.1: Source files rebasing . Source

Source Files Rebasing - taccato! trend tracker, cool hunting, new business ideas wrote Source Files Rebasing - taccato! trend tracker, cool hunting, new business ideas
on Thu, Aug 21 2008 3:24 PM

Pingback from  Source Files Rebasing - taccato! trend tracker, cool hunting, new business ideas

Mirrored Blogs wrote Source Files Rebasing
on Fri, Sep 5 2008 12:42 AM

I would like to detail here a recent featurette we added to NDepend v2.9.1: Source files rebasing . Source

Patrick Smacchia [MVP C#] wrote NDepend Analysis Inputs
on Mon, Jan 26 2009 6:50 AM

I would like here to clarify from where NDepend analysis takes its inputs. Indeed, it might sounds mysterious

Community Blogs wrote NDepend Analysis Inputs
on Mon, Jan 26 2009 7:37 AM

I would like here to clarify from where NDepend analysis takes its inputs. Indeed, it might sounds mysterious

clint wrote re: How do you count your number of Lines Of Code (LOC) ?
on Wed, Jan 28 2009 10:55 PM

The sequence points link doesn't work.

Patrick Smacchia [MVP C#] wrote Rambling on Cyclomatic Complexity
on Tue, Oct 6 2009 10:50 AM

Normal 0 21 false false false FR X-NONE X-NONE After the number of Lines of Code , the Cyclomatic Complexity

Theo wrote re: How do you count your number of Lines Of Code (LOC) ?
on Thu, Dec 17 2009 7:15 AM

JeffB,

people like you caused the economic crisis. And people like you are the first ones to go on a crisis.

My regards.

Fred wrote re: How do you count your number of Lines Of Code (LOC) ?
on Sat, Jan 30 2010 3:47 PM

Jeff B,

If lines of code are the only way that you can tell a good developer from a bad one then you are obviously not a developer but rather just the know-nothing turd that your comments make you appear to be.

Andrew D wrote re: How do you count your number of Lines Of Code (LOC) ?
on Tue, Feb 2 2010 1:50 PM

This is foolish.

Counting lines of code is completely trivial, and really measures nothing.

Let's say that there's a requirements document that outlines some specs for software to be built. Let's also say we give the same requirements docs to two different programmers that don't know about each other, and are equally weird looking.

Let's pretend this happens:

Programmer A: Writes 2500 total lines of code in 3 files, in 5 days. 5 bugs are filed by QA. Bugs are fixed and code passes testing in 8 days total with QA's seal of approval.

Programmer B: Writes 500 total lines of code in 12 files in 8 days. 2 bugs are filed by QA. Bugs are fixed and code passes testing in 10 days total with QA's seal of approval.

Both pieces of software meet the requirements.

It's easy to see why management propagates the "more lines is more better" myth in cases like this. It looks like 2 days were saved by Programmer A!

But wait, there's more. More often than not I've seen this happen:

Programmer A wrote software that is a nightmare to maintain. It's hacked together and excruciating to debug, especially when Programmer C is tasked to maintain the code when Programmer A eventually quits to become a development manager at Microsoft.

Programmer B wrote software that is generic and extensible, reusable and well-documented. Programmer C can learn from the code, extend it, improve it, even refactor it. All because Programmer B spent an appropriate amount of time designing the software with the future in mind.

Lines of code really are irrelevant. Quality of code designed is relevant.

PeteB wrote re: How do you count your number of Lines Of Code (LOC) ?
on Wed, Feb 10 2010 12:42 PM

I have to say I am shocked (and sadened) that counting SLOC is considered a Dilbert-like activity. If I am trying to estimate effort for any activity, design, coding, testing, intergration, I need to have some idea of the scale of the thing under development. If I don;t have SLOC as ONE PART of my data set, it becomes an effort of useless guesswork. I work with many (over 200) high maturity organizations - they measure SLOC. Stop bitching you babies!

Gunnar Peipman's ASP.NET blog wrote Code Metrics: Lines of Code (LoC)
on Thu, Feb 18 2010 6:09 PM

I start writing series of blog posting about code metrics. I plan to introduce different metrics and

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us