spm-goでGoの結合度を計測する

よいパッケージ性の測り方

## 結合度を計測する
コードの良さを測る指標として循環的複雑度などがありますがどちらかというと単一ファイルの指標で、アーキテクチャの良さを測る指標として不足に感じました。

マイクロサービスでもモジュラーモノリスでもマルチモジュールの境界をどううまく引くかは悩みどころで、機械的なソフトウェアメトリクスである程度あたりをつけられると便利です。

Goの結合度を測るツールとして[spm-go](https://github.com/fdaines/spm-go)の存在を知ったので動かしてみます。

## spm-goとMattermostの準備
spm-goをインストールします。helpが出ればインストール成功。

```bash
$ go version
go version go1.21.4 darwin/arm64
$ go install github.com/fdaines/spm-go@latest
$ spm-go
Software Package Metrics for Go

Usage:
  spm-go [command]

Available Commands:
  abstractness Analyzes abstractness of packages
  all          Displays all metric for packages
  dependencies Lists dependencies of each package
  distance     Analyzes distance from the main sequence
  help         Help about any command
  instability  Analyzes instability of packages
  packages     Lists packages

Flags:
  -f, --format string   Output format (default "console")
  -h, --help            help for spm-go
      --html            Generate HTML Output
  -v, --verbose         Verbose Output
      --version         version for spm-go
```

実例で計測した方がより実用性があるのでオープンソースのコラボレーションプラットフォームであるMattermostを今回使います。バックエンドのserverがGo製。

```bash
$ git clone git@github.com:mattermost/mattermost.git
$ cd mattermost/server
```

## ファイル数と依存の統計
pakcagesコマンドでファイル数を計測できます。

```bash
 $ spm-go packages
Packages analysis started.
Module: github.com/mattermost/mattermost/server/v8
126 packages found...
Gathering package metrics, please wait until the command is finished running.
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+-------+
|   # | PACKAGE                       | PATH                                                                                                                    | FILES |
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+-------+
|   1 | main                          | github.com/mattermost/mattermost/server/v8/build/docker-compose-generator                                               |     1 |
|   2 | api4                          | github.com/mattermost/mattermost/server/v8/channels/api4                                                                |    68 |
|   3 | app                           | github.com/mattermost/mattermost/server/v8/channels/app                                                                 |   116 |
|   4 | email                         | github.com/mattermost/mattermost/server/v8/channels/app/email                                                           |     6 |
|   5 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/email/mocks                                                     |     1 |
|   6 | featureflag                   | github.com/mattermost/mattermost/server/v8/channels/app/featureflag                                                     |     2 |
|   7 | imaging                       | github.com/mattermost/mattermost/server/v8/channels/app/imaging                                                         |     6 |
|   8 | imports                       | github.com/mattermost/mattermost/server/v8/channels/app/imports                                                         |     2 |
|   9 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/layer_generators                                                |     1 |
|  10 | oauthgitlab                   | github.com/mattermost/mattermost/server/v8/channels/app/oauthproviders/gitlab                                           |     1 |
|  11 | opentracing                   | github.com/mattermost/mattermost/server/v8/channels/app/opentracing                                                     |     1 |
|  12 | platform                      | github.com/mattermost/mattermost/server/v8/channels/app/platform                                                        |    25 |
|  13 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/platform/mocks                                                  |     1 |
|  14 | plugin_api_tests              | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests                                                |     1 |
|  15 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_hijack_plugin                 |     1 |
|  16 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_upgrade_websocket_plugin      |     1 |
|  17 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_defaults_plugin |     1 |
|  18 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_plugin          |     1 |
|  19 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_serve_metrics_plugin               |     1 |
|  20 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_bots_plugin                               |     1 |
|  21 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_call_log_api_plugin                       |     1 |
|  22 | main_test                     | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_db_driver                                 |     1 |
|  23 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_bundle_path_plugin                    |     1 |
|  24 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_channels_for_team_for_user_plugin     |     1 |
|  25 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_direct_channel_plugin                 |     1 |
|  26 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_plugin_status_plugin                  |     1 |
|  27 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_profile_image_plugin                  |     1 |
|  28 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_kv                                        |     1 |
|  29 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_member_channels_plugin                    |     1 |
|  30 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_members_plugin                            |     1 |
(後略)
```

dependenciesコマンドで依存数を計測できます。定義は以下です。

* INTERNALS: 現在のモジュール内の依存数
* EXTERNALS: 外部ソースへの依存数(例: github.com/spf13/cobra, golang.org/x/tools).
* STANDARD: Goの標準ライブラリーへの依存数(例: math, fmt, net/http).

```bash
 $ spm-go dependencies
Dependencies analysis started.
Module: github.com/mattermost/mattermost/server/v8
126 packages found...
Gathering package metrics, please wait until the command is finished running.
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+-----------+-----------+----------+-------+
|   # | PACKAGE                       | PATH                                                                                                                    | INTERNALS | EXTERNALS | STANDARD | TOTAL |
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+-----------+-----------+----------+-------+
|   1 | main                          | github.com/mattermost/mattermost/server/v8/build/docker-compose-generator                                               |         0 |         1 |        3 |     4 |
|   2 | api4                          | github.com/mattermost/mattermost/server/v8/channels/api4                                                                |        17 |        15 |       26 |    58 |
|   3 | app                           | github.com/mattermost/mattermost/server/v8/channels/app                                                                 |        56 |        32 |       54 |   142 |
|   4 | email                         | github.com/mattermost/mattermost/server/v8/channels/app/email                                                           |         5 |         7 |       14 |    26 |
|   5 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/email/mocks                                                     |         1 |         4 |        1 |     6 |
|   6 | featureflag                   | github.com/mattermost/mattermost/server/v8/channels/app/featureflag                                                     |         0 |         5 |        5 |    10 |
|   7 | imaging                       | github.com/mattermost/mattermost/server/v8/channels/app/imaging                                                         |         0 |         6 |       12 |    18 |
|   8 | imports                       | github.com/mattermost/mattermost/server/v8/channels/app/imports                                                         |         0 |         2 |        6 |     8 |
|   9 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/layer_generators                                                |         0 |         1 |       13 |    14 |
|  10 | oauthgitlab                   | github.com/mattermost/mattermost/server/v8/channels/app/oauthproviders/gitlab                                           |         1 |         3 |        5 |     9 |
|  11 | opentracing                   | github.com/mattermost/mattermost/server/v8/channels/app/opentracing                                                     |        11 |         8 |       10 |    29 |
|  12 | platform                      | github.com/mattermost/mattermost/server/v8/channels/app/platform                                                        |        17 |        12 |       29 |    58 |
|  13 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/platform/mocks                                                  |         0 |         3 |        0 |     3 |
|  14 | plugin_api_tests              | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests                                                |         0 |         0 |        1 |     1 |
|  15 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_hijack_plugin                 |         0 |         1 |        1 |     2 |
|  16 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_upgrade_websocket_plugin      |         0 |         3 |        2 |     5 |
|  17 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_defaults_plugin |         1 |         2 |        0 |     3 |
|  18 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_plugin          |         1 |         2 |        1 |     4 |
|  19 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_serve_metrics_plugin               |         0 |         1 |        1 |     2 |
|  20 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_bots_plugin                               |         1 |         2 |        0 |     3 |
|  21 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_call_log_api_plugin                       |         0 |         3 |        0 |     3 |
|  22 | main_test                     | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_db_driver                                 |         3 |         4 |        2 |     9 |
|  23 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_bundle_path_plugin                    |         1 |         2 |        2 |     5 |
|  24 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_channels_for_team_for_user_plugin     |         1 |         2 |        0 |     3 |
|  25 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_direct_channel_plugin                 |         1 |         2 |        0 |     3 |
|  26 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_plugin_status_plugin                  |         1 |         2 |        0 |     3 |
|  27 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_profile_image_plugin                  |         1 |         2 |        0 |     3 |
|  28 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_kv                                        |         1 |         2 |        2 |     5 |
|  29 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_member_channels_plugin                    |         1 |         2 |        0 |     3 |
|  30 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_members_plugin                            |         1 |         2 |        0 |     3 |
(後略)
```

## 不安定度(INSTABILITY)
instabilityコマンドで不安定度を測ります。定義は以下です。

* AFFERENT(Ca): 求心性結合。本来は外部から該当モジュールが依存されている数だが、モジュール内依存数で代替している模様
* EFFERENT(Ce): 遠心性結合。モジュールが使っている依存の数
* INSTABILITY: 不安定度。Ce / (Ce + Ca)。0に近い方がよく1に近い方が悪い。外部ライブラリーの変更に耐性があるかどうか。

```bash
 $ spm-go instability
Instability analysis started.
Module: github.com/mattermost/mattermost/server/v8
126 packages found...
Gathering package metrics, please wait until the command is finished running.
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+----------+----------+-------------+
|   # | PACKAGE                       | PATH                                                                                                                    | AFFERENT | EFFERENT | INSTABILITY |
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+----------+----------+-------------+
|   1 | main                          | github.com/mattermost/mattermost/server/v8/build/docker-compose-generator                                               |        0 |        0 |           1 |
|   2 | api4                          | github.com/mattermost/mattermost/server/v8/channels/api4                                                                |        2 |       17 |        0.89 |
|   3 | app                           | github.com/mattermost/mattermost/server/v8/channels/app                                                                 |        2 |       56 |        0.97 |
|   4 | email                         | github.com/mattermost/mattermost/server/v8/channels/app/email                                                           |        2 |        5 |        0.71 |
|   5 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/email/mocks                                                     |        0 |        1 |           1 |
|   6 | featureflag                   | github.com/mattermost/mattermost/server/v8/channels/app/featureflag                                                     |        2 |        0 |           0 |
|   7 | imaging                       | github.com/mattermost/mattermost/server/v8/channels/app/imaging                                                         |        2 |        0 |           0 |
|   8 | imports                       | github.com/mattermost/mattermost/server/v8/channels/app/imports                                                         |        3 |        0 |           0 |
|   9 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/layer_generators                                                |        0 |        0 |           1 |
|  10 | oauthgitlab                   | github.com/mattermost/mattermost/server/v8/channels/app/oauthproviders/gitlab                                           |        1 |        1 |         0.5 |
|  11 | opentracing                   | github.com/mattermost/mattermost/server/v8/channels/app/opentracing                                                     |        2 |       11 |        0.85 |
|  12 | platform                      | github.com/mattermost/mattermost/server/v8/channels/app/platform                                                        |        2 |       17 |        0.89 |
|  13 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/platform/mocks                                                  |        0 |        0 |           1 |
|  14 | plugin_api_tests              | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests                                                |        1 |        0 |           0 |
|  15 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_hijack_plugin                 |        0 |        0 |           1 |
|  16 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_upgrade_websocket_plugin      |        0 |        0 |           1 |
|  17 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_defaults_plugin |        0 |        1 |           1 |
|  18 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_plugin          |        0 |        1 |           1 |
|  19 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_serve_metrics_plugin               |        0 |        0 |           1 |
|  20 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_bots_plugin                               |        0 |        1 |           1 |
|  21 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_call_log_api_plugin                       |        0 |        0 |           1 |
|  22 | main_test                     | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_db_driver                                 |        0 |        3 |           1 |
|  23 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_bundle_path_plugin                    |        0 |        1 |           1 |
|  24 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_channels_for_team_for_user_plugin     |        0 |        1 |           1 |
|  25 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_direct_channel_plugin                 |        0 |        1 |           1 |
|  26 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_plugin_status_plugin                  |        0 |        1 |           1 |
|  27 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_profile_image_plugin                  |        0 |        1 |           1 |
|  28 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_kv                                        |        0 |        1 |           1 |
|  29 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_member_channels_plugin                    |        0 |        1 |           1 |
|  30 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_members_plugin                            |        0 |        1 |           1 |
(後略)
```

## 抽象度(ABSTRACTNESS)
abstractnessコマンドで抽象度が測れます。

抽象度(ABSTRACTNESS)は一般にはパッケージ内の抽象クラス数/クラスの総数。0は完全に具象的なパッケージで1は抽象的なパッケージ。

Goはクラスを持たないので、以下を使用しているようです。

* ABSTRACTIONS： 構造体とインターフェースの数
* IMPLEMENTATIONS： 関数とメソッド数

```bash
$ spm-go abstractness
Abstractness analysis started.
Module: github.com/mattermost/mattermost/server/v8
126 packages found...
Gathering package metrics, please wait until the command is finished running.
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+--------------+-----------------+--------------+
|   # | PACKAGE                       | PATH                                                                                                                    | ABSTRACTIONS | IMPLEMENTATIONS | ABSTRACTNESS |
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+--------------+-----------------+--------------+
|   1 | main                          | github.com/mattermost/mattermost/server/v8/build/docker-compose-generator                                               |            2 |               1 |         0.67 |
|   2 | api4                          | github.com/mattermost/mattermost/server/v8/channels/api4                                                                |           18 |             696 |         0.03 |
|   3 | app                           | github.com/mattermost/mattermost/server/v8/channels/app                                                                 |           86 |            1902 |         0.04 |
|   4 | email                         | github.com/mattermost/mattermost/server/v8/channels/app/email                                                           |           10 |              61 |         0.14 |
|   5 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/email/mocks                                                     |            2 |              38 |         0.05 |
|   6 | featureflag                   | github.com/mattermost/mattermost/server/v8/channels/app/featureflag                                                     |            7 |              11 |         0.39 |
|   7 | imaging                       | github.com/mattermost/mattermost/server/v8/channels/app/imaging                                                         |           14 |              20 |         0.41 |
|   8 | imports                       | github.com/mattermost/mattermost/server/v8/channels/app/imports                                                         |           21 |              19 |         0.53 |
|   9 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/layer_generators                                                |            3 |               8 |         0.27 |
|  10 | oauthgitlab                   | github.com/mattermost/mattermost/server/v8/channels/app/oauthproviders/gitlab                                           |            2 |               9 |         0.18 |
|  11 | opentracing                   | github.com/mattermost/mattermost/server/v8/channels/app/opentracing                                                     |            1 |             908 |            0 |
|  12 | platform                      | github.com/mattermost/mattermost/server/v8/channels/app/platform                                                        |           48 |             291 |         0.14 |
|  13 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/platform/mocks                                                  |            2 |               4 |         0.33 |
|  14 | plugin_api_tests              | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests                                                |            1 |               1 |          0.5 |
|  15 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_hijack_plugin                 |            1 |               2 |         0.33 |
|  16 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_upgrade_websocket_plugin      |            1 |               2 |         0.33 |
|  17 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_defaults_plugin |            2 |               3 |          0.4 |
|  18 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_plugin          |            2 |               3 |          0.4 |
|  19 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_serve_metrics_plugin               |            1 |               2 |         0.33 |
|  20 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_bots_plugin                               |            1 |               3 |         0.25 |
|  21 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_call_log_api_plugin                       |            2 |               2 |          0.5 |
|  22 | main_test                     | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_db_driver                                 |            1 |               3 |         0.25 |
|  23 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_bundle_path_plugin                    |            1 |               3 |         0.25 |
|  24 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_channels_for_team_for_user_plugin     |            1 |               3 |         0.25 |
|  25 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_direct_channel_plugin                 |            1 |               3 |         0.25 |
|  26 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_plugin_status_plugin                  |            1 |               3 |         0.25 |
|  27 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_profile_image_plugin                  |            1 |               3 |         0.25 |
|  28 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_kv                                        |            1 |               3 |         0.25 |
|  29 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_member_channels_plugin                    |            1 |               3 |         0.25 |
|  30 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_members_plugin                            |            1 |               3 |         0.25 |
(後略)
```

## 主系列からの距離(DISTANCE)
distanceコマンドで主系列からの距離を測れます。最重要指標。

**DISTANCE(主系列からの距離) = | ABSTRACTNESS(抽象度) + INSTABILITY(不安定度) - 1 |**

理想直線(主系列)A + I = 1からの垂直距離。パッケージの抽象性と安定性のバランスの指標でD=0だと抽象性と安定性のバランスが最適とされる。

ベースの思想として抽象度が高いモジュールはよくできているので他から依存されるべきと言うのがあるみたいですね。

```bash
 $ spm-go distance
Distance from main sequence analysis started.
Module: github.com/mattermost/mattermost/server/v8
126 packages found...
Gathering package metrics, please wait until the command is finished running.
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+-------------+--------------+----------+
|   # | PACKAGE                       | PATH                                                                                                                    | INSTABILITY | ABSTRACTNESS | DISTANCE |
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+-------------+--------------+----------+
|   1 | main                          | github.com/mattermost/mattermost/server/v8/build/docker-compose-generator                                               |           1 |         0.67 |     0.67 |
|   2 | api4                          | github.com/mattermost/mattermost/server/v8/channels/api4                                                                |        0.89 |         0.03 |     0.08 |
|   3 | app                           | github.com/mattermost/mattermost/server/v8/channels/app                                                                 |        0.97 |         0.04 |     0.01 |
|   4 | email                         | github.com/mattermost/mattermost/server/v8/channels/app/email                                                           |        0.71 |         0.14 |     0.15 |
|   5 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/email/mocks                                                     |           1 |         0.05 |     0.05 |
|   6 | featureflag                   | github.com/mattermost/mattermost/server/v8/channels/app/featureflag                                                     |           0 |         0.39 |     0.61 |
|   7 | imaging                       | github.com/mattermost/mattermost/server/v8/channels/app/imaging                                                         |           0 |         0.41 |     0.59 |
|   8 | imports                       | github.com/mattermost/mattermost/server/v8/channels/app/imports                                                         |           0 |         0.53 |     0.47 |
|   9 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/layer_generators                                                |           1 |         0.27 |     0.27 |
|  10 | oauthgitlab                   | github.com/mattermost/mattermost/server/v8/channels/app/oauthproviders/gitlab                                           |         0.5 |         0.18 |     0.32 |
|  11 | opentracing                   | github.com/mattermost/mattermost/server/v8/channels/app/opentracing                                                     |        0.85 |            0 |     0.15 |
|  12 | platform                      | github.com/mattermost/mattermost/server/v8/channels/app/platform                                                        |        0.89 |         0.14 |     0.03 |
|  13 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/platform/mocks                                                  |           1 |         0.33 |     0.33 |
|  14 | plugin_api_tests              | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests                                                |           0 |          0.5 |      0.5 |
|  15 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_hijack_plugin                 |           1 |         0.33 |     0.33 |
|  16 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_upgrade_websocket_plugin      |           1 |         0.33 |     0.33 |
|  17 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_defaults_plugin |           1 |          0.4 |      0.4 |
|  18 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_plugin          |           1 |          0.4 |      0.4 |
|  19 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_serve_metrics_plugin               |           1 |         0.33 |     0.33 |
|  20 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_bots_plugin                               |           1 |         0.25 |     0.25 |
|  21 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_call_log_api_plugin                       |           1 |          0.5 |      0.5 |
|  22 | main_test                     | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_db_driver                                 |           1 |         0.25 |     0.25 |
|  23 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_bundle_path_plugin                    |           1 |         0.25 |     0.25 |
|  24 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_channels_for_team_for_user_plugin     |           1 |         0.25 |     0.25 |
|  25 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_direct_channel_plugin                 |           1 |         0.25 |     0.25 |
|  26 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_plugin_status_plugin                  |           1 |         0.25 |     0.25 |
|  27 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_profile_image_plugin                  |           1 |         0.25 |     0.25 |
|  28 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_kv                                        |           1 |         0.25 |     0.25 |
|  29 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_member_channels_plugin                    |           1 |         0.25 |     0.25 |
|  30 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_members_plugin                            |           1 |         0.25 |     0.25 |
(後略)
```

せっかくなのでグラフ化しました。うーん、不安定度0と1が多すぎるのでうまく捉えられていない気もしますね。

![mattermost_distance.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/103496/b6c54a0f-8ba3-9241-bec3-c993f73ed428.png)



## HML、JSON、CSV出力
HTML、JSON、CSVに出力できます。HTMLは色付き。コード見る感じ0.1以下が緑で0.6を超えると赤の模様。


```bash
# 標準出力に表示と同時に./spm-go/output.htmlにレポート作成
$ spm-go all --html
Full analysis started.
Module: github.com/mattermost/mattermost/server/v8
126 packages found...
Gathering package metrics, please wait until the command is finished running.
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+-------+----------+----------+--------------+-----------------+-------------+--------------+----------+
|   # | PACKAGE                       | PATH                                                                                                                    | FILES | AFFERENT | EFFERENT | ABSTRACTIONS | IMPLEMENTATIONS | INSTABILITY | ABSTRACTNESS | DISTANCE |
+-----+-------------------------------+-------------------------------------------------------------------------------------------------------------------------+-------+----------+----------+--------------+-----------------+-------------+--------------+----------+
|   1 | main                          | github.com/mattermost/mattermost/server/v8/build/docker-compose-generator                                               |     1 |        0 |        0 |            2 |               1 |           1 |         0.67 |     0.67 |
|   2 | api4                          | github.com/mattermost/mattermost/server/v8/channels/api4                                                                |    68 |        2 |       17 |           18 |             696 |        0.89 |         0.03 |     0.08 |
|   3 | app                           | github.com/mattermost/mattermost/server/v8/channels/app                                                                 |   116 |        2 |       56 |           86 |            1902 |        0.97 |         0.04 |     0.01 |
|   4 | email                         | github.com/mattermost/mattermost/server/v8/channels/app/email                                                           |     6 |        2 |        5 |           10 |              61 |        0.71 |         0.14 |     0.15 |
|   5 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/email/mocks                                                     |     1 |        0 |        1 |            2 |              38 |           1 |         0.05 |     0.05 |
|   6 | featureflag                   | github.com/mattermost/mattermost/server/v8/channels/app/featureflag                                                     |     2 |        2 |        0 |            7 |              11 |           0 |         0.39 |     0.61 |
|   7 | imaging                       | github.com/mattermost/mattermost/server/v8/channels/app/imaging                                                         |     6 |        2 |        0 |           14 |              20 |           0 |         0.41 |     0.59 |
|   8 | imports                       | github.com/mattermost/mattermost/server/v8/channels/app/imports                                                         |     2 |        3 |        0 |           21 |              19 |           0 |         0.53 |     0.47 |
|   9 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/layer_generators                                                |     1 |        0 |        0 |            3 |               8 |           1 |         0.27 |     0.27 |
|  10 | oauthgitlab                   | github.com/mattermost/mattermost/server/v8/channels/app/oauthproviders/gitlab                                           |     1 |        1 |        1 |            2 |               9 |         0.5 |         0.18 |     0.32 |
|  11 | opentracing                   | github.com/mattermost/mattermost/server/v8/channels/app/opentracing                                                     |     1 |        2 |       11 |            1 |             908 |        0.85 |            0 |     0.15 |
|  12 | platform                      | github.com/mattermost/mattermost/server/v8/channels/app/platform                                                        |    25 |        2 |       17 |           48 |             291 |        0.89 |         0.14 |     0.03 |
|  13 | mocks                         | github.com/mattermost/mattermost/server/v8/channels/app/platform/mocks                                                  |     1 |        0 |        0 |            2 |               4 |           1 |         0.33 |     0.33 |
|  14 | plugin_api_tests              | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests                                                |     1 |        1 |        0 |            1 |               1 |           0 |          0.5 |      0.5 |
|  15 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_hijack_plugin                 |     1 |        0 |        0 |            1 |               2 |           1 |         0.33 |     0.33 |
|  16 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_upgrade_websocket_plugin      |     1 |        0 |        0 |            1 |               2 |           1 |         0.33 |     0.33 |
|  17 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_defaults_plugin |     1 |        0 |        1 |            2 |               3 |           1 |          0.4 |      0.4 |
|  18 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_plugin          |     1 |        0 |        1 |            2 |               3 |           1 |          0.4 |      0.4 |
|  19 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_serve_metrics_plugin               |     1 |        0 |        0 |            1 |               2 |           1 |         0.33 |     0.33 |
|  20 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_bots_plugin                               |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
|  21 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_call_log_api_plugin                       |     1 |        0 |        0 |            2 |               2 |           1 |          0.5 |      0.5 |
|  22 | main_test                     | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_db_driver                                 |     1 |        0 |        3 |            1 |               3 |           1 |         0.25 |     0.25 |
|  23 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_bundle_path_plugin                    |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
|  24 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_channels_for_team_for_user_plugin     |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
|  25 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_direct_channel_plugin                 |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
|  26 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_plugin_status_plugin                  |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
|  27 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_profile_image_plugin                  |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
|  28 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_kv                                        |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
|  29 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_member_channels_plugin                    |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
|  30 | main                          | github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_members_plugin                            |     1 |        0 |        1 |            1 |               3 |           1 |         0.25 |     0.25 |
(後略)
```

![spm_go_output_html.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/103496/2e36fd8a-fbdc-3688-ae01-ef75089f0082.png)


JSON

```bash
$ spm-go all -f json
Full analysis started.
Module: github.com/mattermost/mattermost/server/v8
126 packages found...
Gathering package metrics, please wait until the command is finished running.
{
    "packages": [
        {
            "name": "main",
            "path": "github.com/mattermost/mattermost/server/v8/build/docker-compose-generator",
            "files": [
                "main.go"
            ],
            "files_count": 1,
            "dependencies": {
                "standard": [
                    "fmt",
                    "os",
                    "strings"
                ],
                "externals": [
                    "gopkg.in/yaml.v2"
                ],
                "standard_count": 3,
                "externals_count": 1,
                "count": 4
            },
            "afferent_coupling": 0,
            "efferent_coupling": 0,
            "instability": 1,
            "abstractness_details": {
                "functions": 1,
                "structs": 2
            },
            "abstractions_count": 2,
            "implementations_count": 1,
            "abstractness": 0.67,
            "distance": 0.67
        },
        {
            "name": "api4",
            "path": "github.com/mattermost/mattermost/server/v8/channels/api4",
            "files": [
                "api.go",
                "apitestlib.go",
                "bleve.go",
                "bot.go",
(後略)
```

CSVはcsvと言う名前のsemicolon separated values。

```bash
$ spm-go all -f csv
Full analysis started.
Module: github.com/mattermost/mattermost/server/v8
126 packages found...
Gathering package metrics, please wait until the command is finished running.
Package;Path;Files;Afferent;Efferent;Abstractions;Implementations;Instability;Abstractness;Distance
main;github.com/mattermost/mattermost/server/v8/build/docker-compose-generator;1;0;0;2;1;1;0.67;0.67
api4;github.com/mattermost/mattermost/server/v8/channels/api4;68;2;17;18;696;0.89;0.03;0.08
app;github.com/mattermost/mattermost/server/v8/channels/app;116;2;56;86;1902;0.97;0.04;0.01
email;github.com/mattermost/mattermost/server/v8/channels/app/email;6;2;5;10;61;0.71;0.14;0.15
mocks;github.com/mattermost/mattermost/server/v8/channels/app/email/mocks;1;0;1;2;38;1;0.05;0.05
featureflag;github.com/mattermost/mattermost/server/v8/channels/app/featureflag;2;2;0;7;11;0;0.39;0.61
imaging;github.com/mattermost/mattermost/server/v8/channels/app/imaging;6;2;0;14;20;0;0.41;0.59
imports;github.com/mattermost/mattermost/server/v8/channels/app/imports;2;3;0;21;19;0;0.53;0.47
main;github.com/mattermost/mattermost/server/v8/channels/app/layer_generators;1;0;0;3;8;1;0.27;0.27
oauthgitlab;github.com/mattermost/mattermost/server/v8/channels/app/oauthproviders/gitlab;1;1;1;2;9;0.5;0.18;0.32
opentracing;github.com/mattermost/mattermost/server/v8/channels/app/opentracing;1;2;11;1;908;0.85;0;0.15
platform;github.com/mattermost/mattermost/server/v8/channels/app/platform;25;2;17;48;291;0.89;0.14;0.03
mocks;github.com/mattermost/mattermost/server/v8/channels/app/platform/mocks;1;0;0;2;4;1;0.33;0.33
plugin_api_tests;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests;1;1;0;1;1;0;0.5;0.5
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_hijack_plugin;1;0;0;1;2;1;0.33;0.33
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_http_upgrade_websocket_plugin;1;0;0;1;2;1;0.33;0.33
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_defaults_plugin;1;0;1;2;3;1;0.4;0.4
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_load_configuration_plugin;1;0;1;2;3;1;0.4;0.4
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/manual.test_serve_metrics_plugin;1;0;0;1;2;1;0.33;0.33
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_bots_plugin;1;0;1;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_call_log_api_plugin;1;0;0;2;2;1;0.5;0.5
main_test;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_db_driver;1;0;3;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_bundle_path_plugin;1;0;1;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_channels_for_team_for_user_plugin;1;0;1;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_direct_channel_plugin;1;0;1;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_plugin_status_plugin;1;0;1;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_get_profile_image_plugin;1;0;1;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_kv;1;0;1;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_member_channels_plugin;1;0;1;1;3;1;0.25;0.25
main;github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests/test_members_plugin;1;0;1;1;3;1;0.25;0.25
(後略)
```

## 最後に
以上です。

凝集度だと[Yahoo Japanのツール](https://github.com/yahoojapan/lcom4go)があるので組み合わせるとはかどりそうです。