Eclipse count lines of code [closed]
Asked Answered
T

10

112

I've tried the Metrics plugin and although it's nice and all, it's not what my boss is looking for. It counts a line with just one } as a line and he doesn't want that to count as "its not a line, its a style choice". I also need to generate some form of report about the metrics provided. Are there any good tools for this?

Thai answered 3/8, 2011 at 9:16 Comment(1)
Side question: is this to be used as a metric to show how well your software developers are working? If so character/non-whitespace count might be better. Even still it seems like a really arbitrary metric... As a friend stated: "in general more lines of code to solve a problem is not better (or worse) than less lines of code to solve the same problem"Shyster
G
97

Install the Eclipse Metrics Plugin. To create a HTML report (with optional XML and CSV) right-click a project -> Export -> Other -> Metrics.

You can adjust the Lines of Code metrics by ignoring blank and comment-only lines or exclude Javadoc if you want. To do this check the tab at Preferences -> Metrics -> LoC.

That's it. There is no special option to exclude curly braces {}.

The plugin offers an alternative metric to LoC called Number of Statements. This is what the author has to say about it:

This metric represents the number of statements in a method. I consider it a more robust measure than Lines of Code since the latter is fragile with respect to different formatting conventions.

Edit:

After you clarified your question, I understand that you need a view for real-time metrics violations, like compiler warnings or errors. You also need a reporting functionality to create reports for your boss. The plugin I described above is for reporting because you have to export the metrics when you want to see them.

Gulgee answered 3/8, 2011 at 10:25 Comment(5)
Exactly what I was looking for and nice export feature.Whimwham
The current version of this plugin (3.14.1) doesn't appear to work with Eclipse Luna (4.4). Surprisingly, the ancient Metrics plugin from Sourceforge still works...Royo
Many people report that it's not working any more, but nobody files a bug at SourceForge...Tachylyte
Yeah. I am not able to see any options under right click a project -> Export, I have installed plug-in directly from eclipse marketplace - version is - Eclipse Metrics 3.12.0, can anyone please help me. thanks!Sollars
There is a working version - github.com/qxo/eclipse-metrics-plugin/blob/master/updatesite/…Strachan
S
288

There's always the "brute force":

  1. Search->File

  2. Type the following in "Containing text" -> ^.*$. Then check the "Regular Expression" checkbox

  3. Type the following in "File name patterns" -> *.java

  4. Click "Search"

  5. Check the number of matches in the "Search" Tab.

Seabury answered 15/4, 2013 at 11:50 Comment(11)
This did exactly what I was looking for. It doesn't exclude brackets or anything fancy, but it gives the answer I was looking for, IN eclipse, WITHOUT plugins. Thanks!Selfmoving
counts everything including comments, would be nice if it counts lines of actual statementsSldney
In eclipse you can define working sets if you would like to get this number separately for src, test, or by any folder. Thanks for the trick!Aksum
You can also use \n[\s]* and it will ignore empty lines as wellHackathorn
This works with latest (neon) eclipse but metrics plugin does notPaeon
Is there any chance to exclude comments?Min
@Sldney comments have to be typed as well and are necessary part of any source code. Not counting those induces they wouldn't be important or are just blind text - which is a serious mistake.Cladding
^.*\w.*$ includes only lines with at least one ascii alphanumeric character. Use this for excluding empty lines, bracket-only lines etc.Citral
And it even works for C if you change *.java to *.c, *.h ;-)Retool
Seems a bit silly, as it doesn't count comments or so, but hey, it can giver a rough view without need of plugins, so thats a smart solution after allMartie
nice clue, suggesting this improved regex: "^\s*(}\s+)?[a-zA-Z0-9{+_@"-].+$" that would not count lone closing brackets, neither empty nor pure comment linesHelenehelenka
G
97

Install the Eclipse Metrics Plugin. To create a HTML report (with optional XML and CSV) right-click a project -> Export -> Other -> Metrics.

You can adjust the Lines of Code metrics by ignoring blank and comment-only lines or exclude Javadoc if you want. To do this check the tab at Preferences -> Metrics -> LoC.

That's it. There is no special option to exclude curly braces {}.

The plugin offers an alternative metric to LoC called Number of Statements. This is what the author has to say about it:

This metric represents the number of statements in a method. I consider it a more robust measure than Lines of Code since the latter is fragile with respect to different formatting conventions.

Edit:

After you clarified your question, I understand that you need a view for real-time metrics violations, like compiler warnings or errors. You also need a reporting functionality to create reports for your boss. The plugin I described above is for reporting because you have to export the metrics when you want to see them.

