Count logical lines of code in Ubuntu

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

But using the terminal lines of the code counter, I naturally feel that I am writing more lines and highlighting the code, for example:

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

will run as:

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

Thus, I can come up with a very large number of lines without real work, is there a way to count the logical lines of code in a directory? Is there any program that can do this?

Thanks!

+6
source share
3 answers

With the caveat that the significance of the “lines of code” metric is very doubtful, you can start by striking out empty lines.

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

(eg).

For languages ​​like JavaScript, personal coding style can significantly affect the original LOC. Think 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 would be more useful (although, in my opinion, doubtful), would be something like "statements." Of course, you will need a tool that clearly understood the syntax of different languages.

I call these statistics “doubtful,” because in organizations the weak nature of numbers is usually forgotten because it breaks into the table after the table. Project managers begin to extract trends based on LOC, errors (also doubtful), checkins (ditto), etc. are recorded, and the fact that there are such weak correlations with real performance is simply lost.

Sermon on :-)

+5
source

There are programs such as CLOC that can calculate lines of code, excluding comments and empty lines, although I don’t think they are "I will work on your sample code.

I think it would work to find some automatic code formatting, for example http://jsbeautifier.org/ , for each language and measure the number of lines in the output (it is best to use the aforementioned CLOC). May reduce the impact of a particular programmer coding style on the result.

+3
source

If you want to avoid a lot of regexp problems or are actually trying to parse the code, you can just count the number of semicolons and brackets. I mean, these are two things that almost always get their lines.

+1
source

All Articles