6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RuboCop | Style/IfUnlessModifier

Last updated at Posted at 2014-07-06

RuboCop | Style/IfUnlessModifier

概要

RuboCopの「Style/IfUnlessModifier」警告について。
1行で書くべき if / unless ステートメントに対して警告を出す。
1行で書くかどうかは、 1行にした場合にIfUnlessModifierMaxLineLength
で収まるかどうかで判断する。

デフォルトは 80 です。

IfUnlessModifier

各設定値での検証結果をまとめます。

検証プログラム

if_unless_modifier.rb

def hage(msg)
  if msg.nil?
    fail ArgumentError, 'Invalid argument nil.  Invalid argument nil.'
  end
  msg * 2
end

def hige(msg)
  if msg.nil?
    fail ArgumentError, 'Invalid argument nil.  Invalid argument nil. '
  end
  msg * 2
end

実行結果 デフォルト の場合

.rubocop.yml

※検証のために LineLength が発生しないように100に変更

LineLength:
  Max: 100

IfUnlessModifier:
  MaxLineLength: 80
$ rubocop
Inspecting 1 file
C

Offenses:

if_unless_modifier.rb:2:3: C: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
  if msg.nil?
  ^^

1 file inspected, 1 offense detected

実行結果 MaxLength を79に変更します

.rubocop.yml

LineLength:
  Max: 100

IfUnlessModifier:
  MaxLineLength: 79
$ rubocop
Inspecting 1 file
.

1 file inspected, no offenses detected

RuboCopまとめ記事

6
3
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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?