Ruby Style Guides and Tools: How to Write Good Looking Ruby
Last week, Noel Rappin of Pathfinder Development wrote Elements of Ruby Style - an attempt at producing a Ruby "style guide." After some initial feedback to this, he's followed up with a response to some of the initial criticisms and suggestions.
Noel isn't the first to try to develop a Ruby style guide. In an attempt to promote Ruby's use within Google, Ian Macdonald wrote an extensive Ruby style guide back in 2006 (rather sadly, it appears his attempt did not work out).
What's a "Style Guide" then?
For those unfamiliar with style guides, the most famous for the English language are probably Strunk & White's The Elements of Style and The Chicago Manual of Style (my personal favorite) - both attempt to define how to write and lay out English texts properly. Style guides for programming languages, however, usually attempt to not only define the "correct" way to write and lay out code but also the right idioms to use in certain situations.
Other Tools
Ruby style and "code smells" have become topics of interest for many Ruby developers recently:
- Last month, Kevin Rutherford released reek, a "code smells detector"
- Marty Andrews released Roodi, a tool that checks Ruby code for complexity
- flog is also a popular Ruby tool for testing and showing the complexity of your Ruby code.
Supported by Media72 Hosting: Looking for reliable UK Ruby on Rails hosting? Packages start from just £7/month on our mod_rails/apache stack hosted on fast UK servers and include free 24/7 support.
October 27, 2008 at 3:24 pm
Blatant self-promotion, and it's still mostly experimental status, but there's also Towelie, my repetition detector. It's on GitHub and my blog. It finds duplicate method definitions effortlessly, and reports them cleanly. It can also find highly similar method definitions, but both the interface and the reporting for that are very clumsy. Lots of rough edges on this code but it works very well for finding cut-and-paste in legacy codebases.
http://gilesbowkett.blogspot.com/search?q=towelie
October 27, 2008 at 3:51 pm
There's also Flay, although the project hasn't released any code yet:
http://blog.zenspider.com/2008/09/flay-is-coming.html
This is a repetition detector like Towelie, but they didn't just copy the idea. It also can recognize when different code does the same thing, and mark that as repetition. That's something Towelie can't do. Unfortunately it still has unicorn status until they release something. :-p
October 27, 2008 at 3:55 pm
As always, nothing to say about the interesting content.
But could you please stop leaving colored ads in http://planetrubyonrails.com/?
Thanks,
Eric
October 27, 2008 at 5:59 pm
Eric: Unfortunately I've had to add styles directly to the paragraph because otherwise it doesn't work in feeds. PlanetRubyOnRails.com seems to take the code in feeds as verbatim for HTML purposes (which has caused the whole front page to end up in bold today, as someone posted a broken item to their feed) so all that goes through with it. Ideally PlanetRubyOnRails would sanitize the HTML formatting.
October 28, 2008 at 3:37 pm
I find duplicate method definitions with -w.
And fsck that 2 space indentation crap. Just because that's what the Rails core developers use, doesn't mean you should shut your brain off and do what they do.
I'm not giving up 3 space indentation. I don't give a crap what the Rails core developers (or Matz) do. My style is my own.
October 30, 2008 at 4:10 pm
Personally I love how the Ruby community has unconditionally decided on 2 spaces for indentation. It sidesteps soooo many tedious arguments that other languages are bogged down with.