LoginSignup
4
2

More than 1 year has passed since last update.

RuboCop Metrics/CyclomaticComplexityをMax: 30で開始した話

Last updated at Posted at 2022-12-20

\\\ みんせつ Advent Calendar 2022の第20日目の記事です ///

みんせつ Advent Calendar 2022

前回の記事はみんせつに入社して7ヶ月目のIさんの「Hello, Zenn!」でした。


Rubyも書きます、T郎です。

みんせつでは創業当初よりRuby on RailsでWebサービスを開発してきました。当初よりRuboCopを導入していました。

RuboCopを導入していました

はい、開発当初よりRuboCopを導入していました。ええ、実装開始してから、555営業日後に。

ということで、555営業日の間はRuboCopを一切使わない形でRubyコードの実装が推進されていました。

RuboCopを導入しました

はい。556日目にRuboCopを導入することにしました。そこからは各自開発環境などでマニュアルで実行してね。という日々だったようです。

RuboCopをCIに組み込みました。

そこから1050日ほど経過したある日突然CIに組み込むことにしました。当然、 --auto-gen-config の結果をそのままTODOに残したままのスタートとなります。

Metrics/CyclomaticComplexityとは何か?

Checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one. Blocks that are calls to builtin iteration methods (e.g. `ary.map{...}) also add one, others are ignored.

  def each_child_node(*types)               # count begins: 1
    unless block_given?                     # unless: +1
      return to_enum(__method__, *types)

    children.each do |child|                # each{}: +1
      next unless child.is_a?(Node)         # unless: +1

      yield child if types.empty? ||        # if: +1, ||: +1
                     types.include?(child.type)
    end

    self
  end                                       # total: 6

ということのようです。

Metrics/CyclomaticComplexity Max: 43からのスタート

--auto-gen-config の結果を .rubocop_todo.yml に入れたままのスタートでしたので、Maxの値は43だったのでした。

Max: 30にする

Maxを7かそれに近い値まで下げたいと思っていましたが、師走の忙しい時期です。同僚の時間をもらうわけにもいかないため、暫定でカットオフバリューを見積もりました。その結果、30でスタートしようということになり、 .rubocop.yml

.rubocop.yml
Metrics/CyclomaticComplexity:
  # Default: 7
  Max: 30
  # Exclude files too old files generated by former members (Max: 31-43)
  Exclude:
    - ‘hello.rb’  # https://github.com/topics/topics/pull/1234
    - ‘world.rb’  # https://github.com/topics/topics/pull/5678

と追記することで対処して、2023年を迎えることにしました。※
(終わり)

※ と言いつつ、この記事を公開してから、クリスマス休暇が始まるまでには、さらに小さい値を指定していると思います。


\\\ みんせつ Advent Calendar 2022の第20日目の記事でした ///

みんせつ Advent Calendar 2022

明日の記事のタイトルは未定ですが、担当者に催促しておきます。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2