Eclipse 计算代码行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6924158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:23:01  来源:igfitidea点击:

Eclipse count lines of code

eclipsemetricsline-count

提问by confusified

I've tried the Metrics pluginand 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?

我试过Metrics 插件,虽然它很好,但它不是我老板想要的。它把一条线算作一条线,}他不希望这算作“它不是一条线,它是一种风格选择”。我还需要生成某种形式的关于所提供指标的报告。有没有什么好的工具可以做到这一点?

采纳答案by aliopi

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

安装Eclipse 指标插件。创建 HTML 报告(带有可选的 XML 和 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.

如果需要,您可以通过忽略空白行和仅注释行或排除 Javadoc 来调整代码行指标。为此,请检查位于 的选项卡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:

该插件为 LoC 提供了一种替代指标,称为Number of Statements。对此,作者有话要说:

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.

在您澄清您的问题后,我了解到您需要查看实时指标违规情况,例如编译器警告或错误。您还需要报告功能来为您的老板创建报告。我上面描述的插件用于报告,因为您必须在想要查看指标时导出指标。

回答by Christian

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.

  1. 搜索->文件

  2. 在“包含文本”-> 中键入以下内容^.*$。然后选中“正则表达式”复选框

  3. 在“文件名模式”中键入以下内容 -> *.java

  4. 点击“搜索”

  5. 检查“搜索”选项卡中的匹配数。

回答by ZOXIS

If on OSX or *NIX use

如果在 OSX 或 *NIX 上使用

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

从 *.java 文件中获取所有实际的 java 代码行

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

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

从 *.java 文件中获取所有行,包括空行和注释

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 {} \;

回答by VonC

Another way would by to use another loc utility, like LocMetricsfor 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-metricsis more adapted to what you expect.

另一种方法是使用另一个 loc 实用程序,例如LocMetrics
它还列出了许多其他 loc 工具。与 Eclipse 的集成并不总是存在(就像与Metrics2 一样,您可以查看它,因为它是比Metrics更新的版本),但至少这些工具可以根据逻辑线进行推理(通过求和计算终端分号和终端大括号)。
您还可以检查eclipse-metrics更适合您的期望。

回答by ?l?

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

另一个工具是Google Analytix,它也允许您运行指标,即使您无法在出现错误的情况下构建项目

回答by ccleve

One possible way to count lines of code in Eclipse:

在 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.

使用搜索/文件...菜单,选择文件搜索选项卡,为包含文本指定\n[\s]*(这不会计算空行),然后勾选正则表达式。

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

帽子提示:www.monblocnotes.com/node/2030

回答by Li Ying

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.

我创建了一个Eclipse插件,可以统计源代码的行数。它支持 Kotlin、Java、Java Script、JSP、XML、C/C++、C# 和许多其他文件类型。

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

请看一看。对于任何反馈,我们都表示感谢!

the git-hub repository is here

git-hub 存储库在这里

回答by kc2001

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

首先要做的是确定您对“代码行”(LOC) 的定义。在你的问题中

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

如果需要,您可以通过忽略空白行和仅注释行或排除 Javadoc 来调整代码行指标

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?

你可以看出人们对一行代码的构成有不同的看法。尤其是,人们对于他们是真正想要代码行数还是语句数往往不准确。例如,如果您有以下非常长的一行语句,您想报告什么,1 个 LOC 还是数百个语句?

{ 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.

当有人问你叫什么 LOC 时,确保你能回答,即使它只是“我对 LOC 的定义是 Metrics2 的定义”。一般来说,对于最常见的格式化代码(与我的示例不同),流行的工具会给出非常相似的数字,因此 Metrics2、SonarQube 等都应该没问题,只要您始终如一地使用它们。换句话说,不要使用一种工具计算某些代码的 LOC,并将该值与使用不同工具测量的该代码的更高版本进行比较。

回答by Andrew Spencer

For static analysis, I've used and recommend SonarQube which runs just about all the metrics you could possibly wanton 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).

对于静态分析,我已经使用并推荐了 SonarQube,它可以运行几乎所有你可能想要的关于各种语言的指标,并且在基本版本中是免费的(你必须付费才能分析我想要的各种语言)只能用枪指着我的头编码)。

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

您必须将其安装为 Web 应用程序,在源代码存储库中运行分析,但它也有一个Eclipse 插件

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.

披露:我与 SonarSource 没有财务关系。

回答by Benjamin

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.

ProjectCodeMeter 完全按照您的描述(仅有效行)计算 LLOC(逻辑代码行)。它作为外部代码度量工具集成到Eclipse 中,虽然它不是实时的,但它会生成一份报告。实际上它计算了许多源代码度量,例如复杂性、算术复杂度、硬编码字符串、数字常量......甚至以小时为单位估计开发时间.