Sunday, October 19, 2008

If you smell something stinking, flog it!!!

Recently, I joined a new project. Before I get started, my tech lead asked me to go through the code first and find complex parts of it, so that during development I will be careful about those areas.

I decided to flog it and I came across Ryan Davis's flog tool.

What is Flog?
It's a tool, which helps you in finding complex areas of your code. More complex code tells you the chances of more bugs. Flog has some smart algorithm which go to methods of a file and gives points to each command/statement of that method. Say, if..else has 5 points, variable declaration has 1 point etc. So if your methods scores more means they are more complex.

How to do it?
Since I am working on windows(don't ask me why :) ), it took me some time to find how to use this tool. After a little hassle I found it and here I am showing the same, just like A, B, C. if you know a better way to do it, please do add your suggestions and comments.

Step A:
Install flog as a gem.
Open command prompt:
c:/>gem install flog

Step B:
Go to your project folder.
c:\>cd my_project

Step C:
Just flog it!!!

  • To get all the flog options:
c:\my_project>flog -h
flog options dirs_or_files
-a display all flog results, not top 60%
-h display help
-I=path extend $LOAD_PATH with path
-s display total score only
-v verbosely display progress and errors
  • If you want to flog all controllers, following will flog all the *.rb files in your apps\controllers folder:
c:\my_project>flog app\controllers\*.rb

  • To flog controllers, models, helpers and libraries, together:
c:\my project>flog app\controllers\*.rb app\models\*.rb app\helpers\*.rb lib\*.rb

Sample method and flog score:


class
PagesController
def
show
@page_title = @page.title
@page_mode = "show"
respond_to do |format|
format.html
format.atom { render :layout => false }
end
end

0 comments: