1
A
回答
1
環複雜度可以使用提供的公式here來計算。
Cyclomatic complexity = E - N + P
where,
E = number of edges in the flow graph.
N = number of nodes in the flow graph.
P = number of nodes that have exit points
對於你的情況,該圖應該是這樣的:
--------------- ----------
| x > level |----- NO ------>| x = x-1|
|-------------| ----|-----
| |---------------------
|
Yes
|
-------|----------
| End while (if) |
-------|----------
|
|
---------
| x = 0 |
----------
(非ASCII藝術人)
因此,cyclomatic complexity
應該是:
E = 4, N = 4, P = 2 => Complexity = 4 - 4 + 2 = 2
[編輯] Ira Baxter
指出很好如何簡化這種計算的語言,如Java
,C#
,C++
等。然而,識別條件語句必須小心,如圖所示here:
- Start with a count of one for the method.
- Add one for each of the following flow-related elements that are found in the method.
Returns - Each return that isn't the last statement of a method.
Selection - if, else, case, default.
Loops - for, while, do-while, break, and continue.
Operators - &&, ||, ?, and :
Exceptions - catch, finally, throw, or throws clause.
相關問題
- 1. 將此重複語句變爲循環
- 2. 控制複雜流程nodejs
- 3. 繪製控制流程圖
- 4. 控制流程圖和圈複雜度
- 5. try/catch語句和循環控制
- 6. Java循環控制
- 7. VS2010「空」控制流語句
- 8. 循環和控制流程
- 9. java爲空語句循環
- 10. if語句的嵌套循環的複雜性
- 11. Java if-else控制語句
- 12. 雙循環的複雜性
- 13. 迭代循環 - 複雜性
- 14. gnuplot - 繪製循環
- 15. 先進控制語句
- 16. 在D3中繪製複雜的圖形
- 17. 在複雜平面上繪製圖形
- 18. 三重循環的複雜性
- 19. 在繪製循環中繪製精靈
- 20. Java控制流圖庫
- 21. 冗餘控制流跳轉語句
- 22. 複雜循環
- 23. 使用java繪製循環的形狀
- 24. for循環的控制流程
- 25. php循環和控制流程
- 26. Ansible劇本循環控制流程
- 27. VBA嵌套循環流量控制
- 28. 複製/克隆流在一個循環...
- 29. For循環/ if語句java
- 30. if語句while循環JAVA
而對於像Java結構語言,你可以計算它作爲#條件加1.(1 + 1 ==> 2爲這個例子)。 –
考慮到休息和繼續總是伴隨着一個條件(否則循環立即退出),你不會只計算條件? – nonconvergent
@nonconvergent - 我想'如果'+'break'算作一個,而不是兩個。如果我在OP中重寫while while(true).. if(x> = level)break',根據規則我會得到+2('if'和'break')。我的感覺是'if .. break'和虛擬'while''應該是例外,因爲複雜性似乎是相同的。無論如何,我不太清楚這對於Programmers.SE來說是個好問題。 – Alexei