Slide 1

Slide 1 text

RuboCop for Code Review Code Review Meetup #1 Jan. 19, 2018

Slide 2

Slide 2 text

User.find(name: "pocke").inspect ● Masataka Kuwabara ● Engineer at Actcat, Inc. / SideCI ○ Rubyist, Gopher and Vimmer. ● RuboCop's core developer. ○ Today, I'll talk about RuboCop.

Slide 3

Slide 3 text

Goal ● Improve code review with RuboCop. ● RuboCop is not a “Silver Bullet”. ○ When RuboCop reviews your code, you should review RuboCop's warnings.

Slide 4

Slide 4 text

Note ● I’ll talk about Ruby today, but we can apply the practices to projects that is written in other languages.

Slide 5

Slide 5 text

Agenda ● What's RuboCop? ● The internal of RuboCop ● RuboCop for Code Review

Slide 6

Slide 6 text

Questions

Slide 7

Slide 7 text

Questions ● Are you Rubyist? ● Do you know RuboCop?

Slide 8

Slide 8 text

What's RuboCop?

Slide 9

Slide 9 text

What's RuboCop? ● A static analyzer for Ruby. ● It has 3 type checks. ○ Style ○ Lint ○ Metrics

Slide 10

Slide 10 text

Style Check coding style

Slide 11

Slide 11 text

Style: check coding style ● White spaces ○ Indentation width ○ White spaces around brackets ● Syntax vs Method call ○ alias vs alias_method ○ for vs each

Slide 12

Slide 12 text

Style checks based on one of the style guides. ● https://github.com/bbatsov/ruby- style-gudie ○ If your coding style has differences, you can configure RuboCop.

Slide 13

Slide 13 text

Style: example # Use `def foo(arr)` def foo arr for x in arr # Use each bar # Bad indentation end end

Slide 14

Slide 14 text

Lint Check bugs

Slide 15

Slide 15 text

Lint: check bugs ● Unreachable code ● Debug code ● Unused variables ● etc...

Slide 16

Slide 16 text

Lint: example def foo binding.irb # debug code return true # It’s never executed. puts 'hi' end

Slide 17

Slide 17 text

Metrics Check complexity

Slide 18

Slide 18 text

Metrics: check complexity ● ABC size ○ Assignment, Branches, Conditional ● Line length ● Class length ● etc...

Slide 19

Slide 19 text

The internal of RuboCop

Slide 20

Slide 20 text

How does RuboCop check code? ● RuboCop analyzes each file individually. ● RuboCop does not execute code. See “Writing Lint for Ruby” in RubyKaigi 2017.

Slide 21

Slide 21 text

Analyzing each file individually

Slide 22

Slide 22 text

Analyzing each file individually ● RuboCop does not check relations between files. ○ e.g. db/schema.rb and models. ● For simplicity and performance. ○ RuboCop can analyze parallelly.

Slide 23

Slide 23 text

Do not execute code

Slide 24

Slide 24 text

Do not execute code ● RuboCop doesn’t execute your code. ○ Pros: If your code has system('rm -rf /'), it does not remove. ○ Cons: RuboCop cannot understand application completely. ■ e.g. eval, method call

Slide 25

Slide 25 text

RuboCop for Code Review

Slide 26

Slide 26 text

RuboCop for Code Review ● What does “RuboCop for Code Review” mean? ● Advantage of RuboCop in Code Review. ● Action for RuboCop Review

Slide 27

Slide 27 text

What does “RuboCop for Code Review” mean?

Slide 28

Slide 28 text

What does “RuboCop for Code Review” mean? ● RuboCop also reviews your code. ○ Human Review + RuboCop Review

Slide 29

Slide 29 text

How does RuboCop review ● rubocop $(git diff --name-only) before human review. ● I recommend using CI to run RuboCop. ○ e.g. SideCI, CodeClimate and Reviewdog.

Slide 30

Slide 30 text

Advantages of RuboCop in review

Slide 31

Slide 31 text

Advantages of RuboCop in review ● Reduce human reviews. ● Enhancement reviews.

Slide 32

Slide 32 text

Reduce human reviews ● Layout and style reviews by human are no longer necessary. ○ e.g. “Remove the spaces”, “Use each method instead of for”. ○ It’s a RuboCop job, not a human job.

Slide 33

Slide 33 text

Enhancement reviews ● RuboCop can find bugs that human will overlook. ○ Duplicate method definitions. ○ binding.irb(debug code)

Slide 34

Slide 34 text

Action for RuboCop Review

Slide 35

Slide 35 text

Action for RuboCop Review ● You should review “RuboCop Review”.

Slide 36

Slide 36 text

Action for RuboCop Review For Style Reviews

Slide 37

Slide 37 text

Action for Style Review ● Check your coding style. ○ If the review does not match with your coding style, you should configure or ignore the review. ○ http://rubocop.readthedocs.io/en/stabl e/

Slide 38

Slide 38 text

Action for RuboCop Review For Lint Reviews

Slide 39

Slide 39 text

Action for Lint Review ● Lint reviews has false positives. ○ In other words, Lint review has a mistake sometimes. ○ Because RuboCop does not know your application completely. ■ See the slides above. ● It is not a bug, it is a restriction.

Slide 40

Slide 40 text

How to start and/or continue to use RuboCop?

Slide 41

Slide 41 text

Start and/or continue to use RuboCop ● You can get knowledges from WEB+DB PRESS vol.102. ○ “RuboCopできれいで安全なコード” written by me. ○ http://amzn.asia/fZ9cgZO

Slide 42

Slide 42 text

Conclusion

Slide 43

Slide 43 text

Conclusion ● RuboCop is not a “Silver Bullet”. ○ In many cases, the default coding style doesn't match your coding style. ○ RuboCop has false positives. ○ So when RuboCop reviews your code, you should review RuboCop Review.