oclint 基本規則介紹 Basic

Basic

條件位運算BitwiseOperatorInConditional


Since: 0.6

位運算是不利於後期維護和理解的,雖然有時候這樣計算速度比較快

定義類: oclint-rules/rules/basic/BitwiseOperatorInConditionalRule.cpp

Example:

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

錯誤Null檢查BrokenNullCheck

Since: 0.7

錯誤的空檢查會讓程序崩潰

定義類: oclint-rules/rules/basic/BrokenNullCheckRule.cpp

Example:

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

    if (a == NULL && a->bar(b))
    {
    }
}

錯誤Nil檢查BrokenNilCheck

Since: 0.7

在某些情況下,在Objective-C中破零檢查返回結果剛好相反。

定義類: oclint-rules/rules/basic/BrokenNullCheckRule.cpp

Example:

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

    if (!obj1 && ![obj1 isEqualTo:obj2])
    {
    }
}

BrokenOddnessCheck

Since: 0.6

檢查 x % 2 == 1 如果是負數,就會出現異常. 應該使用 x & 1 == 1, or x % 2 != 0 替換.

定義類: oclint-rules/rules/basic/BrokenOddnessCheckRule.cpp

Example:

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

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

可以合併的IF語句 CollapsibleIfStatements

Since: 0.6

可以合併的連續兩個if,應該合併,提高代碼的可讀性.

定義類: oclint-rules/rules/basic/CollapsibleIfStatementsRule.cpp

Example:

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

恆定條件運算 ConstantConditionalOperator

Since: 0.6

條件運算符 結果已經確定的條件運算.

定義類: oclint-rules/rules/basic/ConstantConditionalOperatorRule.cpp

Example:

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

IF表達式爲確定ConstantIfExpression

Since: 0.2

if 表達式的條件是已經確定的true 或者else 

定義類: oclint-rules/rules/basic/ConstantIfExpressionRule.cpp

Example:

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

無效代碼DeadCode

Since: 0.4

在 returnbreakcontinue, and throw 之後的代碼都是無效的

定義類: oclint-rules/rules/basic/DeadCodeRule.cpp

Example:

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

雙重否定 DoubleNegative

Since: 0.6

使用雙重否定沒有意義

定義類: oclint-rules/rules/basic/DoubleNegativeRule.cpp

Example:

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

For應該轉換爲While ForLoopShouldBeWhileLoop

Since: 0.6

一些情況下For循環應該轉換爲While,使代碼更簡潔

定義類: oclint-rules/rules/basic/ForLoopShouldBeWhileLoopRule.cpp

Example:

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

Goto語句 GotoStatement

Since: 0.6

“Go To 被認爲是有害的”

定義類: oclint-rules/rules/basic/GotoStatementRule.cpp

Example:

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

References:

Edsger Dijkstra (March 1968). “Go To Statement Considered Harmful”Communications of the ACM (PDF) 11 (3): 147–148. doi:10.1145/362929.362947.

混亂的增量 JumbledIncrementer

Since: 0.7

混亂的增量計算,使代碼很難閱讀

定義類: oclint-rules/rules/basic/JumbledIncrementerRule.cpp

Example:

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

錯位的Null檢查 MisplacedNullCheck

Since: 0.7

(空檢查應該在其他運算之前)The null check is misplaced. In C and C++, sending a message to a null pointer could crash the app. When null is misplaced, either the check is useless or it’s incorrect.

This rule is defined by the following class: oclint-rules/rules/basic/MisplacedNullCheckRule.cpp

Example:

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

    if (a->bar(b) || !a)        // violation
    {
    }
}

錯位的Nil檢查 MisplacedNilCheck

Since: 0.7

(nil檢查應該在其他運算之前)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.

This rule is defined by the following class: oclint-rules/rules/basic/MisplacedNilCheckRule.cpp

Example:

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

    if (![obj1 isEqualTo:obj2] || obj1 == nil)
    {
    }
}

多餘運算符 MultipleUnaryOperator

Since: 0.6

多餘的運算符應該簡化,便於閱讀

This rule is defined by the following class: oclint-rules/rules/basic/MultipleUnaryOperatorRule.cpp

Example:

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

從Finally 返回 ReturnFromFinallyBlock

Since: 0.6

不建議從Finally 返回

定義類: oclint-rules/rules/basic/ReturnFromFinallyBlockRule.cpp

Example:

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

Finally拋出異常 ThrowExceptionFromFinallyBlock

Since: 0.6

從Finally塊拋出異常,可能掩蓋其他的錯誤

定義類: oclint-rules/rules/basic/ThrowExceptionFromFinallyBlockRule.cpp

Example:

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
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章