Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count logical lines of code in Ubuntu

I am working on a web application and I need to be able to keep track of php, css, html and JavaScript lines of code within the /var/www directory.

But using the terminal lines of code counter, I naturally feel like writing more lines and spacing out code like:

if($var == $other)echo("hi");

would be done as:

if($var == $other)
{
    echo("hi");
}

In this way I can come up with a very high number of lines without actually doing any real work, is there any way I can count the logical lines of code in the directory? Is there any program that can do so?

Thanks!

like image 513
user115422 Avatar asked Dec 30 '12 00:12

user115422


1 Answers

With the caveat that the meaningfulness of a "lines of code" metric is highly dubious, you could start by grepping out blank lines.

find . -name '*.php' -print0 | xargs -0 cat | egrep -v '^[ \t]*$' | wc

(for example).

For languages like JavaScript, personal coding style can have a really significant impact on raw LOC. Consider that some people write like this:

if (testSomething()) return null;

if (somethingElse()) {
  doThis();
} else {
  doThat();
}

And some people write like this:

if (testSomething())
{
  return null;
}

if (somethingElse())
{
  doThis();
}
else
{
  doThat();
}

What'd be somewhat more useful (though still, in my opinion, dubious) would be something that would count something like "statements". Of course, you'd need a tool that explicitly understood the syntax of different languages.

I call this statistic "dubious" because in organizations the weak nature of the number tends to be forgotten as it works its way into spreadsheet after spreadsheet. Project managers start extracting trends based on LOC, bugs logged (also dubious), checkins (ditto), etc, and the fact that there are such weak correlations to true productivity is just lost.

Sermon over :-)

like image 173
Pointy Avatar answered Oct 06 '22 23:10

Pointy