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.
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.
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).
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.
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.
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
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.
Unfortunately, it seems that development of LOCCode is over.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With