Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Training RuboCop

Training RuboCop

The deck for the talk I gave at https://trbmeetup.doorkeeper.jp/events/83070

Masafumi Okura

December 05, 2018
Tweet

More Decks by Masafumi Okura

Other Decks in Technology

Transcript

  1. About this talk • 27 pages • With a demo

    • For beginner-intermediate
  2. RuboCop is: • a Ruby static code analyzer (a.k.a. linter)

    and code formatter (from official documentation). • a useful tool to make a team’s coding standard explicit and automatically executable. • an AST-based tool with complex internals, while most customization can be done by simply editing YAML files.
  3. Benefits of using RuboCop • It’s easy to declare your

    team’s coding rule/ standard with YAML files. • Coding rules are enforceable/executable by various ways such as CI, which makes your code more maintainable. • Autocorrect feature with editor plugins enables us to edit code faster.
  4. Wording • “Cop” is a rule for RuboCop. Each cop

    has a rule of offense and sometimes knows how to fix a problem (autocorrect). • “Autocorrect” is a feature of RuboCop modifying code with a rule automatically if possible (eg. fixing indentation). • “Config file” is a file named “.rubocop.yml”, which configures RuboCop’s behavior. • “Todo file” is a YAML file similar to normal config file but generated by RuboCop command.
  5. What I’m gonna talk about: • Introduction <- Done! •

    Main topic 1: Customizing • Main topic 2: Workflow
  6. It’s just a default • RuboCop’s default configuration follows community

    style guide at Ruby Style Guide (https://github.com/ rubocop-hq/ruby-style-guide) • This guide is not “approved by” authorities nor admitted by the whole Ruby community. • There are more guides, such as Cookpad’s one (https://github.com/cookpad/styleguide/blob/ master/ruby.en.md)
  7. Some rules are troublesome • Character length is 80… It’s

    4k era, not vt100 era (however, some people argue character length makes sense). • Double negation is a famous Ruby idiom but RuboCop warns you by default. • Percent literal delimiters default is []… IMHO most of us prefer (), right?
  8. Some are controversial • ABC size (code complexity) default is

    15… isn’t it too strict? • Symbol/String Array cops are mostly useful but sometimes we’d like to use bracket style. • Should spaces inside braces be required? What about spaces inside parentheses?
  9. Tips for customization • Disable all Style cops! (especially when

    you introduce RuboCop to an ongoing project) • Disable all cops for auto generated files like bin, schema.rb, etc.. • Use auto-gen-config feature to generate todo file for fixing problems later unless your project is brand new.
  10. Tips for customization (cont) • Always send a Pull Request

    when you modify “.rubocop.yml” file to get consensus from your team. • Don’t be hasty. Don’t copy them from other teams or companies unless they share most of the rules. It’s a matter of culture.
  11. Notes on disabling • There are two ways to disable

    a certain Cop. • One way is to disable each Cop in config file • Another way is to add a special comment to where you want to disable, such as “rubocop:disable Style/WordArray”.
  12. Recommended workflow • First, edit code with autocorrect. • Then,

    run RuboCop in some way before merging Pull Request. • Finally, maintain your coding rules with Pull Requests (previously mentioned).
  13. On the fly lint • If your editor has a

    plugin for Ruby, RuboCop or syntax checker, you might be able to run RuboCop on the fly, while you edit code. • Autocorrect works on the fly as well. • If your editor does not have a plugin, I recommend changing an editor (use Vim if you have no preference )
  14. SaaS for lint • There are some SaaS available to

    lint your code before merging Pull Requests. • Use them if your budget allows • You can use your favorite CI services to run RuboCop.
  15. RuboCop is useful but… • Don’t forget to customize rules

    to suit your team. Don’t believe the default rules. • Don’t try to fix all problems at once if your project is mature/ongoing. Agree on coding standards of your team first, and write config for RuboCop following it.
  16. pp self • Company: Cycle inc. • Experience: Ruby/Rails over

    5 years • Role: Chief Vim Officer (CVO) • Next talk: This Saturday (in Japanese)