OCLint的部分规则(Convention 部分)

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

OCLint的部分规则(Convention 部分)

对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、avoid branching statement as last in loop

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

Having branching statement as the last statement inside a loop is very confusing, and could largely be forgetting of something and turning into a bug.

简单解释:不要再循环最后加入分支,负责理解起来比较困难,很大程度上会遗忘一些事情,导致一些错误。

    void example() {
        for (int i = 0; i < 10; i++) {
            if (foo(i)) {
                continue;
            }
            break;      // this break is confusing
        }
    }
2、base class destructor should be virtual or protected

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

Make base class destructor public and virtual, or protected and nonvirtual

简单解释:基类的析构函数需要是publicvirtual或者protected``nonvirtual

    class Base {
    public:
        ~Base(); // this should be either protected or virtual
    }
    class C : public Base {
        virtual ~C();
    }

Sutter & Alexandrescu (November 2004). [“C++ Coding Standards: 101 Rules, Guidelines, and Best Practices”(http://gotw.ca/publications/c++cs.htm)]. Addison-Wesley Professional

3、unnecessary default statement in covered switch statement

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

When a switch statement covers all possible cases, a default label is not needed and should be removed. If the switch is not fully covered, the SwitchStatements Should Have Default rule will report.

简单解释:如果switch覆盖了所有的条件,default是不需要的应该被移除。如果不是default还是需要的。

    typedef enum {
        value1 = 0,
        value2 = 1
    } eValues;
    //
    void aMethod(eValues a)
    {
        switch(a)
        {
            case value1:
                break;
            case value2:
                break;
            default:          // this break is obsolete because all
                break;        // values of variable a are already covered.
        }
    }
4、 ill-placed default label in switch statement

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

It is very confusing when default label is not the last label in a switch statement.

简单解释:default应该在switch的最后,负责会很难理解。

    void example(int a) {
        switch (a) {
            case 1:
                break;
            default:  // the default case should be last
                break;
            case 2:
                break;
        }
    }
5、destructor of virtual class

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

This rule enforces the destructor of a virtual class must be virtual.

简单解释:这个规则是虚拟类的析构函数必须是虚拟的。

    class Base { // class Base should have a virtual destructor ~Base()
        public: virtual void f();
    };
    class Child : public Base {
        public: ~Child();  // destructor ~Child() should be virtual
    };
6、inverted logic

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

An inverted logic is hard to understand.

简单解释:倒置逻辑不易理解。

    int example(int a) {
        int i;
        if (a != 0)             // if (a == 0)
        {                       // {
            i = 1;              //      i = 0;
        }                       // }
        else                    // else
        {                       // {
            i = 0;              //      i = 1;
        }                       // }
        return !i ? -1 : 1;     // return i ? 1 : -1;
    }

PS:在做判断的时候,应该先做的判断,在做的判断。做个一个测试如果只有非的测试是不会有警告的。

    int example(int condition) {
        int temp;
        if (acondition!= 0)            //不会有警告
        {                       
            temp = 1;            
        }                  
    }
7、missing break in switch statement

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

A switch statement without a break statement has a very large chance to contribute a bug.

简单解释:在switch语句中缺失了break,很有可能引发bug

    void example(int a) {
        switch (a) {
            case 1:
                break;
            case 2:
                // do something
            default:
                break;
        }
    }
8、non case label in switch statement       Since:0.6 定义类传送门~点击

It is very confusing when label becomes part of the switch statement.

简单解释:label出现在switch条件中不易理解。

    void example(int a) {
        switch (a) {
            case 1:
                break;
            label1:     // label in a switch statement in really confusing
                break;
            default:
                break;
        }
    }
9、 ivar assignment outside accessors or init       Since:0.8 定义类传送门~点击

This rule prevents assigning an ivar outside of getters, setters, and init method.

简单解释:检查某些成员的初始化不再getterssetters andinit method中。

    @interface Foo : NSObject {
        int _bar;
    }
    @property (assign, nonatomic) int bar;
    @end
    @implementation Foo
    @synthesize bar = _bar;
    - (void)doSomething {
        _bar = 3; // access _bar outside its getter, setter or init
    }
    @end

PS:简单和小伙伴讨论了下,感觉iOS开发并不是很适合。

10、parameter reassignment       Since:0.6 定义类传送门~点击

Reassigning values to parameters is very problematic in most cases.

简单解释:对参数进行重新赋值,在很多情况下是有问题的。

    void example(int a) {
        if (a < 0) {
            a = 0; // reassign parameter a to 0
        }
    }

PS:简单测试了传值时使用指针,经测试不会有警告。也就是说不会对指针类型的参数做检查。

11、prefer early exits and continue       Since:0.8 定义类传送门~点击

Early exits can reduce the indentation of a block of code, so that reader do not have to remember all the previous decisions, therefore, makes it easier to understand the code.

简单解释:在有退出语句 的时候,应该让退出语句靠前,这样阅读代码使代码可以很好的被理解。

    int *doSomething(int a) {
      if (!foo(a) && bar(a) && doOtherThing(a)) {
        // ... some really long code ....
      }
      return 0;
    }
    // is preferred as
    int *doSomething(int a) {
      if (foo(a)) {
        return 0;
      }
      if (!bar(a)) {
        return 0;
      }
      if (!doOtherThing(a)) {
        return 0;
      }
      // ... some long code ....
    }
12、 missing default in switch statements       Since:0.6 定义类传送门~点击

Switch statements should have a default statement.

简单解释:检查 Switchdefault缺失的情况。

    void example(int a) {
        switch (a) {
            case 1:
                break;
            case 2:
                break;
            // should have a default
        }
    }
13、 too few branches in switch statement       Since:0.6 定义类传送门~点击

To increase code readability, when a switch consists of only a few branches, it’s much better to use an if statement instead.

简单解释:如果switch语句条件很少,可以一用if else 代替。

    void example(int a) {
        switch (a) {
            case 1:
                break;
            default:
                break;
        } // Better to use an if statement and check if variable a equals 1.
    }

Thresholds:
MINIMUM_CASES_IN_SWITCH The reporting threshold for count of case statements in a switch statement, de-
fault value is 3.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值