oclint规则 Convention(公约)

本文介绍了一系列C++编码规范,包括避免在循环末尾使用分支语句、确保基类析构函数为虚函数或受保护、覆盖完全的Switch语句不需要默认情况等。这些规则有助于提高代码质量和可维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Convention

循环最后避免分支 AvoidBranchingStatementAsLastInLoop

Since: 0.7

在循环最后加入分支,理解起来会比较混乱,并且很有可能因为忘记而引起一些错误

This rule is defined by the following class: oclint-rules/rules/convention/AvoidBranchingStatementAsLastInLoopRule.cpp

Example:

void example()
{
    for (int i = 0; i < 10; i++)
    {
        if (foo(i))
        {
            continue;
        }
        break;      // this break is confusing
    }
}

BaseClassDestructorShouldBeVirtualOrProtected

Since: 0.10.2

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

定义类: oclint-rules/rules/convention/BaseClassDestructorShouldBeVirtualOrProtectedRule.cpp

Example:

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

References:

Sutter & Alexandrescu (November 2004). “C++ Coding Standards: 101 Rules, Guidelines, and Best Practices”.Addison-Wesley Professional

全覆盖的Switch不需要默认值 CoveredSwitchStatementsDontNeedDefault

Since: 0.8

一个全覆盖的Switch语句不应该有默认值设置。

定义类: oclint-rules/rules/convention/CoveredSwitchStatementsDontNeedDefaultRule.cpp

Example:

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.
    }
}

默认值没有在Switch的最后 DefaultLabelNotLastInSwitchStatement

Since: 0.6

默认值应该在switch语句的最后,不然代码会很混乱

定义类: oclint-rules/rules/convention/DefaultLabelNotLastInSwitchStatementRule.cpp

Example:

void example(int a)
{
    switch (a) {
        case 1:
            break;
        default:  // the default case should be last
            break;
        case 2:
            break;
    }
}

虚拟类的析构函数 DestructorOfVirtualClass

Since: 0.8

这条规则要求虚拟类的析构函数必须是虚拟的

This rule is defined by the following class: oclint-rules/rules/convention/DestructorOfVirtualClassRule.cpp

Example:

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
};

倒置逻辑 InvertedLogic

Since: 0.4

倒置逻辑让程序不容易理解

定义类: oclint-rules/rules/convention/InvertedLogicRule.cpp

Example:

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;
}

Switch语句丢失Break MissingBreakInSwitchStatement

Since: 0.6

一个Switch语句中没有Break 很可能是一个错误。

定义类: oclint-rules/rules/convention/MissingBreakInSwitchStatementRule.cpp

Example:

void example(int a)
{
    switch (a) {
        case 1:
            break;
        case 2:
            // do something
        default:
            break;
    }
}

Switch中不是case NonCaseLabelInSwitchStatement

Since: 0.6

标签作为Switch语句的一部分是无法让人理解。

定义类: oclint-rules/rules/convention/NonCaseLabelInSwitchStatementRule.cpp

Example:

void example(int a)
{
    switch (a) {
        case 1:
            break;
        label1:     // label in a switch statement in really confusing
            break;
        default:
            break;
    }
}

IvarAssignmentOutsideAccessorsOrInit

Since: 0.8

(这个规则限制外部访问其直接访问或者初始化)This rule prevents assigning an ivar outside of getters, setters, and init method.

定义类: oclint-rules/rules/convention/ObjCAssignIvarOutsideAccessorsRule.cpp

Example:

@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

参数值重置 ParameterReassignment

Since: 0.6

大多数情况下,参数重新设置值,是有问题的。

定义类: oclint-rules/rules/convention/ParameterReassignmentRule.cpp

Example:

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

用短判断退出然后继续执行UseEarlyExitsAndContinue

Since: 0.8

使用较短的语句退出,可以让代码阅读更容易理解。不用记住之前很长的判断

定义类: oclint-rules/rules/convention/PreferEarlyExitRule.cpp

Example:

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 ....
}

Switch 语句应该有一个默认值 SwitchStatementsShouldHaveDefault

Since: 0.6

Switch 语句应该有一个默认值

定义类: oclint-rules/rules/convention/SwitchStatementsShouldHaveDefaultRule.cpp

Example:

void example(int a)
{
    switch (a) {
        case 1:
            break;
        case 2:
            break;
        // should have a default
    }
}

Switch分支语句太少 TooFewBranchesInSwitchStatement

Since: 0.6

如果Switch语句只有很少的分支应该使用if else 语句

定义类: oclint-rules/rules/convention/TooFewBranchesInSwitchStatementRule.cpp

Example:

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, default value is 3.
默认值是3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值