rails_best practices_rubykaigi
Upcoming SlideShare
Loading in...5
×
 

rails_best practices_rubykaigi

on

  • 4,770 views

My presentation on rubykaigi 2011

My presentation on rubykaigi 2011

Statistics

Views

Total Views
4,770
Views on SlideShare
1,869
Embed Views
2,901

Actions

Likes
2
Downloads
14
Comments
0

19 Embeds 2,901

http://rails-bestpractices.com 1190
http://huangzhimin.com 1097
http://presentations.huangzhimin.com 269
http://blog.huangzhimin.com 90
http://localhost 78
http://feeds.feedburner.com 74
http://flyerhzm.github.com 45
http://alpern.hd.free.fr 27
http://cache.baidu.com 8
http://yangha.me 5
http://twitter.com 4
http://webcache.googleusercontent.com 4
http://xianguo.com 3
http://www.linkedin.com 2
http://10.228.25.16 1
http://gnuhub.com 1
http://flyerhzm.github.io 1
http://aqoon.local 1
http://192.168.1.6 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Hi Everybody, today my topic is use rails_best_practices to refractor your rails codes. rails_best_practices gem is a static code analytics tool for rails project. \nBefore I start the topic, let me ask you a question first, have you ever tried rails_best_practices gem?\n(wow, more than I expected)\n(seems not many, I'd like to give you an introduction about this gem, I think you will like it)\n
  • My name is Richard Huang, here is my personal website, you can follow my twitter account or check out my open sourced projects on github.\nI'm now working for OpenFeint, it's the largest mobile gaming network.\n
  • First, please allow me to tell you the story why I created rails_best_practices gem.\nIn October 2009, I attended a RailsConf in China Shanghai, Mr. Wen-Tian Chang shared his famous presentation Rails Best Practices, it's really exciting, he told us what are bad smells and how to refractor them.\nAfter that, I looked through my rails projects, found a lot of bad smells, then I refractor them according to his prevention. \n
  • But it was not easy to catch all of the bad smells manually, so I thought if I could make it automatically.\nThen I began to write the rails_best_practices gem from November 2009. It doesn't implement all of the practices from Wen-Tian Chang's presentation because some are too difficult to check automatically, but I also added some other useful practices to the gem.\n
  • Now, it contains 28 code checkers, nearly 800 watchers on github, and more than 1 hundred thousand downloads.\n
  • After a year, I realized that I need help, I hope to see more and more rails best practices from talent rails developers all over the world, then I can add their practices to the rails_best_practices gem. So I created a website rails-bestpractices.com in December 2010. On the website, everyone can share his rails practices, others can vote to say if they like it or not, they can also leave comments to discuss. \n
  • This is one of the best practices on webiste, named use query attribute, in common, the practice will show you what’s the bad smell, and how to refactor it.\n
  • Now there are 70 practices on the website, I can’t say all of them are best, but most of them are really useful. The website also contains the gem document and gem weblog.\n
  • Now, let me show you how rails_best_practices gem works.\n
  • First, you should gem install rails_best_practices, go to a rails project directory, type rails_best_practices command.\n
  • It's really easy to use, then you will see the analyze result. It tells you which line in which file has a code smell, and tells you what code smell it is as well. Then you can go to the rails-bestpractices.com website to figure out how to refractor this bad smell.\n
  • rails_best_practices gem also provides a visual result page. It's a html document, same to the terminal, it shows you which file and which line contains the bad smell, the bad smell description is at the right side. Click the filename it will open Textmate or mvim and locate to that file and that line, Click the bad smell description, it will take you to the rails-bestpractices.com website, show you how to refractor the bad smells.\nLet’s try it now.\n
  • Why I think we should use the rails_best_practices gem?\n
  • The most important reason is we are the ruby developer, we are the rails developer, we not only write codes to solve problems, we also have the pleasure to read and write beautiful codes. \nAnd we always try to follow the best practices to solve the problems.\n
  • You know different developers may have different coding styles, but we are always working in a team, we should have a code convention for team, to say what should do and what should not do.\nBesides this, in a rails team, we have senior rails developers, we also have junior developers, most of the time we will do the code review for others, but what if we can make the code review partial automatically? For example, before code review, the codes are checked automatically for syntax, so we can focus on how they are implemented and if there will be a performance issue without worrying too much about if the codes are ugly.\n\n
  • Before, I wrote Java codes, there is a great code analytics tool, named checkstyle, which tells you the bad smells in your java codes.\n
  • In Ruby world, we also have a lot of code analytics tools, like saikuro, flog, flay, reek and roodi, they help you to analyze the code complex, find out similar or duplicated codes and other codes smells in ruby codes. They are great.\n
  • But they are used for ruby projects, no special for rails projects. rails_best_practices gem is a static code analytics tool, created for rails projects, helps you to find out the rails code smells, give you some suggestions to improve your rails code quality. It provides you 28 default code checkers, if you don't like all of the default checkers, you can customize to use only some. rails_best_practices gem also provides a way to write plugins, which allows you to write your own code check lists for your rails team.\n
  • If you are interested in writing your own check list, you should know a bit about how rails_best_practices gem works internally?\n
  • This is the high-level work flow of rails_best_practices gem. The left top is the source codes, left bottom is the analyze result, the middle part is the rails_best_practices gem, the right side is the sexp, sexp is a data structure to represent the abstract syntax tree. \n
  • First step, the gem reads source codes of rails project, \nthen, the gem translates the source codes into sexp\n
  • The third step is lexical, it only analyze the code formats, like if the codes contain the trailing white space, it doesn't depend on sexp, just reads the content of source codes, and outputs to the analyze result if necessary. \n
  • The fifth step is prepare, just as its name implies, it does some preparation for the final code check, it reads the sexp, analyzes the model names, model attributes, model associations, and so on. \n
  • After that, it does the code review, reads prepare result and the sexp again, check the sexp according to the best practices on website, if it finds anything violates the best practices, it will send it to the analyze result.\n
  • Let's have a look what's the sexp. The top side is the ruby source code, and the bottom side is the corresponding sexp, it is readable, define a class Post, inherited from ActiveRecord::Base, within it, call a method attr_accessor with argument editable.\n
  • Here is another example, it's a method definition, method name is test, with empty arguments, contains a code block, call a method puts with argument hello world.\n
  • I don't want to tell you all about sexp in this presentation, you can play it with ruby_parser gem. And here is the link that show you how rails_best_practices gem use ruby_parser to get the sexp.\n
  • rails_best_practices command provides several options, by default it outputs the bad smells in terminal with colors, you can also ask it to output as html page, and with textmate or mvim support. there are other options, type rails_best_practices -h to get more.\n
  • You may not like all of the default code checkers, it's not a problem, rails_best_practices allow you to customize which code checkers should use or not. Go to the rails project directory, type rails_best_practices -g, it will generate a configuration file rails_best_practices.yml under config directory, you can comment or delete the code checkers if you don't like. Save it, then when you use rails_best_practices again, the commented or deleted code checkers will not take effect. Let’s try it now.\n
  • What about the code checkers that you want but not exist in the rails_best_practices default checkers? Don't worry, rails_best_practices gem allows you to easily extend it. It's helpful to create your own check list for your rails team. Here is the wiki page tells you how to write your own check list, it just finds all upper case RAILS_ROOT, which should be replaced to Rails.root. Let’s look at the wiki page.\n
  • Here are some resources, website, source codes and document. If you are interested in and willing to contribute to rails_best_practices gem, I'd like that you post your best practices first on website, then implement it in the gem with good comment. It's easy to do if you spent about half an hour to read the source codes.\n
  • Thank you. And any questions?\n

