<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>Ruby Best Practices</title><link>http://blog.rubybestpractices.com/</link><description>Increase your productivity -- Write Better Code</description><language>en-us</language><item><title>Sexp for Rubyists</title><description>&lt;p&gt;&lt;em&gt;Sorry to interrupt you, but you&amp;#8217;ve totally lost me. What &lt;strong&gt;is&lt;/strong&gt; this &amp;#8220;Sexp&amp;#8221;&lt;br /&gt;
you&amp;#8217;re speaking of? I&amp;#8217;ve heard it before, but never quite understood it&amp;#8230;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Oh, don&amp;#8217;t feel sorry!  It&amp;#8217;s still quite esoteric for Rubyists.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Yeah&amp;#8230;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Okay.  Let&amp;#8217;s start at the beginning.  Lisp!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;With all the parenthesis?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Bingo!  Have you tried it?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Not really.  It seems a little too &amp;#8220;hardcore&amp;#8221;, if you know what I mean?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Ah, yes. It&amp;#8217;s just a neat little language, nothing to be afraid of. Let me&lt;br /&gt;
show you an example:&lt;/p&gt;
&lt;pre&gt;
(define (plus-five n)
  (+ n 5))
&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;It&amp;#8217;s a method that adds five to the argument?  Like this:&lt;/em&gt;&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
def plus_five(n)
  n + 5
end
&lt;/pre&gt;
&lt;p&gt;Yep, but more importantly: It&amp;#8217;s also an example of S-expressions. Because Sexp&lt;br /&gt;
is really just a data format.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Data format?  Like &lt;span class="caps"&gt;YAML&lt;/span&gt;?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Just like &lt;span class="caps"&gt;YAML&lt;/span&gt;.  It has support for numbers, symbols, pairs, lists and nil.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Really?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let me explain a few more things. You see, Lisp wasn&amp;#8217;t originally supposed to&lt;br /&gt;
be programmed in S-expression. Nope, the idea was to have complex,&lt;br /&gt;
human-readable &lt;em&gt;M-expressions&lt;/em&gt; which should manipulate data in S-expressions.&lt;/p&gt;
&lt;pre&gt;
    Data  =  S-expressions
    Code  =  M-expressions
&lt;/pre&gt;
&lt;p&gt;Another idea was that M-expressions could easily be compiled into&lt;br /&gt;
S-expressions. So the computer itself would only work with S-expressions,&lt;br /&gt;
while humans could focus on M-expressions.&lt;/p&gt;
&lt;pre&gt;
    M-expressions  --&amp;gt;   S-expressions
&lt;/pre&gt;
&lt;p&gt;However, the first Lisp implementations only accepted S-expressions since it&lt;br /&gt;
was easier to start from there and rather add M-expression support later.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;So what happend with these M-expressions?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;They were &amp;#8220;receded into the indefinite future&amp;#8221; as the creator, John McCarthy,&lt;br /&gt;
said. It turned out that S-expressions were readable enough.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Go on, please.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now Lisp suddenly used S-expressions for both code and data. The interesting&lt;br /&gt;
thing is how code was represented in Lisp. It&amp;#8217;s in fact very simple: The first&lt;br /&gt;
element in the list is the operator/function and the rest is the&lt;br /&gt;
operands/arguments:&lt;/p&gt;
&lt;pre&gt;
    (+ 1 2)
    (plus-five 5)
    (factorial 10)
&lt;/pre&gt;
&lt;p&gt;This makes Lisp a very simple language to implement. You need very few&lt;br /&gt;
primitives before you have a fully working Lisp system. In fact, I would&lt;br /&gt;
recommend watching James Coglan&amp;#8217;s screencast. There isn&amp;#8217;t any audio since he&lt;br /&gt;
used it in a talk he gave, but it&amp;#8217;s still fairy interesting.&lt;/p&gt;
&lt;p&gt;&lt;object width="581" height="421"&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=4339116&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=00ADEF&amp;amp;fullscreen=1" /&gt;&lt;embed src="http://vimeo.com/moogaloop.swf?clip_id=4339116&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=00ADEF&amp;amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="581" height="421"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;p&gt;&lt;a href="http://vimeo.com/4339116"&gt;Scheme interpreter in 15 minutes&lt;/a&gt; from &lt;a href="http://vimeo.com/jcoglan"&gt;James Coglan&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Wow.  I must definitely check out Lisp.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Indeed. He&amp;#8217;s also written a &amp;#8220;real&amp;#8221; Lisp implementation in Ruby if you&amp;#8217;re&lt;br /&gt;
interested: &lt;a href="http://github.com/jcoglan/heist"&gt;Heist&lt;/a&gt;. Oh, and I&amp;#8217;ll also have to&lt;br /&gt;
recommend &lt;a href="http://www.ccs.neu.edu/home/matthias/BTLS/"&gt;The Little Schemer&lt;/a&gt; by&lt;br /&gt;
Daniel P. Friedman and Matthias Felleisen, it has inspired me in several&lt;br /&gt;
ways&amp;#8230;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I will, but back to Sexp?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;No.  Let&amp;#8217;s talk about &lt;span class="caps"&gt;AST&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;span class="caps"&gt;AST&lt;/span&gt;? While &lt;strong&gt;I&lt;/strong&gt; certainly appreciate these digressions, (&lt;/em&gt;whispers_) others&lt;br /&gt;
might be a little bored, you know?_&lt;/p&gt;
&lt;p&gt;Don&amp;#8217;t worry, this is pretty essential. An Abstract Syntax Tree is a tree&lt;br /&gt;
representation of source code. Compilers and interpreters never work directly&lt;br /&gt;
on the source code, but parses it into an &lt;span class="caps"&gt;AST&lt;/span&gt; which is much easier work with.&lt;br /&gt;
Your Ruby example above could look something like:&lt;/p&gt;
&lt;pre&gt;
[Def,
  plus_five
  [n]
  [Call, n, +, [[Lit, 5]]]]
