6

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!

3
  • 4
    Why do you care about lines of code, especially if you're worried about gaming your own metric?
    – Pointy
    Dec 30, 2012 at 0:06
  • @Pointy, project statistics.
    – user115422
    Dec 30, 2012 at 0:07
  • phploc for PHP code, there's probably equivalents for JS
    – Mark Baker
    Dec 30, 2012 at 0:08

3 Answers 3

5

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

10
  • so your script actually would mark both of those as having the same number of lines? correct?
    – user115422
    Dec 30, 2012 at 0:12
  • @fermionoid no, mine wouldn't; that's my point. You'd need a tool that understands the syntax of JavaScript (or PHP or CSS or HTML or Ruby or whatever), which is a non-trivial thing to do; you need a parser, really, and a parser for each language. And even then you have a number that's not super-meaningful, because you don't know the relationship between the number of statements and the actual operational realities of the code.
    – Pointy
    Dec 30, 2012 at 0:16
  • when I ran your code for the first script, I got: 6 13 92 and for the second I got 12 15 98 so what do these numbers mean?
    – user115422
    Dec 30, 2012 at 0:16
  • The wc program tells you the number of "lines", "words", and "characters". The "lines" count is what you asked for; "words" means space-separated strings of characters (I think; something like that), and characters means "bytes" basically.
    – Pointy
    Dec 30, 2012 at 0:17
  • PS, how much does code formatting matter, I mean, I personally like to write code in as many lines as i logically can, it gives me the feeling on pride "i wrote 10k lines of code"!!!
    – user115422
    Dec 30, 2012 at 0:17
3

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.

1
  • Yes that's a great suggestion, though in my advanced state of crusty decrepitude I still consider LOC counts to be a poor substitute for direct code reviews.
    – Pointy
    Dec 30, 2012 at 0:28
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.