npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

when2run

v0.0.2

Published

with when2run to reduce cyclomatic complexity by chaning execute

Downloads

7

Readme

Cyclomatic Complexity 圈复杂度

“圈复杂度”(Cyclomatic Complexity),这是衡量程序源代码中线性独立路径数量的一种度量。圈复杂度用来指示程序的复杂性。它是通过程序的控制流图计算得出的:图的节点对应于程序的不可分割的命令组,如果第二个命令可能在第一个命令之后立即执行,则有向边连接两个节点。

How to reduce Cyclomatic Complexity? 如何降低圈复杂度?

  1. 简化条件逻辑
    1. 合并条件:如果可以将多个条件合并成一个,则这样做
    2. 使用守卫子句:用守卫子句替换嵌套条件
      1. 检查无效输入
      2. 检查边界条件
      3. 确保必要条件
    3. 应用德摩根定律:简化复杂的布尔表达式
      1. 原始表达式: !(A && B) 应用德摩根定律后: !A || !B
      2. 原始表达式: !(A || B) 应用德摩根定律后: !A && !B
  2. 分解函数
    1. 提取方法:将大型函数分解为更小、更易于管理的函数
    2. 遵循单一职责原则:确保函数或方法制作一件事
  3. 减少分支
    1. 使用多态性替换条件语句:使用多态性处理不同的情况,而不是使用switch或if-else语句
      1. 多态实现方式
        1. 继承:子类继承父类,并可以拥有自己的行为
        2. 抽象类和接口:定义方法的抽象类型,让子类提供具体实现
        3. 方法重载:相同的方法名,但参数列表不同
        4. 方法覆写:子类重新定义父类的方法
    2. 使用查找表或字典:代替多个if-else或switch-case语句
  4. 循环简化
    1. 移除不必要的循环:消除对最终结果没有贡献的循环
    2. 使用高阶函数:使用map、filter、reduce等函数来简化循环
  5. 重构代码
    1. 移除无用代码:清除不再可达或可用的代码
    2. 内联单次使用的变量:如果一个变量只使用一次,考虑内联该表达式
  6. 使用设计模式
    1. 策略模式:将算法封装到不同的类中,并使用他们可互换
    2. 命令模式:将请求封装为对象,从而允许不同请求的客户端进行参数化
  7. 编写清晰的代码
    1. 可理解的名称:使用清晰且又意义的变量、函数和类名
    2. 保持简单:不要过度设计解决方案;力求最简单的方法来完成工作
  8. 编写测试
    1. 单元测试:独立于应用程序的其余部分对应用程序的小部分进行测试
    2. 测试驱动开发:在编写代码之前编写测试用例,以确保您的代码保持简单,并且只做它应该做的一件事

Conclusion 结论

降低圈复杂度通常是有益的,因为他通常会让代码更易于维护和理解。然而,重要的是要平衡降低复杂性的愿望与其他因素之间的关系,如性能和可读性。有时,增加一些复杂性可以带来更优化或更清晰的代码。