Language / Framework: Ruby
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+.
To enable the RuboCop engine for your project, add the following to your .codeclimate.yml configuration file:
- Commit a
.codeclimate.ymlto the root of all branches in your repository.
- Add an
rubocopas enabled: true.
engines rubocop enabledtrue ratings paths "**.rb"
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:
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.
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.
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:
Note: these defaults diverge slightly from those in the native RuboCop tool.
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
engines rubocop enabledtrue config filemy_rubocop.yml
Users can adjust their RuboCop configuration through a variety of means, including:
- Directly disable a check in their
engines: rubocop: enabled: true checks: Rubocop/Metrics/ClassLength: enabled: false
- Enable and disable rules by specifying
falseunder Rule name in their
- Comment inline
for x in (0..19) # rubocop:disable Style/AvoidFor
- Configure tolerance for violations (including complexity) by tuning the threshold in their
.rubocop.ymlunder the Rule name. For instance:
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:
remediation_points = 1,000,000 + (Actual - Threshold) * 70,000
remediation_points = 5,000,000 + (Actual - Threshold) * 35,000
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.