user115422
user115422

Reputation: 4710

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!

Upvotes: 6

Views: 695

Answers (3)

nebuch
nebuch

Reputation: 7075

If you want to avoid a lot of hassle with regex or actually trying to parse the code, you could just count the number of semicolons and close-braces. I mean, those are the two things that get their own lines almost always.

Upvotes: 1

kamilk
kamilk

Reputation: 4039

There are programs, such as CLOC which can count lines of code excluding comments and empty lines, though I don't think they're going to work for your example code.

I think what might work would be finding some automated code formatters, something like http://jsbeautifier.org/, for every language, and measure the number of lines in the output (best using the aforementioned CLOC). Might decrease the impact of a particular programmer's coding style on the result.

Upvotes: 3

Pointy
Pointy

Reputation: 413884

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 :-)

Upvotes: 5

Related Questions