Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?

Tags:

By redundant stuff, I mean the namespaces, as I know they are necessary but if there are 10k of them, it doesn't add valuable info to the table.

Could this be done using Linq?

like image 547
Joan Venge Avatar asked May 10 '11 20:05

Joan Venge


People also ask

How do you count the number of lines of code?

To use cloc simply type cloc followed by the file or directory which you wish to examine. Now lets run cloc on it. As you can see it counted the number of files, blank lines, comments and lines of code. Another cool feature of cloc is that can even be used on compressed files.

How many lines of code is C?

There are 11 lines of executable code in my C program. So that works out to be 15.2 instructions per line of C for the for the PIC18F; 24.7 instructions for the PIC16F; 9 instructions for the 8051; and 6.2 instructions For the PIC32.

How do I count the number of lines in C#?

Method 4: Using IndexOf() Method We can also count the number of lines present in the given string using the IndexOf() method.

How can I count all the lines of code in a directory recursively?

The SLOCCount tool may help as well. It will give an accurate source lines of code count for whatever hierarchy you point it at, as well as some additional stats. This will print more than one number when there are many files (because wc will be run multiple times. Also doesn't handle many special file names.


2 Answers

No need to reinvent the wheel. Take a look at the Visual Studio Code Metrics PowerTool 11.0

Overview

The Code Metrics PowerTool is a command line utility that calculates code metrics for your managed code and saves them to an XML file. This tool enables teams to collect and report code metrics as part of their build process. The code metrics calculated are:

• Maintainability Index

• Cyclomatic Complexity

• Depth of Inheritance

• Class Coupling

• Lines Of Code (LOC)

I know you said you don't have Ultimate, so I just wanted to show you what you're missing.

Code Metrics in VS 2010 Ultimate

For everyone else, there's SourceMonitor Source Monitor

like image 82
grenade Avatar answered Oct 23 '22 22:10

grenade


Visual studio will do this for you. Right click on your project and choose Calculate Code Metrics.

like image 25
Kyle Trauberman Avatar answered Oct 23 '22 20:10

Kyle Trauberman