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.