rails_best practices_rubykaigi rails_best practices_rubykaigi Presentation Transcript

  • Use rails_best_practicesto refactor your rails codes flyerhzm@gmail.com Richard
Huang Proprietary
and
Confiden/al.
©
2011
OpenFeint. 1
  • About MeRichard
Huang
(flyerhzm)• hEp://huangzhimin.com• hEp://twiEer.com/flyerhzm• hEp://github.com/flyerhzmWork
for
OpenFeint Proprietary
and
Confiden/al.
©
2011
OpenFeint. 2
  • Presentation from ihower Proprietary
and
Confiden/al.
©
2011
OpenFeint. 3
  • rails_best_practices gem Proprietary
and
Confiden/al.
©
2011
OpenFeint. 4
  • rails_best_practices gem28
code
checkers~
800
watchers
on
github>
100K
downloads Proprietary
and
Confiden/al.
©
2011
OpenFeint. 5
  • rails-bestpractices.com Proprietary
and
Confiden/al.
©
2011
OpenFeint. 6
  • rails-bestpractices.com Proprietary
and
Confiden/al.
©
2011
OpenFeint. 7
  • rails-bestpractices.com70
prac/cesgem
documentgem
blog Proprietary
and
Confiden/al.
©
2011
OpenFeint. 8
  • How rails_best_practices gem works Proprietary
