Counting lines of code and comments is sometimes bogus, since most of
what we write may be written in one or more lines, depending column
count limitations, screen size, style and so forth.
When doing anything with lines of code, you need a consistent definition of what a "line of code" is, exactly. You need to know if you are counting physical lines of code or logical lines of code and what exactly makes up a "line of code".
As an example:
for (int i = 0; i < array.length; i++) {
boolean truthValue = processElement(array[i]);
if (truthValue) {
break;
}
}
That could be considered 6 physical lines, 4 logical lines, or something else entirely.
In addition, you need rules on a per-language basis, based on coding style that can be consistently applied across projects. It's a fact that 3 lines of Python is not equal to 3 lines of Java is not equal to 3 lines of Objective-C. However, 3 lines of Python always needs to be 3 lines of Python, 3 lines of Java always needs to be 3 lines of Java, and 3 lines of Objective-C always needs to be 3 lines of Objective-C.
Also, comments and code should be counted separately. For example, in a comment, I mentioned that a line of code can be used in Six Sigma as an "opportunity". A non-executable component should not be included in that. In addition, comparing a ratio of comments/lines of code isn't necessarily meaningful as well-organized code should need fewer comments to begin with. I'd be much more careful with counting comments than counting code, other than identifying a distinct lack of comments (especially in public APIs or systems that use automatic documentation generation) or excessive comments as those are opportunities to improve documentation or refactor (or remove useless, potentially out-of-date comments).
Since the most used languages (say C, C++, C# and Java) are free-form,
wouldn't it be more clever to count characters instead?
I don't think so. The line and the statement are the most fundamental building blocks of a unit of code. Every language has a concept of a statement - a single, meaningful, block. Your coding style should specify how to format your statements into lines. Thus, making both statements and lines consistent measurements across projects (written in the same language) to compare.