Language / Framework: Ruby
Checks: 300
Categories: Complexity, Security, Clarity, Performance, Bug Risk, Style
Rubocop Version: 0.46.0

Our RuboCop engine provides style and quality checks for Ruby code. RuboCop includes support for MRI 1.9.3, 2.0, 2.1, 2.2, and 2.3, jRuby in 1.9 mode, and Rubinius 2.0+.

Enable the Engine

To enable the RuboCop engine for your project, add the following to your .codeclimate.yml configuration file: ‚Äč

  1. Commit a .codeclimate.yml to the root of all branches in your repository.
  2. Add an engines node.
  3. Mark rubocop as enabled: true.
engines:
  rubocop:
    enabled: true
    
ratings:
   paths:
   - "**.rb"

Configuration

RuboCop supports configuration via a .rubocop.yml configuration file as detailed in RuboCop's documentation. Edit your .rubocop.yml to select rules and tune thresholds that best meet the requirements of your team.

A complete list of available "cops" can be found by scanning the file lists for each cop category:

Default RuboCop Configuration

If you're new to RuboCop, we recommend starting off with our default configuration. Our default emphasizes penalization for complexity and disables style checks. Specifically, our default enables some Lint, Metrics, and Performance cops, but no Rails or Style cops.

We disable style cops so that pull requests initially will not error on style choices that your team may not yet have agreed upon. If you're looking to get started with some style checks, but aren't sure which ones, you may want to look at RuboCop's default config which enables checks based on the Ruby Style Guide.

Am I already using the default .rubocop.yml?

Our default configuration is applied automatically on Code Climate as part of our "inferred configuration", a process which runs when the repository is missing a .codeclimate.yml (or using an outdated one), or engine-specific configuration files. Instructions and access to this inferred configuration (including your .codeclimate.yml and other configuration files) is available within your repo on Code Climate.

If you do not have a .rubocop.yml, Code Climate will automatically generate one.

To use RuboCop's default configuration, add a .rubocop.yml containing RuboCop's defaults.

Default Complexity Cops

Code Climate's default .rubocop.yml enables specific Metrics cops which we think make a good starting point for thinking about the maintainability of your code. Specifically, we enable lines of code-based cops as well as the Cyclomatic Complexity cop.

There are many different ways to measure complexity and maintainability in code. We believe lines of code is the most intuitive, and a great place to start.

We enable Cyclomatic Complexity as it is also a relatively simple concept, and distinct enough from lines of code to sometimes be tripped independently. A detailed explanation of Cyclomatic Complexity can be found in our glossary.

The details of the complexity checks we enable in our default configuration can be found below:

Check Max Category
Class Length 250 Complexity
Method Length 30 Complexity
Module Length 250 Complexity
Block Nesting 3 Complexity
Cyclomatic Complexity 6 Complexity

Note: these defaults diverge slightly from those in the native RuboCop tool.

Using a non-standard configuration file

Some users may want to keep their configuration in a file named something other than .rubocop.yml. You can specify an alternate file name in your .codeclimate.yml:

engines:
  rubocop:
    enabled: true
    config:
      file: my_rubocop.yml

Customizing Checks and Thresholds

Users can adjust their RuboCop configuration through a variety of means, including:

  • Directly disable a check in their .codeclimate.yml file:
    engines:
    rubocop:
      enabled: true
      checks:
        Rubocop/Metrics/ClassLength:
          enabled: false
    
  • Enable and disable rules by specifying Enabled: true or false under Rule name in their .rubocop.yml
  • Comment inline
    • for x in (0..19) # rubocop:disable Style/AvoidFor
  • Configure tolerance for violations (including complexity) by tuning the threshold in their .rubocop.yml under the Rule name. For instance:

Impact of Complexity on Grades

RuboCop provides several different metrics which can be used to help assess the maintainability of code, including:

By default, as mentioned above, Code Climate enables length based cops and Cyclomatic Complexity.

In penalizing grades for violations, Code Climate assigns remediation points, proportionate to the amount of effort required to repair a software issue.

Code Climate treats complexity violations as one of weightiest because of the effort involved in repair, and penalizes according to the following formula:

Method violations

    remediation_points = 1,000,000 + (Actual - Threshold) * 70,000

Class and Module violations

    remediation_points = 5,000,000 + (Actual - Threshold) * 35,000

where Threshold is the configured maximum length or McCabe complexity number, and Actual is the score for a given method or code unit.

Note that the RuboCop engine configures each complexity metric to penalize code using this essential rubric, and that there's some overlap; in some cases two different complexity checks could end up penalizing essentially the same code. This is perfectly fine, but may result in greater penalties to the code than you were expecting.

As such, you may prefer to think of some of the complexity cops that RuboCop offers as alternatives. For example, you may want to choose between ABC Complexity and Cyclomatic Complexity.

RuboCop