Slide 1

Slide 1 text

Training RuboCop By OKURA Masafumi (@okuramasafumi) For Tokyo Rubyist Meetup, 2018/12/05

Slide 2

Slide 2 text

About this talk • 27 pages • With a demo • For beginner-intermediate

Slide 3

Slide 3 text

Introduction: What’s RuboCop?

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

What I’m gonna talk about: • Introduction <- Done! • Main topic 1: Customizing • Main topic 2: Workflow

Slide 8

Slide 8 text

Main topic 1: Customizing (Training RuboCop not to hurt you)

Slide 9

Slide 9 text

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)

Slide 10

Slide 10 text

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?

Slide 11

Slide 11 text

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?

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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”.

Slide 15

Slide 15 text

Topic 2: Workflow (Tranging RuboCop to work for you)

Slide 16

Slide 16 text

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).

Slide 17

Slide 17 text

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 )

Slide 18

Slide 18 text

Demo

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

Conclusion

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

One more thing:

Slide 23

Slide 23 text

Who are you?

Slide 24

Slide 24 text

pp self • Company: Cycle inc. • Experience: Ruby/Rails over 5 years • Role: Chief Vim Officer (CVO) • Next talk: This Saturday (in Japanese)

Slide 25

Slide 25 text

Special thanks • @koic (a maintainer of RuboCop)