Rubocop: Style/IfUnlessModifier having its own LineLength

Created on 3 Aug 2018  路  11Comments  路  Source: rubocop-hq/rubocop

Hi All!

In the Ruby projects I'm working on the maximum line length is _recommended_ to be at 80 chars, but the _hard cap_, that rubocop uses for validation, is set to 120. This is because a long line every now and than can be easier to read than splitting one action into several lines (e.g. method calls with many parameters).

This has worked fine for quite a long time, but then I updated rubocop from 0.51.0 to 0.58.0. Now the Styling/IfUnlessModifier cop expects if and unless to be on the same line as a single line body, even if it would make the line far longer than the _recommended_ 80 characters we use.

The 80 characters "soft" limit isn't specified in rubocop.yml, only written for humans in a comment, and it seems we've unknowingly relied on a bug in the IfUnlessModifier that has now been fixed.

Is it possible to set the IfUnlessModifier cop to use its own line length?

If not possible now, would it be possible to add a LineMaxLength option to it?

Thanks!

$ rubocop -V
0.58.1 (using Parser 2.5.1.2, running on ruby 2.4.2 x64-mingw32)
feature request stale

Most helpful comment

Maybe it would be better implemented as a global RecommendedLineLength (defaulting to MaxLineLength). I don't know if there are other cops that could rely on it.

All 11 comments

IMO, there is nothing unique about the IfUnlessModifier cop. Adding a LineMaxLength option to it would suggest that many other cops should also receive a LineMaxLength option. Doing so would not only make the LineLength cop pointless, it would be confusing (from a user point of view) and bug prone (from a developer point of view) when these options conflicted.

Maybe it would be better implemented as a global RecommendedLineLength (defaulting to MaxLineLength). I don't know if there are other cops that could rely on it.

I have the same problem. I like both the cop specific length and the recommended line length approaches.

A global setting would apply to while and until loops too.

I think that's fine. The policy that I would like to have is that 120 chars is the limit but 80 is desirable, so I don't want Rubocop changing stuff to more than 80.

I found myself in the same shoes. Thanks for opening this issue. I set my MaxLength to 90 and I like the Style/IfUnlessModifier cop but now I have to write something like:

        return value if [true, false, nil].include?(value) && config[:as].to_s != 'count'

instead of:

      if [true, false, nil].include?(value) && config[:as].to_s != 'count'
        return value 
      end

Obviously, I can work around this with some local variables, but I don't like this solution and want a single IF statement. However, because Rubocop wants me to keep 1 line, I have to obey (or ignore, or disable this cop). None of these solutions are good for me and I just want to set MaxLength for this cop to 80.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

Homebrew would still find this useful so just commenting so stale bot knows someone cares (although this could be combined with https://github.com/rubocop-hq/rubocop/issues/3534 or https://github.com/rubocop-hq/rubocop/issues/6077)

I also still find this useful, even though I am now leaning towards a more general approach of a MaxLineLength and a RecommendedLineLength as different options. All cops recommending to join things into a single line would honor to RecommendedLineLength if it's set or otherwise use MaxLineLength.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bquorning picture bquorning  路  3Comments

kirrmann picture kirrmann  路  3Comments

Aqualon picture Aqualon  路  3Comments

bbatsov picture bbatsov  路  3Comments

ecbrodie picture ecbrodie  路  3Comments