while writing Ruby code for the last three years now, I think I have a solid knowledge, how Ruby code and projects should be written. But who am I to think, that I do everything perfect. No. I know, that there are always flaws in my code, your code, everybody’s code.
That’s the reason why there are code metric and analyzing tools for each programming language. For Ruby there are also several. I started using four of them. For convenience purposes, I created a small git repository with some rake tasks. You can find it here on github. I used it in the sample output below.
reek is an awesome code smell detector written by Kevin Rutherford and now maintained by Timo Rößner. It actually is looking for code smells like Duplication, Long Method, Large Class, Simulated Polymorphism and the like. Check out the code smells Wiki page for more info. I strongly recommend optimizing your code to avoid these smells.
$ rake analyzer:reek Running reek and find code smells app/controllers/application_controller.rb -- 2 warnings: ApplicationController has no descriptive comment (IrresponsibleModule) ApplicationController#render_500 has the name 'render_500' (UncommunicativeMethodName) app/controllers/news_controller.rb -- 2 warnings: NewsController has no descriptive comment (IrresponsibleModule) NewsController#save_newsletter_registration calls flash 3 times (DuplicateMethodCall)
rails_best_practices is a tool to check the quality of Rails code. It is written by @flyerhzm. It tells you, where you can optimize your Rails code by providing detection for e.g. MoveModelLogicIntoModelReview, RemoveUnusedMethodsInControllersReview, ReplaceComplexCreationWithFactoryMethodReview and many others.
$ rake analyzer:rails_best_practices Running rails_best_practices and inform about found issues Source Codes: 100% |ooooooooooo| Time: 0:00:02 app/models/image.rb:11 - change Hash Syntax to 1.9 app/models/image.rb:19 - change Hash Syntax to 1.9 app/controllers/pages_controller.rb:59 - move model logic into model (page use_count > 4) app/controllers/admin/galleries_controller.rb:20 - remove unused methods (Admin::GalleriesController#create)
First of all, I really like the website of the RubySadists. I guess these two guys have a lot of fun working on Ruby projects.
flay is detecting structural similarities in your code. The found issues are good candidates for refactoring.
$ rake analyzer:flay Running flay and and analyze code for structural similarities flay app/* 1) Similar code found in :defn (mass = 87) app/controllers/admin/images_controller.rb:11 app/controllers/admin/news_controller.rb:19 app/controllers/admin/uploads_controller.rb:31 2) Similar code found in :defn (mass = 78) app/controllers/admin/galleries_controller.rb:45 app/controllers/admin/news_controller.rb:40 app/controllers/admin/uploads_controller.rb:41
This also a library from the RubySadists. It simply shows you shitty code. This is a good starting point to rethink what the heck you have coded ;-). As an indicator, a number is used to show how ugly the code at the found line is. The higher the number, the worse it is.
rake analyzer:flog Running flog and find the most tortured code flog -cad app/models/*.rb 130.7: flog total 4.4: flog/method average 11.8: Image#none 2.5: assignment 2.3: branch 1.5: max 1.4: sorting 1.3: + 1.2: nil? 1.1: before_save 1.1: mount_uploader 1.1: validates 1.1: belongs_to 1.1: attr_accessible 0.4: lit_fixnum 8.1: Image::json_data app/models/image.rb:17 1.6: array_of_images_with_id_and_path 1.4:  1.4: joins 1.2: where 1.2: new 1.2: assignment 1.2: to_json 8.0: ImageSorter#change_position app/models/image_sorter.rb:26 2.8: sorting 2.4: assignment 2.4: update_image
These four code metric tools are just a few out of a bunch of tools. But it’s always the same. It’s better than nothing. And here’s a challange: try to fix all found issues in one of your smaller projects. How longe did it take? A day? A week? A month? The most important part of the story is the fact, that you will remember what you did wrong the next time you open your code editor and start hacking on a Ruby project.
EDIT: you can see the analyzer in action here: http://shelr.tv/records/4ffa15da9660805a7a000005