&lt;/pre&gt;
&lt;p&gt;The basic idea here is that we only deal with &lt;em&gt;nodes&lt;/em&gt; like Def, Call and Lit,&lt;br /&gt;
and every node has a set of &lt;em&gt;children&lt;/em&gt;. In this tree the nodes are built like&lt;br /&gt;
this:&lt;/p&gt;
&lt;pre&gt;
[Def, method_name, arguments, body] # =&amp;gt; def method_name(*arguments); body; end
[Lit, number]                       # =&amp;gt; (integer)
[Call, receiver, method_name, arguments] # =&amp;gt; receiver.method_name(*arguments)
&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;I think I get your point.  This is very similar to S-expressions!&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;
(def plus_five (n)
  (call n + 5))
&lt;/pre&gt;
&lt;p&gt;Yes! It turns out that S-expressions are excellent to represent abstract&lt;br /&gt;
syntax trees. And when compiler gurus talk about S-expressions, they don&amp;#8217;t&lt;br /&gt;
talk about the file format, they talk about the way Lisp represent it&amp;#8217;s code:&lt;br /&gt;
The first element in the list is the operator/function and the rest are the&lt;br /&gt;
operands/arguments.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;So S-expressions themselves aren&amp;#8217;t very exciting, it all depends on what you&lt;br /&gt;
put in them?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Exactly.  The Sexp-class basically looks like this:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class Sexp &amp;lt; Array
  def sexp_type
    self[0]
  end
  
  def sexp_body
    self[1..-1]
  end
end
&lt;/pre&gt;
&lt;p&gt;A glorified Array.  That&amp;#8217;s all there is to it.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;And what do people put in these Sexps?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let me show you an example. First, &lt;code&gt;gem install ruby_parser&lt;/code&gt;. Then:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
require 'ruby_parser'
require 'pp'

pp RubyParser.new.parse(&amp;lt;&amp;lt;-EOF)
def plus_five(n)
  n + 5
end
EOF
&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Interesting:&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;
s(:defn,
 :plus_five,
 s(:args, :n),
 s(:scope,
   s(:block,
     s(:call, s(:lvar, :n), :+, s(:arglist, s(:lit, 5))))))
&lt;/pre&gt;
&lt;p&gt;This is the &lt;em&gt;syntatic&lt;/em&gt; strucure of our code. It&amp;#8217;s not possible to reconstruct&lt;br /&gt;
the original snippet character-by-character; you only get what the&lt;br /&gt;
compiler/interpreter/virtual machine actually sees. And often that&amp;#8217;s exactly&lt;br /&gt;
the only thing we want.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Aha.  Who decided this structure and these names by the way?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Well, I&amp;#8217;m not quite certain, but I believe ParseTree by Ryan Davis started it&lt;br /&gt;
all.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Oh, ParseTree sounds familiar.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It extracts the raw &lt;span class="caps"&gt;AST&lt;/span&gt; that Ruby 1.8 uses internally. It&amp;#8217;s not very elegant,&lt;br /&gt;
so Ryan also wrote UnifiedRuby which cleans it up a bit. RubyParser returns&lt;br /&gt;
such a cleanup up Sexp.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;And what do people use these cleaned up Sexps for?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;There&amp;#8217;s plenty of projects floating around, here&amp;#8217;s some I&amp;#8217;ve heard of:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://ruby.sadi.st/Flog.html"&gt;Flog&lt;/a&gt; analyzes your code and shows how complex your methods are, and which you should refactor.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://ruby.sadi.st/Flay.html"&gt;Flay&lt;/a&gt; analyzes your code for structural similarities, ready to be DRYed out!&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/martinjandrews/roodi"&gt;Roodi&lt;/a&gt; warns you about design issues.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/kevinrutherford/reek"&gt;Reek&lt;/a&gt; detects code smells.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://saikuro.rubyforge.org/"&gt;Saikuro&lt;/a&gt; is a &amp;#8220;cyclomatic complexity analyzer&amp;#8221;.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://ambition.rubyforge.org/"&gt;Ambition&lt;/a&gt; let&amp;#8217;s you write &lt;span class="caps"&gt;SQL&lt;/span&gt; statements as Ruby blocks.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/judofyr/parkaby"&gt;Parkaby&lt;/a&gt; compiles Markaby-like code into super-fast code.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/raganwald/rewrite_rails"&gt;RewriteRails&lt;/a&gt; adds syntactic abstractions (like Andand) to Rails projects without monkey-patching.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Cool, that&amp;#8217;s more than I thought.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And if more people knew about them, I guess there would be even more.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Let me recap:&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;For Rubyists, Sexp is an Array-ish list where the first element is the&lt;br /&gt;
operator/function and the rest is the operands/arguments. The arguments can&lt;br /&gt;
also contain other Sexp. When I think about it, that&amp;#8217;s probably the most&lt;br /&gt;
important thing: They are nested.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I guess it&amp;#8217;s mostly useful when we&amp;#8217;re actually manipulating code that&lt;br /&gt;
executes. Like source code or templates?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Exactly!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Ah, excellent.  Why were we talking about Sexps again?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Hm.. Now that&amp;#8217;s a good question. I simply don&amp;#8217;t remember. Let&amp;#8217;s talk about it&lt;br /&gt;
another time, shall we?&lt;/p&gt;</description><author>judofyr@gmail.com (Magnus Holm)</author><pubDate>Thu, 04 Feb 2010 21:00:00 +0000</pubDate><link>http://blog.rubybestpractices.com/posts/judofyr/sexp-for-rubyists.html</link><guid>http://blog.rubybestpractices.com/posts/judofyr/sexp-for-rubyists.html</guid></item><item><title>Name that method: Symmetry</title><description>&lt;pre name="code" class="ruby"&gt;
class Users &amp;lt; Application
  provides :xml, :json

  def index
    @users = User.all
    display @users
  end
end
&lt;/pre&gt;
&lt;p&gt;Take a look at the snippet above. It&amp;#8217;s from Merb, and the &lt;code&gt;provides/display&lt;/code&gt; feature is something that will be implemented in Rails 3 after it was decided to merge the two projects. Don&amp;#8217;t think too much about what it does, the point is to see how it will look in Rails 3:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
class UsersController &amp;lt; ApplicationController
  respond_to :html, :xml, :json

  def index
    @users = User.find(:all)
    respond_with(@users)
  end
end  
&lt;/pre&gt;
&lt;p&gt;A subtle difference. Let&amp;#8217;s have a look at another example, this time from Camping:&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;
module Blog::Controllers
  # Define routes using R()
  class Index &amp;lt; R '/'
  end
  
  class Posts &amp;lt; R '/posts'
  end
end   
  
module Blog::Views
  def index
    # Generate URLs using R()
    a :href =&amp;gt; R(Posts) { 'Show me the posts!' }
  end
end   
&lt;/pre&gt;
&lt;p&gt;Notice how you use the class method &lt;code&gt;R&lt;/code&gt; to define routes in the controllers, and the instance method &lt;code&gt;R&lt;/code&gt; to build URLs to those routes again.&lt;/p&gt;
&lt;h3&gt;What&amp;#8217;s the deal?&lt;/h3&gt;
&lt;p&gt;I&amp;#8217;m not a very good designer, but I &lt;em&gt;have&lt;/em&gt; managed to get a hold of some of the principles. One of them is called the principle of &lt;em&gt;proximity&lt;/em&gt;, and says that &lt;strong&gt;related items should be grouped together&lt;/strong&gt;. We programmers already do this all the time: We group our code into classes and modules and separate them into different files and folders. However, this same principle can be applied when it comes to method names: &lt;strong&gt;related methods should have related names&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&amp;#8220;Provide&amp;#8221; and &amp;#8220;display&amp;#8221; are neither synonyms nor antonyms, nor do they share a common prefix/suffix. They are however closely related and obviously symmetric. &lt;strong&gt;That symmetry should be reflected in their names.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When you quickly glance over some code, your brain starts creating connections between different pieces and how they interact with each other. A well named method makes it far easier to understand the flow, while a poorly named one means you might have to start looking a completely different place while still be able to quickly jump back and continue interpreting the rest. By naming related methods alike, you make the connections more obvious and easier to spot.&lt;/p&gt;
&lt;p&gt;It may not seem like a big deal, but it certainly makes your code look cleaner and easier to understand.&lt;/p&gt;</description><author>judofyr@gmail.com (Magnus Holm)</author><pubDate>Fri, 05 Jun 2009 19:39:00 +0000</pubDate><link>http://blog.rubybestpractices.com/posts/judofyr/symmetry.html</link><guid>http://blog.rubybestpractices.com/posts/judofyr/symmetry.html</guid></item></channel></rss>