计算 Javascript 的循环复杂度

发布于 2024-07-06 04:54:01 字数 1511 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

能否归途做我良人 2024-07-13 04:54:01

新版本的 http://jshint.com 已经出来了,有一个非常好的圈复杂度计算器

The new version of http://jshint.com is out and has a very good cyclomatic complexity calculator

甜味拾荒者 2024-07-13 04:54:01

现在还有 Yardstick: https://github.com/calmh/yardstick

它尝试计算圈复杂度惯用的 Javascript,比 jscheckstyle 处理更多的情况。

There's now also Yardstick: https://github.com/calmh/yardstick

It tries to calculate cyclomatic complexity for idiomatic Javascript, handling more cases than for example jscheckstyle.

哎呦我呸! 2024-07-13 04:54:01

您可以使用 BLUNCK ARCHIVE 中的 ccm 工具.info 或 github repo jonasblunck/ccm

它支持 JavaScript、C/C++ 和 C# 。 它是免费的,在 Windows 上运行(也可以在 Linux 和 Mac OS X 上运行 - 使用 Mono 框架)。

You can use the ccm tool from ARCHIVE of blunck.info or the github repo jonasblunck/ccm

It supports JavaScript, C/C++ and C#. It's free, runs on Windows (can be run on Linux and Mac OS X as well - using the Mono framework).

轻拂→两袖风尘 2024-07-13 04:54:01

我帮助编写了一个工具来对 JavaScript 项目执行软件复杂性分析:

complexity-report

它报告了一堆不同复杂度指标:代码行数、参数数量、圈复杂度、圈密度、Halstead 复杂度度量、可维护性指数、一阶密度、变更成本和核心大小。

它是根据 MIT 许可证发布的,并使用 Node.js 和 Esprima JavaScript 解析器构建。 它可以通过 npm 安装,如下所示:

npm i -g complexity-report

I helped write a tool to perform software complexity analysis on JavaScript projects:

complexity-report

It reports a bunch of different complexity metrics: lines of code, number of parameters, cyclomatic complexity, cyclomatic density, Halstead complexity measures, the maintainability index, first-order density, change cost and core size.

It is released under the MIT license and built using Node.js and the Esprima JavaScript parser. It can be installed via npm, like so:

npm i -g complexity-report
生生不灭 2024-07-13 04:54:01

为了答案的完整性,我前段时间一直在寻找相同的工具,但没有找到任何适合可视化的工具,所以我写了 plato< /a>

示例报告:

它使用 phil 的复杂性报告(上面提到的) )并且还聚合来自 jshint(以及最终其他)的数据。

For completeness in the answers, I was looking for the same tool some time ago and didn't find anything that worked well for visualization so I wrote plato

Sample reports for :

It uses phil's complexity-report (mentioned above) and also aggregates data from jshint (and eventually, others).

柠檬 2024-07-13 04:54:01

JSHint 最近 添加了对计算代码指标的支持

您可以设置最大值:

示例

形式参数的最大数量每个函数允许

/*jshint maxparams:3 */

function login(request, onSuccess) {
  // ...
}

// JSHint: Too many parameters per function (4).
function logout(request, isManual, whereAmI, onSuccess) {
  // ...
}

的最大嵌套代码块数 每个函数允许的最大嵌套代码块数

/*jshint maxdepth:2 */

function main(meaning) {
  var day = true;

  if (meaning === 42) {
    while (day) {
      shuffle();

      if (tired) { // JSHint: Blocks are nested too deeply (3).
          sleep();
      }
    }
  }
}

每个函数允许的最大语句数

/*jshint maxstatements:4 */

function main() {
  var i = 0;
  var j = 0;

  // Function declarations count as one statement. Their bodies
  // don't get taken into account for the outer function.
  function inner() {
    var i2 = 1;
    var j2 = 1;

    return i2 + j2;
  }

  j = i + j;
  return j; // JSHint: Too many statements per function. (5)
}

JSHint recently added support for calculating code metrics.

You can set maximum values for:

Examples

Maximum number of formal parameters allowed per function

/*jshint maxparams:3 */

function login(request, onSuccess) {
  // ...
}

// JSHint: Too many parameters per function (4).
function logout(request, isManual, whereAmI, onSuccess) {
  // ...
}

Maximum number of nested code blocks allowed per function

/*jshint maxdepth:2 */

function main(meaning) {
  var day = true;

  if (meaning === 42) {
    while (day) {
      shuffle();

      if (tired) { // JSHint: Blocks are nested too deeply (3).
          sleep();
      }
    }
  }
}

Maximum number of statements allowed per function

/*jshint maxstatements:4 */

function main() {
  var i = 0;
  var j = 0;

  // Function declarations count as one statement. Their bodies
  // don't get taken into account for the outer function.
  function inner() {
    var i2 = 1;
    var j2 = 1;

    return i2 + j2;
  }

  j = i + j;
  return j; // JSHint: Too many statements per function. (5)
}
过度放纵 2024-07-13 04:54:01

由于圈复杂度是通过计算关键字的数量来评估的
“if、switch、while for break”等...每个与 C 一起使用的工具都可以完成这项工作,例如
源监视器:
http://www.campwoodsw.com/sourcemonitor.html

实际上,在 javascript 上,你尝试的越多模块化你的代码,你会减慢它的速度,所以要持保留态度;)

编辑:
真的无法理解这个答案是怎么回事,当我在答案中告诉我一个用于计算 javascript 中的圈复杂度的好工具时,我又收到了反对票,
这尤其有效。

对于第二个断言,我的评论是来自经验,我从来没有告诉不要模块化你的js代码,我只是告诉在做的时候要注意,因为通常需要对速度进行权衡,当我谈到速度时,我的意思是可能会发生两种不同的速度减慢:在下载时执行时(以及在慢速设备中,例如pda/智能手机这很重要)。

由于这样的工具通常会促使开发人员编写更多代码,试图追求尽可能小的索引,
但不幸的是,在 js 中,更多代码意味着可能会发生速度减慢的情况,并且过度使用这些工具是不好的。
当然,这些工具可以为您提供代码可以改进的地方的提示,但您必须掌握如何使用该工具,而不是盲目依赖它。

所以,如果你再次对我投反对票,请写一条评论,解释你这样做的原因,讨论只能从中受益,谢谢你,也很抱歉发泄。

Since cyclomatic complexity is evaluated counting the number of keyword
"if, switch, while for break" etc.. every tools that works with C will do the job, like
sourcemonitor:
http://www.campwoodsw.com/sourcemonitor.html

Actually, on javascript the more you try to modulize your code, the more you will slow it down, so take it with a grain of salt ;)

EDIT:
I Really can't understand what's going on on this answer, I get another downvote, when in my answer I tell a good tool for calculating cyclomatic complexity in javascript,
and this in particular works very well.

For the second assertion, mine is a comment that comes from experience, I never tell don't modulize your js code, I only tell to make attention in doing it, because often there is a tradeoff with speed, and when I talk of speed I mean that 2 different slowdown can happen: at download time and at execution time (and in slow device like pda/smartphone this is important).

Since tools like this often drive developer into writing more code trying to chase the smaller index possible,
but in js more code unfortunately means that slowdowns can happen, and the overuse of these tools is bad.
Surelly these tools can give you hints of where your code can be improved, but you've to master how to use the tool and not blindy rely on it.

So if you downvote me again, please write a comment in which you explain why you do so, the discussion can only benefit from this, thank you and sorry for the vent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文