Цикломатическая сложность - нарисуйте блок-схему управления для этого оператора Java

Может кто-нибудь помочь с этим?

while (x > level)
    x = x – 1;
x = 0

1 ответ

Решение

Цикломатическая сложность может быть рассчитана по формуле, приведенной здесь.

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++ и т. д. Тем не менее, определение условий должно быть тщательно выполнено, как показано здесь:

- 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.
Другие вопросы по тегам