Is there a standard way to count lines of code?
No, there is no standard convention, and every tool that counts them will be slightly different.
This may make you ask, "Why then would I ever use LOC as a productivity measure?" and the answer is, because it doesn't really matter how you count a line of code, as long as you count them consistently you can get some idea of the general size of a project in relation to others.
Have a look at the Wikipedia Article, especially the "Measuring SLOC" section:
There are two major types of SLOC measures: physical SLOC and logical SLOC. Specific definitions of these two measures vary, but the most common definition of physical SLOC is a count of lines in the text of the program's source code including comment lines. Blank lines are also included unless the lines of code in a section consists of more than 25% blank lines. In this case blank lines in excess of 25% are not counted toward lines of code.
Logical SLOC measures attempt to measure the number of "statements", but their specific definitions are tied to specific computer languages (one simple logical SLOC measure for C-like programming languages is the number of statement-terminating semicolons). It is much easier to create tools that measure physical SLOC, and physical SLOC definitions are easier to explain. However, physical SLOC measures are sensitive to logically irrelevant formatting and style conventions, while logical SLOC is less sensitive to formatting and style conventions. Unfortunately, SLOC measures are often stated without giving their definition, and logical SLOC can often be significantly different from physical SLOC.
Consider this snippet of C code as an example of the ambiguity encountered when determining SLOC:
for (i=0; i<100; ++i) printf("hello"); /* How many lines of code is this? */
In this example we have:
- 1 Physical Lines of Code LOC
- 2 Logical Lines of Code lLOC (for statement and printf statement)
- 1 Comment Line
[...]
I'd say
- comments count
- blank lines count, because they're important for readability, but not more than one contiguously
- lines with braces count too, but apply the same rule as for blank lines - i.e. 5 nested braces with no code between them counts as one line.
I'd also humbly suggest that any productivity measure which actually relies on a LoC value is bunk :)