Size¶
高圈複雜度HighCyclomaticComplexity¶
Since: 0.4
圈複雜度(CyclomaticComplexity)是很常用的一種度量軟件代碼複雜程度的標準。這裏所指的“代碼複雜程度”並非軟件內在業務邏輯的複雜程度,而是指代碼的實現方式的複雜程度。有很多研究統計指出,圈複雜度高的代碼中錯誤的可能性更大,維護的成本更高。
非常非常簡單的說它就是統計一個函數有多少個分支(if,
while,for,等等),沒有的話複雜度爲一,每增加一個分支複雜度加一。讓很多人不解的是,無論這些分支是並列還是嵌套,統統都是加一。
圈複雜度的作者McCabe,經過試驗認爲,圈複雜度在3~~7比較好的結構化方法。他還認爲10是圈複雜度的一個合理化上限。
定義類: oclint-rules/rules/size/CyclomaticComplexityRule.cpp
Example:
void example(int a, int b, int c) // 1
{
if (a == b) // 2
{
if (b == c) // 3
{
}
else if (a == c) // 3
{
}
else
{
}
}
for (int i = 0; i < c; i++) // 4
{
}
switch(c)
{
case 1: // 5
break;
case 2: // 6
break;
default: // 7
break;
}
}
Thresholds:
- CYCLOMATIC_COMPLEXITY
- 圈複雜度閥值, 默認 10.
Suppress:
__attribute__((annotate("oclint:suppress[high cyclomatic complexity]")))
References:
McCabe (December 1976). “A Complexity Measure”. IEEE Transactions on Software Engineering: 308–320
太長的類 LongClass¶
Since: 0.6
複雜的事兒應該拆解分段去解決,降低複雜度,以及耦合.
定義類: oclint-rules/rules/size/LongClassRule.cpp
Example:
class Foo
{
void bar()
{
// 1001 lines of code
}
}
Thresholds:
- LONG_CLASS
- 類長度閥值, 默認行數 1000.
太長的行LongLine¶
Since: 0.6
一行代碼太長,非常不利於閱讀和維護,應該將代碼轉換爲多行.
定義類: oclint-rules/rules/size/LongLineRule.cpp
Example:
void example()
{
int a012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789;
}
Thresholds:
- LONG_LINE
- 行長度閥值,默認值 100.
太長的方法 LongMethod¶
Since: 0.4
一個方法不應該太長,太長的方法影響後續的閱讀,應該將每個方法都去做一個簡單的事。
This rule is defined by the following class: oclint-rules/rules/size/LongMethodRule.cpp
Example:
void example()
{
cout << "hello world";
cout << "hello world";
// repeat 48 times
}
Thresholds:
- LONG_METHOD
- 方法長度閥值, 默認 50.
方法有效代碼行太高HighNcssMethod¶
Since: 0.6
有效代碼行,系統只計算實際的有效語句。空語句,空塊,右括號或分號後的右括號會被忽略。一個被分解成多行的語句只計算一行。
定義類: oclint-rules/rules/size/NcssMethodCountRule.cpp
Example:
void example() // 1
{
if (1) // 2
{
}
else // 3
{
}
}
Thresholds:
- NCSS_METHOD
- 有效行代碼閥值, 默認 30.
Suppress:
__attribute__((annotate("oclint:suppress[high ncss method]")))
嵌套塊太深DeepNestedBlock¶
Since: 0.6
檢查嵌套塊是否超過指定的深度值.
This rule is defined by the following class: oclint-rules/rules/size/NestedBlockDepthRule.cpp
Example:
if (1)
{ // 1
{ // 2
{ // 3
}
}
}
Thresholds:
- NESTED_BLOCK_DEPTH
- 深度閥值,默認值 5.
高Npath複雜性 HighNPathComplexity¶
Since: 0.4
NPath複雜度是一個方法中各種可能的執行路徑總和
一般把200作爲考慮降低複雜度的臨界點
定義類: oclint-rules/rules/size/NPathComplexityRule.cpp
Example:
void example()
{
// complicated code that is hard to understand
}
Thresholds:
- NPATH_COMPLEXITY
- Npath複雜度閥值, 默認 200.
Suppress:
__attribute__((annotate("oclint:suppress[high npath complexity]")))
References:
Brian A. Nejmeh (1988). “NPATH: a measure of execution path complexity and its applications”. Communications of the ACM 31 (2) p. 188-200
太多字段 TooManyFields¶
Since: 0.7
一個類定義太多的字段,表明它做了太多的事兒,應該去做抽象,重新設計.
定義類: oclint-rules/rules/size/TooManyFieldsRule.cpp
Example:
class c
{
int a, b;
int c;
// ...
int l;
int m, n;
// ...
int x, y, z;
void m() {}
};
Thresholds:
- TOO_MANY_FIELDS
- 最多字段閥值, 默認 20.
太多方法 TooManyMethods¶
Since: 0.7
一個類有太多的方法,證明他做了太多的事兒,不利於理解。應該考慮重構。
定義類: oclint-rules/rules/size/TooManyMethodsRule.cpp
Example:
class c
{
int a();
int b();
int c();
// ...
int l();
int m();
int n();
// ...
int x();
int y();
int z();
int aa();
int ab();
int ac();
int ad();
int ae();
};
Thresholds:
- TOO_MANY_METHODS
- 最多方法閥值, 默認 30.
太多參數 TooManyParameters¶
Since: 0.7
方法中有太多的參數是難以理解和後期維護的。應該嘗試重構,或者使用 方法替換參數, 對象替換參數, or 使用完整對象.
定義類: oclint-rules/rules/size/TooManyParametersRule.cpp
Example:
void example(int a, int b, int c, int d, int e, int f,
int g, int h, int i, int j, int k, int l)
{
}
Thresholds:
- TOO_MANY_PARAMETERS
- 方法參數閥值, 默認 10.
References:
Fowler, Martin (1999). Refactoring: Improving the design of existing code. Addison Wesley.