Gulgee answered 3/8, 2011 at 10:25 Comment(5)
Exactly what I was looking for and nice export feature.Whimwham
The current version of this plugin (3.14.1) doesn't appear to work with Eclipse Luna (4.4). Surprisingly, the ancient Metrics plugin from Sourceforge still works...Royo
Many people report that it's not working any more, but nobody files a bug at SourceForge...Tachylyte
Yeah. I am not able to see any options under right click a project -> Export, I have installed plug-in directly from eclipse marketplace - version is - Eclipse Metrics 3.12.0, can anyone please help me. thanks!Sollars
There is a working version - github.com/qxo/eclipse-metrics-plugin/blob/master/updatesite/…Strachan
S
23

If on OSX or *NIX use

Get all actual lines of java code from *.java files

find . -name "*.java" -exec grep "[a-zA-Z0-9{}]" {} \; | wc -l

Get all lines from the *.java files, which includes empty lines and comments

find . -name "*.java" -exec cat | wc -l

Get information per File, this will give you [ path to file + "," + number of lines ]

find . -name "*.java" -exec wc -l {} \;
Stilted answered 16/8, 2012 at 10:16 Comment(3)
Running the first command on OSX I get: grep: invalid character range 0Sldney
This has worked for me find . -name "*.java" | xargs cat | grep '[alnum]' | wc -lSldney
should be find . -name "*.java" | xargs cat | grep "[a-zA-Z0-9{}]" | wc -lGrindstone
I
13

One possible way to count lines of code in Eclipse:

using the Search / File... menu, select File Search tab, specify \n[\s]* for Containing text (this will not count empty lines), and tick Regular expression.

Hat tip: www.monblocnotes.com/node/2030

Impeachment answered 16/1, 2015 at 20:54 Comment(2)
wow, that's super genius. nice to have it not count blank lines, as long as you're not paid by the number...Vow
Also select "*.java" (or your extension) to focus on the code filesGilud
C
8

Another way would by to use another loc utility, like LocMetrics for instance.
It also lists many other loc tools. The integration with Eclipse wouldn't be always there (as it would be with Metrics2, which you can check out because it is a more recent version than Metrics), but at least those tools can reason in term of logical lines (computed by summing the terminal semicolons and terminal curly braces).
You can also check with eclipse-metrics is more adapted to what you expect.

Compatible answered 3/8, 2011 at 9:25 Comment(0)
D
7

Another tool is Google Analytix, which will also allow you to run metrics even if you can`t build the project in case of errors

Damnatory answered 23/10, 2012 at 7:17 Comment(1)
Although the site says it's only for older versions of eclipse, it works just fine on Luna for me. Thanks for this.Guffey
S
1

I created a Eclipse plugin, which can count the lines of source code. It support Kotlin, Java, Java Script, JSP, XML, C/C++, C#, and many other file types.

Please take a look at it. Any feedback would be appreciated!

the git-hub repository is here

Silures answered 14/6, 2016 at 0:50 Comment(0)
D
1

The first thing to do is to determine your definition of "line of code" (LOC). In both your question

It counts a line with just one } as a line and he doesn't want that to count as "its not a line, its a style choice"

and in the answers, e.g.,

You can adjust the Lines of Code metrics by ignoring blank and comment-only lines or exclude Javadoc if you want

you can tell that people have different opinions as to what constitutes a line of code. In particular, people are often imprecise about whether they really want the number of lines of code or the number of statements. For example, if you have the following really long line filled with statements, what do you want to report, 1 LOC or hundreds of statements?

{ a = 1; b = 2; if (a==c) b++; /* etc. for another 1000 characters */ }

And when somebody asks you what you are calling a LOC, make sure you can answer, even if it is just "my definition of a LOC is Metrics2's definition". In general, for most commonly formatted code (unlike my example), the popular tools will give numbers fairly similar, so Metrics2, SonarQube, etc. should all be fine, as long as you use them consistently. In other words, don't count the LOC of some code using one tool and compare that value to a later version of that code that was measured with a different tool.

Discern answered 23/10, 2019 at 19:5 Comment(0)
D
0

ProjectCodeMeter counts LLOC (logical lines of code) exactly as you described (only effective lines). it integrates into eclipse as external code metrics tool, it's not real-time though, it generates a report.actually it counts many source code metrics such as complexity, arithmetic intricacy, hard coded strings, numeric constants.. even estimates development time in hours.

Drawee answered 6/10, 2011 at 11:12 Comment(0)
I
0

For static analysis, I've used and recommend SonarQube which runs just about all the metrics you could possibly want on a wide range of languages, and is free in the basic version (you have to pay to analyse the sorts of languages I'd only code in with a gun to my head).

You have to install it as a web-app running the analysis off your source code repository, but it also has an Eclipse plugin.

It's overkill if you just want to know, as a one-off, how many lines of code there are in your project. If you want to track metrics through time, compare across projects, fire warnings when a threshold is exceeded, etc., it's fantastic.

Disclosure: I have no financial relationship with SonarSource.

Insectarium answered 21/10, 2013 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.