Lines-of-code counting for many C# solutions
Asked Answered
S

5

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.

Stopple answered 5/5, 2010 at 12:16 Comment(6)
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 ;-) )Stopple
“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill GatesRoll
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 :-)Stopple
You could exclude auto generated files based on either partial filename or location, but you couldn't necessarily identify ALL autogenerated code.Gaut
The exclusion of auto-generated code is only a plus not a real requirement. But I was specifically asked to exclude comments.Stopple
Try GNU find and wc. ;)Melody
P
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

Propane answered 5/5, 2010 at 12:19 Comment(6)
Unfortunatly this counts all text lines, I am more interested in real code lines, excluding comments for example and maybe even auto-generated code.Stopple
@Stopple - your productivity is going to go way down if you don't count comments and auto-generated code. :-)Maryrosemarys
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?Liebowitz
@nc97217 sure I could come up with my own implementation, just hoped this has already been doneStopple
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.Stopple
@Eric: that's true. But it does have many useful metrics; much more useful than simple line countsPropane
P
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.

Plasma answered 27/1, 2013 at 4:46 Comment(2)
too bad it counts XMLs >.<Ashore
Link is dead and Wayback Machine can't help. :(Ecumenicalism
A
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) ?

Austin answered 6/5, 2010 at 8:17 Comment(0)
S
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.

Symon answered 24/2, 2014 at 15:18 Comment(0)
S
-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

Stopple answered 12/5, 2010 at 7:48 Comment(1)
Why did you down vote this answer? Should I have done something differently?Stopple

© 2022 - 2024 — McMap. All rights reserved.