and
Confiden/al.
©
2011
OpenFeint. 9
  • Usagegem
install
rails_best_prac/cescd
rails_projectrails_best_prac/ces Proprietary
and
Confiden/al.
©
2011
OpenFeint. 10
  • ResultProprietary
and
Confiden/al.
©
2011
OpenFeint. 11
  • Html ResultProprietary
and
Confiden/al.
©
2011
OpenFeint. 12
  • Why should userails_best_practices gem Proprietary
and
Confiden/al.
©
2011
OpenFeint. 13
  • We Are Ruby/Rails Developers Write
Beau/ful
Codes Follow
Best
Prac/ces Proprietary
and
Confiden/al.
©
2011
OpenFeint. 14
  • We Are a TeamCode
Conven/onCode
Review Proprietary
and
Confiden/al.
©
2011
OpenFeint. 15
  • Javacheckstyle Proprietary
and
Confiden/al.
©
2011
OpenFeint. 16
  • RubySaikuroFlogFlayReekRoodi Proprietary
and
Confiden/al.
©
2011
OpenFeint. 17
  • rails_best_practices gemCheck
rails
codes
qualityCreate
check
list
for
rails
team Proprietary
and
Confiden/al.
©
2011
OpenFeint. 18
  • How rails_best_practice gem works Proprietary
and
Confiden/al.
©
2011
OpenFeint. 19
  • 1 2Codes rbp
gem sexp 3 4 Lexical 5 6Analyze
 Prepare Result 7 9 8 Review Proprietary
and
Confiden/al.
©
2011
OpenFeint.
  • 1 2Codes rbp
gem sexp LexicalAnalyze
 Prepare Result Review Proprietary
and
Confiden/al.
©
2011
OpenFeint.
  • 1 2Codes rbp
gem sexp 3 4 LexicalAnalyze
 Prepare Result Review Proprietary
and
Confiden/al.
©
2011
OpenFeint.
  • 1 2Codes rbp
gem sexp 3 4 Lexical 5 6Analyze
 Prepare Result Review Proprietary
and
Confiden/al.
©
2011
OpenFeint.
  • 1 2Codes rbp
gem sexp 3 4 Lexical 5 6Analyze
 Prepare Result 7 9 8 Review Proprietary
and
Confiden/al.
©
2011
OpenFeint.
  • sexp class Post < ActiveRecord::Base   attr_accessor :editable ends(:class, :Post,  s(:colon2, s(:const, :ActiveRecord), :Base),  s(:scope,    s(:call, nil, :attr_accessor,      s(:arglist, s(:lit, :editable))    )  )) Proprietary
and
Confiden/al.
©
2011
OpenFeint. 25
  • sexp def test   puts "hello world" ends(:defn, :test, s(:args),  s(:scope,    s(:block,      s(:call, nil, :puts,        s(:arglist, s(:str, "hello world"))      )    )  )) Proprietary
and
Confiden/al.
©
2011
OpenFeint. 26
  • Play with sexp ruby_parserhEps://github.com/flyerhzm/rails_best_prac/ces/blob/master/lib/rails_best_prac/ces/core/runner.rb#L104 Proprietary
and
Confiden/al.
©
2011
OpenFeint. 27
  • More Optionsrails_best_prac/cesrails_best_prac/ces
‐f
htmlrails_best_prac/ces
‐f
html
‐‐with‐textmaterails_best_prac/ces
‐f
html
‐‐with‐mvim Proprietary
and
Confiden/al.
©
2011
OpenFeint. 28
  • Configurablerails_best_prac/ces
‐gmvim
config/rails_best_prac/ces.ymlrails_best_prac/ces Proprietary
and
Confiden/al.
©
2011
OpenFeint. 29
  • ExtensibilityHow
to
write
your
own
check
list? RAILS_ROOT

=>

Rails.root Proprietary
and
Confiden/al.
©
2011
OpenFeint. 30
  • Resource• rails
best
prac/ces
Website hEp://rails‐bestprac/ces.com/
• rails_best_prac/ces
Source
Codes hEps://github.com/flyerhzm/rails_best_prac/ces• rails_best_prac/ces
Document hEp://rdoc.rails‐bestprac/ces.com/03/23/11 Proprietary
and
Confiden/al.
©
2010
Aurora
Feint. 31
  • Thank you Questions?Proprietary
and
Confiden/al.
©
2011
OpenFeint. 32