OCLint的部分規則(Basic 部分)

OCLint的部分規則(Basic 部分)

對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、bitwise operator in conditional

      since:0.6 定義類傳送門~點擊

Checks for bitwise operations in conditionals. Although being written on purpose in some rare cases, bitwise opera- tions are considered to be too “smart”. Smart code is not easy to understand.

簡單解釋:OCLint認爲位運算符可讀性不好

    void example(int a, int b) {
        if (a | b) {
        }
        if (a & b) {
        }
     }

2、broken null check

      since:0.7 定義類傳送門~點擊

The broken null check itself will crash the program.

簡單解釋:錯誤的null檢查會導致程序crash

    void m(A *a, B *b) {
        if (a != NULL || a->bar(b))
        {
        }
        if (a == NULL && a->bar(b))
        {
        }
    }   

PS:OC(objective-c)向空對象可以發消息,不會發生crash,但是上面這樣的代碼的邏輯是錯誤的。

3、broken nil check

      since:0.7 定義類傳送門~點擊

The broken nil check in Objective-C in some cases returns just the opposite result.

簡單解釋:Objective-C中判空,有些情況下會返回預期相反的結果

    + (void)compare:(A *)obj1 withOther:(A *)obj2 {
        if (obj1 || [obj1 isEqualTo:obj2])
        {
        }
        if (!obj1 && ![obj1 isEqualTo:obj2])
        {
        }
    }

PS:這樣寫應該是邏輯錯誤

4、broken oddness check

      since:0.6 定義類傳送門~點擊

Checking oddness by x % 2 == 1won’t work for negative numbers. Use x & 1 == 1,or x % 2 != 0 instead.

簡單解釋:對數字奇數性進行檢查(x % 2 == 1),如果是負數,會有異常,應使用x & 1 == 1或者x % 2 != 0

    void example() {
        if (x % 2 == 1)         // violation
        {
        }
        if (foo() % 2 == 1)     // violation
        {
        }
    }

PS:在Objective-C 上的測試結果上看,並沒有什麼大的異常。應該主要的問題是負數進行取模區的結果不會出現正值。
比如(x % 2)的結果是-1或者0

5、collapsible if statements

      Since: 0.6 定義類傳送門~點擊

This rule detects instances where the conditions of two consecutive if statements can be combined into one in order to increase code cleanness and readability.

簡單解釋:兩個連續if條件可以合併的應該合併,可以提高可讀性和代碼整潔。

    void example(bool x, bool y){
        if (x)              // these two if statements can be
        {
            if (y)          // combined to if (x && y)
            {
                foo();
            }
        }
    }

6、constant conditional operator

      Since: 0.6 定義類傳送門~點擊

conditional operator whose conditionals are always true or always false are confusing.

簡單解釋:檢查始終爲true或者false的操作,會使人疑惑。

    void example() {
        int a = 1 == 1 ? 1 : 0;     // 1 == 1 is actually always true
    }

7、constant if expression

      Since: 0.2 定義類傳送門~點擊

if statements whose conditionals are always true or always false are confusing.

簡單解釋:if始終爲true或者false的操作,會使人疑惑。

    void example(){
        if (true) {      // always true
            foo();
        }
        if (1 == 0) {    // always false
            bar();
        }
    }

8、 dead code

      Since: 0.4 定義類傳送門~點擊

Code after return, break, continue, and throw statements is unreachable and will never be executed.

簡單解釋:在 returnbreakcontinue and throw 之後的代碼都是無效的

    void example(id collection) {
        for (id it in collection) {
            continue;
            int i1;                 // dead code
        }
        return;
        int i2;                     // dead code
    }

9、double negative

      Since:0.6 定義類傳送門~點擊

There is no point in using a double negative, it is always positive.

簡單解釋:代碼中雙重否定沒有意義

    void example() {
        int b1 = !!1;
        int b2 = ~~1;
    }

10、for loop should be while loop

      Since:0.6 定義類傳送門~點擊

Under certain circumstances, some for loops can be simplified to while loops to make code more concise.

簡單解釋:在有些情況下使用while比使用for更簡潔

    void example(int a) {
        for (; a < 100;) {
            foo(a);
        }
    }

11、 goto statement

      Since:0.6 定義類傳送門~點擊

Go To Statement Considered Harmful

簡單解釋:go to被認爲有害的

    void example() {
        A:
            a();
        goto A;     // Considered Harmful
    }

12、jumbled incrementer

      Since:0.7 定義類傳送門~點擊

Jumbled incrementers are usually typos. If it’s done on purpose, it’s very confusing for code readers.

簡單解釋:混亂的迭代器通常會出現錯誤,可讀性不好。

    void aMethod(int a) {
        for (int i = 0; i < a; i++) {
            for (int j = 0; j < a; i++) {     // references both 'i' and 'j'
            }
        }
    }

13、misplaced null check

      Since:0.7 定義類傳送門~點擊

The null check is misplaced. In C and C++, sending a message to a null pointer could crash the program. When null is misplaced, either the check is useless or it’s incorrect.

簡單解釋:CC++在條件判斷的時候注意null判斷放在前面,邏輯短路

    void m(A *a, B *b) {
        if (a->bar(b) && a != NULL) { // violation 
        }
        if (a->bar(b) || !a) {        // violation
        }
    }

14、misplaced nil check

      Since:0.7 定義類傳送門~點擊

The nil check is misplaced. In Objective-C, sending a message to a nil pointer simply does nothing. But code readers may be confused about the misplaced nil check.

簡單解釋:Objective-C在條件判斷的時候注意nil判斷放在前面,邏輯短路

    + (void)compare:(A *)obj1 withOther:(A *)obj2 {
        if ([obj1 isEqualTo:obj2] && obj1) {
        }
        if (![obj1 isEqualTo:obj2] || obj1 == nil)   {
        }
    }

15、 multiple unary operator

      Since:0.6 定義類傳送門~點擊

Multiple unary operator can always be confusing and should be simplified.

簡單解釋:多重的運算符不易理解,應該簡化。

    void example() {
        int b = -(+(!(~1)));
    }

16、return from finally block

      Since:0.6 定義類傳送門~點擊

Returning from a finally block is not recommended.

簡單解釋:不建議在finally中返回

    void example() {
        @try {
            foo();
        }
        @catch(id ex) {
            bar();
        }
        @finally {
            return;         // this can discard exceptions.
        }
    }

17、throw exception from finally block

      Since:0.6 定義類傳送門~點擊

Throwing exceptions within a finally block may mask other exceptions or code defects.

簡單解釋:從finally塊拋出異常,可能忽略其他的錯誤

    void example() {
        @try {;}
        @catch(id ex) {;}
        @finally {
            id ex1;
            @throw ex1;                              // this throws an exception
            NSException *ex2 = [NSException new];
            [ex2 raise];                             // this throws an exception, too
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章