――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
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)
--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.
--"1" for programs with no branches --Add "1" every time there is a branch (if / else, for statement, switch statement, while statement)
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)
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.
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.
--IntelliJ >> Select preference
--Search for "overly complex"
--Set for each class / method If you exceed the set value, you will get angry like this.
--The idea of cyclomatic complexity is simple ――Once you understand it, it will be useful for refactoring and review timing.
[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