14

I am currently researching a solution for counting lines of code in C#.

I pretty much need a combination of the following two tools:
http://richnewman.wordpress.com/2007/07/01/c-and-vbnet-line-count-utility/
http://www.locmetrics.com/index.html

My problem is that I need to recursively scan a folder containing a lot of visual studio solutions. So can't really use the first tool without any major work on its code, as it's only able to scan a single solution at a time.
But I also need to split the results for each solution, preferably even the contained projects. This disqualifies the second tool I found. I also found NDepend which suffers from the same problem.

Do you know of any free tools that do what I need? I am unable to find anything suitable.

Kobi
  • 135,331
  • 41
  • 252
  • 292
Eric
  • 151
  • 1
  • 1
  • 4
  • Its also important that the tool is able to exclude comments. Maybe even auto-generated code. Sorry forgot (can edit the original question, as Kobi already repaired the hyperlinks ;-) ) – Eric May 05 '10 at 12:25
  • 10
    “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates – Oliver May 05 '10 at 12:30
  • 1
    I am not sure what these numbers are going to be used for. At the moment they are just a onetime snapshot. Probably to get a sense of the size of all projects. I am just an intern asked to find out :-) – Eric May 05 '10 at 12:36
  • 1
    You could exclude auto generated files based on either partial filename or location, but you couldn't necessarily identify ALL autogenerated code. – slugster May 05 '10 at 12:36
  • The exclusion of auto-generated code is only a plus not a real requirement. But I was specifically asked to exclude comments. – Eric May 05 '10 at 12:37
  • Try GNU `find` and `wc`. ;) – adamdunson Jan 27 '13 at 05:01

5 Answers5

22

NDepend is a great tool designed for measuring and visualising code metrics and complexity.

Powershell would do it:

(dir -Include *.cs -Recurse | select-string .).Count

Counting Lines of Source Code in PowerShell:

Line count per path:

   gci . *.cs -Recurse | select-string . | Group Path

Min / Max / Averages:

   gci . *.cs -Recurse | select-string . | Group Filename | Measure-Object Count -Min -Max -Average

Comment ratio:

   $items = gci . *.cs -rec; ($items | select-string "//").Count / ($items | select-string .).Count


## Count the number of lines in all C# files in (and below) 
## the current directory. 

function CountLines($directory) 
{ 
    $pattern = "*.cs" 
    $directories = [System.IO.Directory]::GetDirectories($directory) 
    $files = [System.IO.Directory]::GetFiles($directory, $pattern) 

    $lineCount = 0 

    foreach($file in $files) 
    { 
        $lineCount += [System.IO.File]::ReadAllText($file).Split("`n").Count 
    } 

    foreach($subdirectory in $directories) 
    { 
        $lineCount += CountLines $subdirectory 
    } 

    $lineCount 
} 

CountLines (Get-Location) 

Also, Line Counter

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • Unfortunatly this counts all text lines, I am more interested in real code lines, excluding comments for example and maybe even auto-generated code. – Eric May 05 '10 at 12:22
  • 4
    @Eric - your productivity is going to go way down if you don't count comments and auto-generated code. :-) – tvanfosson May 05 '10 at 12:25
  • 1
    Couldn't you use a combination of this method to find all the .sln files, then feed each of them into the first tool you mentioned? – Niall C. May 05 '10 at 12:32
  • 1
    @nc97217 sure I could come up with my own implementation, just hoped this has already been done – Eric May 05 '10 at 12:34
  • As far as I can tell NDepend also only works on a single solution/assembly at a time. But I need to scan the complete repository at once. – Eric May 05 '10 at 13:08
  • @Eric: that's true. But it does have many useful metrics; much more useful than simple line counts – Mitch Wheat May 05 '10 at 14:23
3

I think LOCcode is an interesting free tool for counting number of Lines Of Code. It allows to choose which of files must be processed. It counts LOC in all enabled tasks.

enter image description here

Unfortunately, it seems that development of LOCCode is over.

V.D.S.
  • 103
  • 6
2

What you need is logical lines of code counting as defined here: How do you count your number of Lines Of Code (LOC)

If you use NDepend to count your number of lines of code you can still append all your VS sln in a NDepend project. However logical lines of code is a metric inferred from PDB files so make sure that all your assemblies have corresponding PDB files associated.

Also you might be interested by:Why is it useful to count the number of Lines Of Code (LOC) ?

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
1

I liked what Mitch Wheat has said but I don't like some useless information is calculated as 'line of code'. I've written a C# code to find total number of REAL lines in the code here: http://rajputyh.blogspot.in/2014/02/counting-number-of-real-lines-in-your-c.html

You need to build a small utility out of that code to provide path of your root folder where all "*.cs" files are kept. Good thing about that code is that it is not dependent upon the project file. I generally check-out my code and delete auto generated files and the use the tool to count number of lines.

Yogee
  • 1,412
  • 14
  • 22
-3

In the end I went with LocMetrics, unfortunately this didn't really solve my per-solution problem.

But the folder structure of the reposiotry maps well enough to solutions so I was decided to use the tool above.

Thanks everybody for helping

Eric
  • 151
  • 1
  • 1
  • 4