SlideShare une entreprise Scribd logo
1  sur  32
Use rails_best_practices
to refactor your rails codes


      flyerhzm@gmail.com
         Richard
Huang



       Proprietary
and
Confiden/al.
©
2011
OpenFeint.   1
About Me

Richard
Huang
(flyerhzm)
• hEp://huangzhimin.com
• hEp://twiEer.com/flyerhzm
• hEp://github.com/flyerhzm

Work
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 gem


28
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.com

70
prac/ces

gem
document

gem
blog


        Proprietary
and
Confiden/al.
©
2011
OpenFeint.   8
How rails_best_practices gem
          works




        Proprietary
and
Confiden/al.
©
2011
OpenFeint.   9
Usage

gem
install
rails_best_prac/ces

cd
rails_project

rails_best_prac/ces


         Proprietary
and
Confiden/al.
©
2011
OpenFeint.   10
Result




Proprietary
and
Confiden/al.
©
2011
OpenFeint.   11
Html Result




Proprietary
and
Confiden/al.
©
2011
OpenFeint.   12
Why should use
rails_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 Team


Code
Conven/on

Code
Review



       Proprietary
and
Confiden/al.
©
2011
OpenFeint.   15
Java
checkstyle




        Proprietary
and
Confiden/al.
©
2011
OpenFeint.   16
Ruby

Saikuro
Flog
Flay
Reek
Roodi


          Proprietary
and
Confiden/al.
©
2011
OpenFeint.   17
rails_best_practices gem



Check
rails
codes
quality

Create
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                                                   2
Codes                    rbp
gem                                       sexp
                                       3
           4
                           Lexical
                                       5

                                                                   6
Analyze
                 Prepare
 Result                                7
           9                                                       8
                          Review

               Proprietary
and
Confiden/al.
©
2011
OpenFeint.
1                                                   2
Codes                    rbp
gem                                   sexp

                           Lexical


Analyze
                 Prepare
 Result
                          Review

               Proprietary
and
Confiden/al.
©
2011
OpenFeint.
1                                                   2
Codes                    rbp
gem                                   sexp
                                       3
           4
                           Lexical


Analyze
                 Prepare
 Result
                          Review

               Proprietary
and
Confiden/al.
©
2011
OpenFeint.
1                                                   2
Codes                    rbp
gem                                       sexp
                                       3
           4
                           Lexical
                                       5

                                                                   6
Analyze
                 Prepare
 Result
                          Review

               Proprietary
and
Confiden/al.
©
2011
OpenFeint.
1                                                   2
Codes                    rbp
gem                                       sexp
                                       3
           4
                           Lexical
                                       5

                                                                   6
Analyze
                 Prepare
 Result                                7
           9                                                       8
                          Review

               Proprietary
and
Confiden/al.
©
2011
OpenFeint.
sexp
       class Post < ActiveRecord::Base
         attr_accessor :editable
       end




s(: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"
       end




s(: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_parser

hEps://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 Options


rails_best_prac/ces
rails_best_prac/ces
‐f
html
rails_best_prac/ces
‐f
html
‐‐with‐textmate
rails_best_prac/ces
‐f
html
‐‐with‐mvim



            Proprietary
and
Confiden/al.
©
2011
OpenFeint.   28
Configurable

rails_best_prac/ces
‐g

mvim
config/rails_best_prac/ces.yml

rails_best_prac/ces


            Proprietary
and
Confiden/al.
©
2011
OpenFeint.   29
Extensibility

How
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

Contenu connexe

Plus de Richard Huang (7)

Rails project code review
Rails project code reviewRails project code review
Rails project code review
 
使用Rails best practices做代码审查
使用Rails best practices做代码审查使用Rails best practices做代码审查
使用Rails best practices做代码审查
 
Rails bestpractices.com
Rails bestpractices.comRails bestpractices.com
Rails bestpractices.com
 
Ruby application based on http
Ruby application based on httpRuby application based on http
Ruby application based on http
 
Css sprite best practices
Css sprite best practicesCss sprite best practices
Css sprite best practices
 
Static Code Analysis For Ruby
Static Code Analysis For RubyStatic Code Analysis For Ruby
Static Code Analysis For Ruby
 
Email Contacts For Invitation
Email Contacts For InvitationEmail Contacts For Invitation
Email Contacts For Invitation
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

rails_best practices_rubykaigi

Notes de l'éditeur

  1. 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&apos;d like to give you an introduction about this gem, I think you will like it)\n
  2. 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&apos;m now working for OpenFeint, it&apos;s the largest mobile gaming network.\n
  3. 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&apos;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
  4. 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&apos;t implement all of the practices from Wen-Tian Chang&apos;s presentation because some are too difficult to check automatically, but I also added some other useful practices to the gem.\n
  5. Now, it contains 28 code checkers, nearly 800 watchers on github, and more than 1 hundred thousand downloads.\n
  6. 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
  7. This is one of the best practices on webiste, named use query attribute, in common, the practice will show you what&amp;#x2019;s the bad smell, and how to refactor it.\n
  8. Now there are 70 practices on the website, I can&amp;#x2019;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
  9. Now, let me show you how rails_best_practices gem works.\n
  10. First, you should gem install rails_best_practices, go to a rails project directory, type rails_best_practices command.\n
  11. It&apos;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
  12. rails_best_practices gem also provides a visual result page. It&apos;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&amp;#x2019;s try it now.\n
  13. Why I think we should use the rails_best_practices gem?\n
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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&apos;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
  19. If you are interested in writing your own check list, you should know a bit about how rails_best_practices gem works internally?\n
  20. 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
  21. First step, the gem reads source codes of rails project, \nthen, the gem translates the source codes into sexp\n
  22. The third step is lexical, it only analyze the code formats, like if the codes contain the trailing white space, it doesn&apos;t depend on sexp, just reads the content of source codes, and outputs to the analyze result if necessary. \n
  23. 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
  24. 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
  25. Let&apos;s have a look what&apos;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
  26. Here is another example, it&apos;s a method definition, method name is test, with empty arguments, contains a code block, call a method puts with argument hello world.\n
  27. I don&apos;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
  28. 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
  29. You may not like all of the default code checkers, it&apos;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&apos;t like. Save it, then when you use rails_best_practices again, the commented or deleted code checkers will not take effect. Let&amp;#x2019;s try it now.\n
  30. What about the code checkers that you want but not exist in the rails_best_practices default checkers? Don&apos;t worry, rails_best_practices gem allows you to easily extend it. It&apos;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&amp;#x2019;s look at the wiki page.\n
  31. Here are some resources, website, source codes and document. If you are interested in and willing to contribute to rails_best_practices gem, I&apos;d like that you post your best practices first on website, then implement it in the gem with good comment. It&apos;s easy to do if you spent about half an hour to read the source codes.\n
  32. Thank you. And any questions?\n