About cyclomatic complexity

background

――When I was coding at work, my superior pointed out the complexity. ――I didn't know the word cyclomatic complexity, but I knew that somehow a bug might be hidden. ――Because it's a big deal, I investigated the cyclomatic complexity ――For people who are as inexperienced as me

What is cyclomatic complexity?

I checked it for the time being ...

Cyclomatic complexity is a type of software metric. Developed by Thomas McCabe, it is used to measure the complexity of a program. Count the number of linearly independent paths directly from the program source code. [wikipedia](https://ja.wikipedia.org/wiki/%E5%BE%AA%E7%92%B0%E7%9A%84%E8%A4%87%E9%9B%91%E5%BA % A6)

in short

--Calculate how many branches such as if / else, for statement, switch statement are included in the code ――The magnitude of the number tells you how easy it is to mix bugs.

Specific counting method

--"1" for programs with no branches --Add "1" every time there is a branch (if / else, for statement, switch statement, while statement)

General idea

There seem to be various theories, but ...

Cyclomatic complexity Status
1-10 Easy to test with secure code
11-20 A little complicated code
21-40 Complex code makes testing difficult
41 or more Bad guy. Untestable

If you write it normally, I think it's hard to exceed 10. (In my case, I once measured 20 in business logic)

Actually think using an example

I prepared a suitable service below. Since it is just an example, the process itself has no meaning, so please ignore it.

Service.java



private String familyService(List<String> nameList) {
    String id;
    for (String name : nameList) {
        if (name.startsWith("a")){
            id = "a";
        } else if (name.startsWith("b")){
            id = "b";
        } else if (name.startsWith("c")){
            id = "c";
        } else if (name.startsWith("d")){
            id = "d";
        } else if (name.startsWith("e")){
            id = "e";
        } else if (name.startsWith("f")){
            id = "f";
        } else if (name.startsWith("g")){
            id = "g";
        } else if (name.startsWith("h")){
            id = "h";
        } else {
            id = "xxxx";
        }
    }
    return id;
}
      

In the above example, there is one for statement and nine if / else branches, so 1 + 1 + 9 gives a complexity of 11.

Countermeasures

I think there are several ways to reduce cyclomatic complexity.

--Review the condition of the if statement --Cut out what can be shared --Cut out as a new class or method --Review method responsibilities

I think there are many ways to do it, so I think it's more important to be aware of cyclomatic complexity when coding on a regular basis. I personally have been able to calculate it visually thanks to my research on cyclomatic complexity, but I don't think everyone is, so I would like to introduce the features of IntelliJ IDEA for those people. Put.

What you have prepared

How to do

--IntelliJ >> Select preference スクリーンショット 2018-10-14 12.55.49.png

--Search for "overly complex" スクリーンショット 2018-10-14 12.58.12.png

--Set for each class / method If you exceed the set value, you will get angry like this. スクリーンショット 2018-10-14 13.01.39.png

Summary

--The idea of cyclomatic complexity is simple ――Once you understand it, it will be useful for refactoring and review timing.

Referenced site

[Cyclomatic complexity-Wikipedia](https://ja.wikipedia.org/wiki/%E5%BE%AA%E7%92%B0%E7%9A%84%E8%A4%87%E9%9B% 91% E5% BA% A6) Learn Mccabe's Cyclomatic Complexity with Example

Recommended Posts

About cyclomatic complexity
Cohesion / Coupling / Cyclomatic complexity
About =
About method.invoke
About Kotlin
About attr_accessor
About Hinemos
About inheritance
About params
About Docker
About Rails 6
About form_for
About Spring ③
About enum
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About JAVA_HOME
About active_hash
About static
About exceptions
About scope
[Maven] About Maven