oclint 基本规则介绍 Basic

11 篇文章 0 订阅
1 篇文章 0 订阅

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
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Basic.Qos 方法是 RabbitMQ 提供的一种 Quality of Service(服务质量)机制,用于控制消费者在从 RabbitMQ 中获取消息时的预取数量和速率。通过使用 Basic.Qos 方法,可以提高消费者的处理能力,减少消息的积压和堆积。 Basic.Qos 方法的语法如下: ```java void Basic.Qos(uint prefetchSize, ushort prefetchCount, bool global) ``` 其中: - prefetchSize:预取大小,表示消费者从 RabbitMQ 中预取的消息的总大小。建议将其设置为 0,表示忽略该参数。 - prefetchCount:预取数量,表示消费者从 RabbitMQ 中预取的消息的数量。建议将其设置为 1,表示每次只从队列中获取一条消息。 - global:是否对整个连接进行限制,如果设置为 true,则表示对整个连接的所有通道都生效,否则只对当前通道生效。 使用 Basic.Qos 方法时,需要注意以下几点: 1. Basic.Qos 方法只对当前通道生效,如果需要对多个通道生效,需要在每个通道上都调用该方法。 2. prefetchCount 参数设置得太大会导致消费者一次性获取过多的消息,从而导致消息堆积和积压。因此,建议将 prefetchCount 参数设置为 1,表示每次只从队列中获取一条消息。 3. Basic.Qos 方法只对消费者生效,对生产者没有影响。因此,如果需要控制生产者的速率,需要使用其他机制,比如发送消息时设置发送速率或者使用发送方确认机制等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值