Fog Creek Software
g
Discussion Board




Silly question about counting lines of code...

Does one typically count whitespace lines and lines of comments?

One one hand, they're not code, but on the other hand they're necessarily and (usually) present, so in a way it makes sense to count them.

Just a trivial question as to what the prevailing practice is.  :D

John Rose
Monday, January 5, 2004

Not too silly... I was wondering the same thing myself a few days ago.

Also, if whitespace/comments don't count, are there any handy utilities (like a Visual Studio plugin) that will tally my bananas (the "real" lines of code)?  I imagine that coders have more important things to do than count lines one by one.

Hmm... maybe there's a job opportunity here for laid-off Florida election judges.  <g>

Robert Jacobson
Monday, January 5, 2004

one way to do it is to count semicolons if applicable.

mb
Monday, January 5, 2004

As long as the language doesn't support nested comments, it's pretty simple to use regular expressions to strip out comments and whitespace, then count the lines left.

One caveat: Look for comment symbols within quoted strings, and substitute them with something else *before* doing the regular expression parsing mentioned above.

T. Norman
Monday, January 5, 2004

I also should mention ... if you have a single function call or expression that spans multiple lines but you want to treat it as one line of code, it would be more appropriate to count line-terminating characters (semicolons, end-of-line characters, etc.) after stripping out the whitespace and comments.  Naturally, you'd also have to account for the line-continuation characters in languages like VB.

T. Norman
Monday, January 5, 2004

Stripping out comments and whitespace is pretty easy, but is it common practice?

I guess there's not really an established common practice for something like this since L.O.C. is such a vague indicator anyway.

Basically I'm just wrapping up a personal project and was curious to know how many lines of code I'd written.  It's about 12K including whitespace+comments.  So then I started wondering if I ought to have counted them, thus this thread.  Purely on a whim...

John Rose
Monday, January 5, 2004

http://www.codeproject.com/macro/linecount.asp

coresi
Monday, January 5, 2004

What is the purpose for doing the count?  In most cases, it doesn't matter as long as all the code is consistent.  For example, if you count the number of lines it takes to implement CRUD logic for a given database table, it should be a reasonable predictor of the number of lines for implementing CRUD logic on similar tables, provided you use consistent coding standards.

anon
Monday, January 5, 2004

This is a pretty well-defined issue in the research.  The usual definition of a "physical line of code", which is probable what you're counting, is a line of code ending in newline or end-of-file, and containing at least one statement termination.  In other words, it has some non-comment and non-whitespace content.

In practice, the ratio of PLOC-to-whitespace (i.e., all-whitespace lines) and PLOC-to-comment (i.e., all-comment lines) is sometimes (but not always) a decent, very rough indicator of how hard code is to read.  It's not perfect, and anyone with half a brain can come up with pathological counterexamples, but it's one tool in the toolbox.

Tim Lesher
Monday, January 5, 2004

BTW, to answer the followup question, SLOCCOUNT is a very good, crossplatform, crosslanguage tool that does PLOC counts as well as COCOMO cost estimations.

http://www.dwheeler.com/sloccount/

Tim Lesher
Monday, January 5, 2004

> What is the purpose for doing the count?

Bragging rights?  Not to say that bragging rights is a trivial thing, especially if someone on an interview asks what kinds of projects you've worked on.  There's a far cry difference, I'd think in saying, "I have a 5,000 line Visual Basic application" and "I have a 30 line C# app."

I would imagine anyway...

Andrew Burton
Monday, January 5, 2004

I created a line numbering program for visual basic out of necessity.  It numbers the lines of code so I can use the line number to find errors.  At any rate, whitespace, labels, comments, general declarations and various other language entities generate compile time errors if they are numbered in VB.  It's a good way to tell how many actual lines of code I have written.  The first version of the program, counted every line no matter what.  I keep this version around to compare the number of lines in the files to the actual number of "numbered lines".  The difference is around half.  So if I had a 20,000 line VB program there would be approximately 10,000 extra lines.  I don't know if this is universal or just attributed to my coding style.

Dave B.
Monday, January 5, 2004

I should say, If I had 20,000 numbered lines of code there would be 10,000 extra lines, so 30,000 total counting all the lines in the file.  So take half of the numbered lines.

Dave B.
Monday, January 5, 2004

http://www.wndtabs.com/plc/

Project Line Counter for Visual Studio.NET

Rob Walker
Monday, January 5, 2004

The purpose of counting the lines of code? Mostly out of curiosity.  Now that the project is nearing completion I found myself wondering how much code I'd actually written. 

The question about whitespace/comment counting arose from curiosity and comparative purposes.  Occaisionally you hear people refer to the size of a project based on LOC count. 

Now, if only I could find a tool which counts the lines of GOOD code. :P

John Rose
Monday, January 5, 2004

What programming language did you use?


Monday, January 5, 2004

As some others have said, the answer depends on what you want the count for.  You'd want to see separate counts of everything to get the most complete view.  In terms of representing code complexity, the popular method seems to be to count things like control statements.  I'd guess that typically when someone quotes a line count without further qualification, they mean all lines. 

SomeBody
Monday, January 5, 2004

For what it's worth, the code complete guy doesn't include whitespace or comments.

Jason McCullough
Monday, January 5, 2004

Actually, I wrote my program in Whitespace ( http://compsoc.dur.ac.uk/whitespace/ ).  Depending on the answer to this burning question, maybe I didn't write any lines of code at ALL!  :-(

John Rose
Monday, January 5, 2004

If anyone is interested in an addin that counts lines of code in VB as well as some other useful features (procedure/error handling templates, etc.) - MZTools  @ http://www.mztools.com/index.htm

Totally free, bonus!

Anon
Monday, January 5, 2004

These sorts of issues are addressed with function points, which just for the record, are not functions/procedures, nor are they statements. They are units of functionality.

Function points have the advantage of being transportable across languages and programming style, and can also be calibrated to an individual programmer if desired.

Being able to say how big your program was in fps also will get you some cred in certain interviews.

It's also useful for accurately predicting how long a project will take using a given team.

Dennis Atkins
Tuesday, January 6, 2004

There's a far cry difference, I'd think in saying, "I have a 5,000 line Visual Basic application" and "I have a 30 line C# app."
--------

You're right, there's a huge difference!  The difference is that the 30-line C# app will get you more "street cred".  :P

John Rose
Tuesday, January 6, 2004

Search Google (or your preferred search engine) for the Software Metrics Association in your country. They normally have a wealth of information on not just line counting, but many other aspects of software measurement.

For example, the Australian association is www.asma.org.au

HeWhoMustBeConfused
Tuesday, January 6, 2004

Count both.

GP
Tuesday, January 6, 2004

*  Recent Topics

*  Fog Creek Home