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

110cd4dcaed019b3bd9809e541afdeb6?s=128

Masafumi Okura

December 05, 2018
Tweet

Transcript

  1. Training RuboCop By OKURA Masafumi (@okuramasafumi) For Tokyo Rubyist Meetup,

    2018/12/05
  2. About this talk • 27 pages • With a demo

    • For beginner-intermediate
  3. Introduction: What’s RuboCop?

  4. 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.
  5. 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.
  6. 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.
  7. What I’m gonna talk about: • Introduction <- Done! •

    Main topic 1: Customizing • Main topic 2: Workflow
  8. Main topic 1: Customizing (Training RuboCop not to hurt you)

  9. 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)
  10. 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?
  11. 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?
  12. 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.
  13. 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.
  14. 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”.
  15. Topic 2: Workflow (Tranging RuboCop to work for you)

  16. 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).
  17. 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 )
  18. Demo

  19. 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.
  20. Conclusion

  21. 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.
  22. One more thing:

  23. Who are you?

  24. pp self • Company: Cycle inc. • Experience: Ruby/Rails over

    5 years • Role: Chief Vim Officer (CVO) • Next talk: This Saturday (in Japanese)
  25. Special thanks • @koic (a maintainer of RuboCop)