Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to count lines of code?

I realize there's no definitely "right" answer to this question, but when people talk about lines of code, what do they mean? In C++ for example, do you count blank lines? Comments? Lines with just an open or close brace?

I know some people use lines of code as a productivity measure, and I'm wondering if there is a standard convention here. Also, I think there's a way to get various compilers to count lines of code - is there a standard convention there?

like image 853
Gwildore Avatar asked Dec 09 '08 16:12

Gwildore


People also ask

How do you count the number of lines of code?

If you want to get the lines of code of a single file, you can use cloc <filename>. We're using the command to get the lines of codes of a simple Vue file. It also recognized that it is a VueJS component. We can check all of the lines of code in a file or directory by the command cloc --by-file.

What is standard of counting the line of code?

Source lines of code (SLOC), also known as lines of code (LOC), is a software metric used to measure the size of a computer program by counting the number of lines in the text of the program's source code.

Can Visual Studio count lines of code?

Feature to count lines of code - Calculate Metrics. With it you can calculate your metrics (LOC, Maintaince index, Cyclomatic index, Depth of inheritence) for each project or solution.


2 Answers

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.

like image 59
Craig H Avatar answered Sep 30 '22 17:09

Craig H


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

[...]

like image 42
xsl Avatar answered Sep 30 '22 18:09

xsl