OCLint的部分规则(Size 部分)

40 篇文章 0 订阅
17 篇文章 0 订阅

OCLint的部分规则(Size 部分)

对OCLint的部分规则进行简单翻译解释,有部分进行了验证以及进一步分析、测试。OCLint其他相关内容如下:

--
OCLint-iOS-OC项目几种简单使用OCLint的部分规则(Basic 部分)
OCLint的部分规则(Unuseed 部分)OCLint的部分规则(Size 部分)
OCLint的部分规则(Redundant 部分)OCLint的部分规则(Naming 部分)
OCLint的部分规则(Migration 部分)OCLint的部分规则(Empty 部分)
OCLint的部分规则(Design 部分)OCLint的部分规则(Convention 部分)
OCLint的部分规则(CoCoa 部分)



1、high cyclomatic complexity

      Since:0.4 定义类传送门~点击

Cyclomatic complexity is determined by the number of linearly independent paths through a program’s source code. In other words, cyclomatic complexity of a method is measured by the number of decision points, like if, while, and for statements, plus one for the method entry.

简单解释:圈复杂度过高。统计一个函数有多少个分支(if, while,for,等等),没有的话复杂度为一,每增加一个分支复杂度加一。简单计算的话V(G)=e-n+2。其中,e表示控制流图中边的数量,n表示控制流图中节点的数量,或者V(G)=区域数=判定节点数+1。当然可以数一数。

    void example(int a, int b, int c) { // 1
        if (a == b) {                   // 2
            if (b == c) {               // 3 
            } else if (a == c){          // 3
            }
            else {
            }
        }
        for (int i = 0; i < c; i++)  {  // 4
        }
        switch(c)  {
            case 1:                   // 5
                break;
            case 2:                   // 6
                break;
            default:                  // 7
                break;
        }
    }

Thresholds:
CYCLOMATIC_COMPLEXITY The cyclomatic complexity reporting threshold, default value is 10. Suppress:

Suppress:
__attribute__((annotate("oclint:suppress[high cyclomatic complexity]")))

References:
McCabe (December 1976). “A Complexity Measure”. IEEE Transactions on Software Engineering: 308–320

2、long class

      Since:0.6 定义类传送门~点击

Long class generally indicates that this class tries to do many things. Each class should do one thing and that one thing well.

简单解释:类行数太多。

    class Foo {
        void bar() {
            // 1001 lines of code
        }
    }

Thresholds:
LONG_CLASS The class size reporting threshold, default value is 1000.

3、 long line

      Since:0.6 定义类传送门~点击

When the number of characters for one line of code is very high, it largely harms the readability. Break long lines of code into multiple lines.

简单解释:单行代码太长,影响可读性。

    void example()
    {
        int a012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789;
    }

Thresholds:
LONG_LINE The long line reporting threshold, default value is 100.

4、long method

      Since:0.6 定义类传送门~点击

Long method generally indicates that this method tries to do many things. Each method should do one thing and that one thing well.

简单解释:方法太长,影响阅读,应该实现单一职责。

    void example() {
        cout << "hello world";
        cout << "hello world";
        // repeat 48 times
    }

Thresholds:
LONG_METHOD The long method reporting threshold, default value is 50.

5、 high ncss method

      Since:0.6 定义类传送门~点击

This rule counts number of lines for a method by counting Non Commenting Source Statements (NCSS). NCSS only takes actual statements into consideration, in other words, ignores empty statements, empty blocks, closing brackets or semicolons after closing brackets. Meanwhile, a statement that is broken into multiple lines contribute only one count.

简单解释:其实是指某个代码块中代码行数过多(只统计有效的语句),查看代码块中代码是否能拆分,公共功能能否提供一个公共接口。空语句,空块,右括号或分号后的右括号会被忽略。

    void example()          // 1
    {
        if (1)              // 2
        {
        }  else                // 3
        {
        }
    }

Thresholds:
NCSS_METHOD The high NCSS method reporting threshold, default value is 30.

Suppress:
attribute((annotate(“oclint:suppress[high ncss method]”)))

6、deep nested block

      Since:0.6 定义类传送门~点击

This rule indicates blocks nested more deeply than the upper limit.

简单解释:嵌套块是否超过指定的深度值.

    if (1) {               // 1
        {           // 2
            {       // 3
            }
        }
    }

Thresholds:
NESTED_BLOCK_DEPTH The depth of a block or compound statement reporting threshold, default value is 5.

7、high npath complexity

      Since:0.4 定义类传送门~点击

NPath complexity is determined by the number of execution paths through that method. Compared to cyclomatic complexity, NPath complexity has two outstanding characteristics: first, it distinguishes between different kinds of control flow structures; second, it takes the various type of acyclic paths in a flow graph into consideration.
Based on studies done by the original author in AT&T Bell Lab, an NPath threshold value of 200 has been established for a method.

简单解释:NPath复杂度是一个方法中各种可能的执行路径总和,一般把200作为考虑降低复杂度的临界点,这里提示NPath复杂度过高。

    void example() {
        // complicated code that is hard to understand
    }

Thresholds:
NPATH_COMPLEXITY The NPath complexity reporting threshold, default value is 200.

Suppress:
__attribute__((annotate("oclint:suppress[high npath complexity]")))

References:
Brian A. Nejmeh (1988). “NPATH: a measure of execution path complexity and its applications”. Communications of the ACM 31 (2) p. 188-200

8、too many fields

      Since:0.7 定义类传送门~点击

A class with too many fields indicates it does too many things and lacks proper abstraction. It can be redesigned to have fewer fields.

简单解释:一个类中有定义太多东西,需要进行适当的抽象、设计。

    class c {
        int a, b;
        int c;
        // ...
        int l;
        int m, n;
        // ...
        int x, y, z;
        void m() {}
    };

Thresholds:
TOO_MANY_FIELDS The reporting threshold for too many fields, default value is 20.

9、too many methods

      Since:0.7 定义类传送门~点击

A class with too many methods indicates it does too many things and is hard to read and understand. It usually contains complicated code, and should be refactored.

简单解释:一个类有太多的方法,证明他做了太多的事儿,不利于理解。应该考虑重构。考虑单一职责。

    class c {
        int a();
        int b();
        int c();
        // ...
        int l();
        int m();
        int n();
        // ...
        int x();
        int y();
        int z();
        int aa();
        int ab();
        int ac();
        int ad();
        int ae();
    };
10、too many parameters

      Since:0.7 定义类传送门~点击

Methods with too many parameters are hard to understand and maintain, and are thirsty for refactorings, like Replace Parameter With method, Introduce Parameter Object, or Preserve Whole Object.

简单解释: 一个方法中参数过多。

    void example(int a, int b, int c, int d, int e, int f,
        int g, int h, int i, int j, int k, int l) {
    }

TOO_MANY_PARAMETERS The reporting threshold for too many parameters, default value is 10.

References:
Fowler, Martin (1999). Refactoring: Improving the design of existing code. Addison Wesley.

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值