What are Java metrics? _Memo_20200818


** 1. What are metrics **

** Useful metrics **

Metrics are the classes and methods that make up a model, the relationships between classes, the relationships between packages, etc. It is measured from the viewpoint of complexity, coupling, cohesion, etc. and expressed as a numerical value.

There are numerous types of metrics used in software development, including minor ones. However, even if you study too advanced metrics, it is difficult to use them well in practice. Therefore, in this material, we will focus on three useful indicators, "LOC", "CC", and "WMC". Even if you remember a lot, you will forget it if you don't have a chance to use it.

** Advantages and disadvantages of using metrics **

Code size and complexity, cohesion (how scattered elements are related to each other in the code), Measure the degree of coupling (high degree of independence of classes and packages) using a dedicated tool, You can set certain goals and make improvements.

There are so many that it takes time to remember


** You can roughly grasp the scale LOC **

** LOC (Line of Code) **, as the name implies, represents the number of lines in the program. For example, for a program with 200 lines of content, the LOC is 200. However, it does not count lines that are not related to the essence of the program, such as blank lines or lines with only parentheses. There are several ways to count.

Also, if the number of lines is large, KLOC may be used, which counts in units of 1000 lines. (80,000 lines of program is 80KLOC) * You do not need to remember this material.

** Features of LOC **

It is not suitable for strict operation because it is a metric that easily increases or decreases depending on how to write and count the code. Let's just divide it into ** a tool to get a rough idea of the scale **.


** CC showing method complexity **

** CC (Cyclomatic Complexity) ** is a metric developed by a person named Thomas McCabe. Also known as "McCabe's ** Cyclomatic Complexity **". CC is a number greater than or equal to 1 that describes how complex the content of a particular method is. The larger it is, the more complicated it is.

Specifically, the higher the number is, the more branches by if and loops by for are used. Methods that are too complex are hard to read, prone to bugs, and difficult to fix later. For each method under development, check if CC is suppressed above a certain level, If the CC is too large, it may be better to fix it.

** CC features **

The higher it is, the higher the probability that bugs will be mixed in, so it may be used when fixing I can think of it.


** WMC showing class complexity **

** WMC (Weighted Method Per Class) ** is the sum of CCs of all methods included in a class. Represents the complexity of the class. It seems better to consider whether a class whose WMC shows an abnormally large value can be divided into multiple classes.

Correspondence when WMC is large

-Split the function of the method

・ Consider whether the structure can be simplified

** Features of WMC **

It is the total value of CC, so the larger it is, the more you are developing You can see if it is too complicated or difficult to read.

  • If you don't care, please skip it.
    My impression

When I was actually entrusted with manufacturing at the development site, I decided to pay attention to the metrics as well.

Recommended Posts