14

I neeed a free tool to count SLOC on a Java project. I only really need the following metrics:

  • SLOC
  • number of comment lines
  • optionally javadoc metrics
  • optionally sort statistics by file type (.java, .js, .css, .html, .xml, etc)

Bonus:

  • 100% Java, I don't like mix something like sloccount with cygwin
  • netbeans plugin
  • or preferably, maven plugin
dfa
  • 114,442
  • 31
  • 189
  • 228

8 Answers8

18

Did you consider using Sonar (which uses its own internal tool since version 1.9, sonar-squid, instead of JavaNCSS which has some flaws and doesn't work well with Java 1.5 or 1.6 projects)? This is the kind of report you'll get:

alt text http://sonar.codehaus.org/wp-content/uploads/2009/05/sonar-squid.jpg

Sonar does a lot more than just collecting SLOC though, maybe not what you want.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
9

My advice is don't.

You get what you measure for.

If you reward big LOC, you'll get a lot of LOC.

If you reward small LOC, you'll get perl.

Will
  • 73,905
  • 40
  • 169
  • 246
  • 1
    Why do you think that he *rewards* big LOC? Reward is not the only usage of SLOC. – Rorick Feb 02 '10 at 15:17
  • "You get what you measure for" == reward, even if implicit. Everybody should go read what our hosts think about metrics :) – Will Feb 02 '10 at 17:42
  • This is "the" reference: *Tell me how [and when] you’ll measure me, and I’ll tell you how I’ll behave.* --Eliyahu M. Goldratt (The Goal). But nothing says the OP is measuring people here so I still do not agree with you. – Pascal Thivent Feb 02 '10 at 23:54
  • 2
    And here is another one: *If you cannot measure it, you cannot improve it.* --Lord Kelvin – Pascal Thivent Feb 02 '10 at 23:57
  • I'm not sure that our hosts are the last authority) There's a plenty of opinions. – Rorick Feb 03 '10 at 07:21
  • I'd take Joel over random programmer any day – Will Feb 03 '10 at 16:27
  • I think it's different if a dev measures for his own personal improvement vs. management measuring to make the the job of evaluation seem objective. (And also to make it easier) For personal observation and learning, I'm okay with it. Oh, and I'm a perl programmer! :-) – Matthew Heusser Feb 04 '10 at 19:08
  • SLOC definitely has usages other than simply rewarding developer "productivity". It is just one way to measure the health and growth of a system. Tracking it helps determine how the system is evolving. If I see a sudden huge jump or drop in SLOC I want to know why. I think metrics like cyclomatic complexity are more useful, but SLOC is definitely a useful metric. – Jon Jul 16 '15 at 15:14
5

There is a maven plugin for JavaNCSS. There is also the JDepend plugin that generates some complexity metrics if you're interested in those.

Interestingly, the reason you're having trouble finding a decent plugin is because most people don't care enough to write one or don't find those metrics useful enough. Take that as you wish.

Kevin
  • 30,111
  • 9
  • 76
  • 83
  • The JavaNCSS-maven-plugin http://www.mojohaus.org/javancss-maven-plugin/index.html seams to be is a bit more active then the old maven-JavaNCSS-plugin – Ralph Jan 31 '17 at 17:00
1

This is free (as in beer, not source sadly): http://www.campwoodsw.com/sourcemonitor.html

It's a bit rough around the edges, but does the job.

Paolo
  • 22,188
  • 6
  • 42
  • 49
  • 1
    Where in the world is that famous "free" beer. I still don't get where that phrase originated. – OscarRyz Feb 02 '10 at 15:27
  • @Oscar *"Free software" is a matter of liberty, not price. To understand the concept, you should think of "free" as in "free speech," not as in "free beer."* http://www.gnu.org/philosophy/free-sw.html – Pascal Thivent Feb 02 '10 at 15:55
  • @Pascal: Yeah I know that, but again, "as in free beer". What "free beer" everywhere I know the beer is not free at all :) – OscarRyz Feb 02 '10 at 16:17
  • @Oscar I misunderstood the initial question then. Anyway, the fact is that "free as in beer" has been adopted for gratis, even if beer is not gratis :) More at http://en.wikipedia.org/wiki/Gratis_versus_Libre – Pascal Thivent Feb 02 '10 at 16:34
  • 1
    @Reyes - you misunderstood, the idea is to promote free (libre) beer. Everywhere beer is kept locked in dark cellars or smoky bars - it wants to run free in the hills – Martin Beckett Feb 04 '10 at 17:02
  • @MartinBeckett From the German wikipedia: https://de-m-wikipedia-org.translate.goog/wiki/Freibier?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=wapp – AxelW Oct 20 '22 at 10:53
1

I used combination of RefactorIt browser and FreeMind (with some Groovy scripting) for semi-automatic generating of SLOC and Comments LOC metrics reports. RefactorIt can export metric in csv format, it is open source and pure Java. Unfortunately, it seems to be no longer developed. FreeMind is mind mapping software and I used it to generate mind map from that csv metrics report. I've done that mostly for fun, to be honest. You could build your solution upon these tools, though I doubt that it is the best way.

Rorick
  • 8,857
  • 3
  • 32
  • 37
1

For an open source project, you can get SLOC measures as a side benefit of registering your project at http://ohloh.org

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

CodeFacts will give you line counts for the source and comments along with some stats on lines and class/method counts differences between two revisions. It's really a metrics tool though so it may be overkill for what you want.

enter image description here

Al.
  • 1,507
  • 1
  • 15
  • 12
0

If using NetBeans 8, download the TikiOne JaCoCoverage plugin and then run the code coverage test against your project. It will give you lines per package and total lines. It doesn't, however, differentiate between things like lines of code and lines of comments.

enter image description here

james.garriss
  • 12,959
  • 7
  • 83
  • 96