Misc

  • Write ruby -w safe code. [link]

  • Avoid hashes as optional parameters. Does the method do too much? (Object initializers are exceptions for this rule). [link]

  • Avoid methods longer than 10 LOC (lines of code). Ideally, most methods will be shorter than 5 LOC. Empty lines do not contribute to the relevant LOC. [link]

  • Avoid parameter lists longer than three or four parameters. [link]

  • If you really need "global" methods, add them to Kernel and make them private. [link]

  • Use module instance variables instead of global variables. [link]

    # bad
    $foo_bar = 1
    
    # good
    module Foo
      class << self
        attr_accessor :bar
      end
    end
    
    Foo.bar = 1
    
  • Use OptionParser for parsing complex command line options and ruby -s for trivial command line options. [link]

  • Prefer Time.now over Time.new when retrieving the current system time. [link]

  • Code in a functional way, avoiding mutation when that makes sense. [link]

  • Do not mutate parameters unless that is the purpose of the method. [link]

  • Avoid more than three levels of block nesting. [link]

  • Be consistent. In an ideal world, be consistent with these guidelines. [link]

  • Use common sense. [link]