Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lines-of-code counting for many C# solutions

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.

like image 313
Eric Avatar asked May 05 '10 12:05

Eric


People also ask

How do you count lines of code?

The most direct way to count lines of code (LOC) is to, well, count lines of code assuming each line corresponds to a line feed sequence ( \n or \r\n ). Our IDE tells us how many lines of text a file has and displays a count in one of the margins.

How many lines of code is too much?

My advice is to avoid having many statements (or lines of code) in a single method or function. I would recommend having less than a thousand lines of code in each of them (and usually, just a hundred or two).

How many lines of code does a programmer write per day?

The mythical book, Mythical man month quotes that no matter the programming language chosen, a professional developer will write on average 10 lines of code (LoC) day.

What does the line of code counter counter +1 do?

count = count + 1; By running this line once each time you spot an object that you want to count, you can keep a running tally in your program, always stored in the same variable.


2 Answers

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

like image 50
Mitch Wheat Avatar answered Sep 27 '22 20:09

Mitch Wheat


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.

like image 24
V.D.S. Avatar answered Sep 27 '22 18:09

V.D.S.