Excellent
Me
                   Marco Otte-Witte

•   Freelancer since 2007
•   Web Development since ~1998
•   PHP => (Java) => .NET => Ruby (on Rails)


•   http://simplabs.com
•   http://xing.com/profile/Marco_OtteWitte
•   http://github.com/marcoow
Excellent
•   static code analysis for Ruby
•   like roodi, flog, reek etc. but more


•   static code analysis for Rails


•   (it‘s still beta despite the 1.5.4 - be kind)
Static Analysis

•   it‘s not about finding errors
•   it‘s no red/ green, no fail/ pass


•   it‘s about getting hints on possible problems
•   don‘t try to code for zero warnings!
Complexity Measures
global.checkEmail = function(e) {
    function checkChars(s, i, l) {
        while (i < l && "_-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(s.charAt(i)) != -1)
        {
            i = ++i;
        } // end while
        return (i);
    }
 
    function checkFirstLevelDomainChars(s, i, l)
    {
        while (i < l && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(s.charAt(j)) != -1)
        {
            i = ++i;
        } // end while
        return (i == l);
    } // End of the function
    var _loc1;
    var j;
    var _loc2 = e.length;
    var _loc4 = false;
    _loc1 = checkChars(e, 0, _loc2);
    if (checkChars(e, 0, _loc2) == 0)
    {
        return (-1);
    } // end if
    j = _loc1;
    while (_loc1 < _loc2 && e.charAt(_loc1) == ".")
    {

    ...
Complexity Measures

•   Cyclomatic Complexity
•   ABC metric
•   Flog
•   also: method line count, even class name check
•   etc.
Rails specific checks
•   no restriction on (sometimes helpful, sometime not
    so helpful) complexity measures etc.
•   Rails checks can be red/ green
•   no magic, finds stuff you forgot/ didn‘t know about


•   AttrAccessibleCheck: specify attr_accessible!
•   ValidationsCheck: validate!
•   InstanceVarInPartialCheck: don‘t use instance vars
    in partials!
Rails specific checks
1   class ShoppingBasket < ActiveRecord::Base
2    
3     def initialize(items = [])
4       self.items = items
5     end
6    
7   end
Rails specific checks
1   class ShoppingBasket < ActiveRecord::Base
2    
3     def initialize(items = [])
4       self.items = items
5     end
6    
7   end



$ excellent shopping_basket.rb
 
  Excellent result:
 
  test.rb
    * Line   1: ShoppingBasket does not validate any attributes.
    * Line   1: ShoppingBasket defines initialize method.
    * Line   1: ShoppingBasket does not specify attr_accessible.
 
  Found 3 warnings.
How does it work?

•   ruby_parser
•   sexp_processor
•   Stack of context infos
•   simple checks on these contexts
Run it via rake

1   require 'simplabs/excellent/rake'
2
3   desc 'Analyse the Excellent source with itself.'
4   Simplabs::Excellent::Rake::ExcellentTask.new(:excellent) do |t|
5     t.html = 'doc/excellent.html'
6     t.paths = ['lib']
7   end
with HTML reports
Get it

•   http://simplabs.github.com/excellent/
Get it

•   http://simplabs.github.com/excellent/

$ sudo gem install gemcutter
$ sudo gem tumble
$ sudo gem install excellent
Resources

•   http://simplabs.github.com/excellent/
•   http://wiki.github.com/simplabs/excellent
•   http://static.simplabs.com/rails3-0-unstable.html


•   http://parsetree.rubyforge.org/

Excellent

  • 1.
  • 2.
    Me Marco Otte-Witte • Freelancer since 2007 • Web Development since ~1998 • PHP => (Java) => .NET => Ruby (on Rails) • http://simplabs.com • http://xing.com/profile/Marco_OtteWitte • http://github.com/marcoow
  • 3.
    Excellent • static code analysis for Ruby • like roodi, flog, reek etc. but more • static code analysis for Rails • (it‘s still beta despite the 1.5.4 - be kind)
  • 4.
    Static Analysis • it‘s not about finding errors • it‘s no red/ green, no fail/ pass • it‘s about getting hints on possible problems • don‘t try to code for zero warnings!
  • 5.
    Complexity Measures global.checkEmail =function(e) { function checkChars(s, i, l) { while (i < l && "_-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(s.charAt(i)) != -1) { i = ++i; } // end while return (i); }   function checkFirstLevelDomainChars(s, i, l) { while (i < l && "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(s.charAt(j)) != -1) { i = ++i; } // end while return (i == l); } // End of the function var _loc1; var j; var _loc2 = e.length; var _loc4 = false; _loc1 = checkChars(e, 0, _loc2); if (checkChars(e, 0, _loc2) == 0) { return (-1); } // end if j = _loc1; while (_loc1 < _loc2 && e.charAt(_loc1) == ".") { ...
  • 6.
    Complexity Measures • Cyclomatic Complexity • ABC metric • Flog • also: method line count, even class name check • etc.
  • 7.
    Rails specific checks • no restriction on (sometimes helpful, sometime not so helpful) complexity measures etc. • Rails checks can be red/ green • no magic, finds stuff you forgot/ didn‘t know about • AttrAccessibleCheck: specify attr_accessible! • ValidationsCheck: validate! • InstanceVarInPartialCheck: don‘t use instance vars in partials!
  • 8.
    Rails specific checks 1 class ShoppingBasket < ActiveRecord::Base 2   3   def initialize(items = []) 4     self.items = items 5   end 6   7 end
  • 9.
    Rails specific checks 1 class ShoppingBasket < ActiveRecord::Base 2   3   def initialize(items = []) 4     self.items = items 5   end 6   7 end $ excellent shopping_basket.rb     Excellent result:     test.rb     * Line 1: ShoppingBasket does not validate any attributes.     * Line 1: ShoppingBasket defines initialize method.     * Line 1: ShoppingBasket does not specify attr_accessible.     Found 3 warnings.
  • 10.
    How does itwork? • ruby_parser • sexp_processor • Stack of context infos • simple checks on these contexts
  • 11.
    Run it viarake 1 require 'simplabs/excellent/rake' 2 3 desc 'Analyse the Excellent source with itself.' 4 Simplabs::Excellent::Rake::ExcellentTask.new(:excellent) do |t| 5 t.html = 'doc/excellent.html' 6 t.paths = ['lib'] 7 end
  • 12.
  • 13.
    Get it • http://simplabs.github.com/excellent/
  • 14.
    Get it • http://simplabs.github.com/excellent/ $ sudo gem install gemcutter $ sudo gem tumble $ sudo gem install excellent
  • 15.
    Resources • http://simplabs.github.com/excellent/ • http://wiki.github.com/simplabs/excellent • http://static.simplabs.com/rails3-0-unstable.html • http://parsetree.rubyforge.org/