<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>H3RALD: Book Review: Writing Efficient Ruby Code</title>
    <link>http://www.h3rald.com/articles/efficient-ruby-code-shortcut-review</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Fabio Cevasco's Writings</description>
    <item>
      <title>Book Review: Writing Efficient Ruby Code</title>
      <description>&lt;p style="float:right"&gt;&lt;img src="/files/efficient_ruby_shortcut.jpeg" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;The second shortcut from Addison-Wesley Professional series I&amp;#8217;m going to review is called &lt;a href="http://www.informit.com/store/product.aspx?isbn=0321540034"&gt;Writing Efficient Ruby Code&lt;/a&gt;. A very promising title, especially considering that this book is only 50 pages long.&lt;/p&gt;


	&lt;p&gt;As usual, this shortcut can be intended as a sort of programmer-friendly detailed cheatsheet: like the other ones in this series it sports a monitor-friendly landscape layout and does not go to deep into the details unless strictly necessary to understand a particular concept.&lt;/p&gt;


	&lt;h3&gt;The Author&lt;/h3&gt;


	&lt;p&gt;&lt;a href="http://railsexpress.de/blog/"&gt;Dr. Stefan Kaes&lt;/a&gt;, the author, contributed a lot to improve Ruby on Rails&amp;#8217; performance by refactoring portions of its core and try to &amp;#8220;get maximum speed out of performance-critical sections of code&amp;#8221;. This short but interesting shortcut groups together a lot of performance tweaks, tips and tricks but also some &amp;#8220;anti-patterns&amp;#8221; Kaes was able to identify through his career as programming teacher Ruby software consultant and key Rails contributor.&lt;/p&gt;


	&lt;h3&gt;The Contents&lt;/h3&gt;


	&lt;p&gt;Like with the previously-covered &lt;a href="/articles/mongrel-shortcut-review"&gt;Mongrel shortcut&lt;/a&gt;, &lt;em&gt;Writing Efficient Ruby Code&lt;/em&gt; always goes straight to the point when it comes to identify problems. The first one mentioned is of course that the &lt;em&gt;Ruby Interpreter is Slow&lt;/em&gt;, most people are aware of that, due to their direct experience or because this argument is normally used by non-Rubyists to argue the language&amp;#8217;s usability in commercial projects. What you may not know is why that is so, and that&amp;#8217;s where the first part of this book comes into play.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;em&gt;&amp;#8220;Ruby is a highly dynamic language: Almost all language entities are first-class citizens in that they can be created, changed, and destroyed at runtime. This comprises classes, modules, methods, constants, and class and instance variables. Only local variables are second-class citizens in Ruby: Whether a name refers to a local variable is determined at parse time.&lt;/em&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;This makes Ruby extremely flexible, but also more complex. Whever you use a name to refer to an object, Ruby has to search for the object it refers to, and this costs in terms of processing time.&lt;/p&gt;


	&lt;p&gt;As a matter of fact, one of the most recurring tips in the book to improve code performance is the following:&lt;/p&gt;


	&lt;p style="text-align:center;"&gt;&lt;strong&gt;Method calls are expensive, use variables directly when possible.&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Keep this in mind: &lt;code&gt;self.something&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; the same as &lt;code&gt;@something&lt;/code&gt;. The end result is the same, but the first way costs more in terms of performance because Ruby has to look up the method name.
Similarly, &lt;strong&gt;local variables &lt;em&gt;should&lt;/em&gt; be introduced as a way to &amp;#8220;cache&amp;#8221; the result of method calls&lt;/strong&gt;. Often you may feel &amp;#8220;guilty&amp;#8221; to introduce a new variable and keep calling the same method over and over: this should definitely be avoided.&lt;/p&gt;


	&lt;p&gt;Other useful tips include, for example:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Use syntax constructs (e.g. assignments) as expressinons. Use evaluation precedences.&lt;/li&gt;
		&lt;li&gt;Use interpolated strings &lt;code&gt;"... #{string_variable}"&lt;/code&gt; (there&amp;#8217;s also no performance difference if constant strings are used between &lt;code&gt;"&lt;/code&gt; or &lt;code&gt;'&lt;/code&gt;)&lt;/li&gt;
		&lt;li&gt;Use operators which update the data structure without copying it (when possible). Use &lt;code&gt;update&lt;/code&gt; or &lt;code&gt;merge&lt;/code&gt; to update hashes.&lt;/li&gt;
		&lt;li&gt;Iterating using &lt;code&gt;for a in  A&lt;/code&gt; is slightly faster than performing the same iteration using &lt;code&gt;each&lt;/code&gt;, (it is the opposite in Ruby 1.9 though)&lt;/li&gt;
		&lt;li&gt;do not use &lt;code&gt;return&lt;/code&gt; unless you have to&lt;/li&gt;
		&lt;li&gt;test in order of expected case frequency&lt;/li&gt;
		&lt;li&gt;Use parallel assignment (&lt;code&gt;a, b = 5, 6&lt;/code&gt;) where applicable&lt;/li&gt;
		&lt;li&gt;If a module gets included in only one other class (or module), it&#8217;s preferable to open the class instead.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I deliberately chose not to elaborate any further on the tips listed above because otherwise I&amp;#8217;ll give a big chunk of the contents of the book itself. If you know Ruby enough, you may already know why such reccommendations make sense, but if you don&amp;#8217;t, &lt;em&gt;Writing Efficient Ruby Code&lt;/em&gt; can be a short but very interesting read.&lt;/p&gt;


	&lt;h3&gt;The Good&lt;/h3&gt;


	&lt;p&gt;For each of the 30 &amp;#8220;coding patterns&amp;#8221; (and consequent anti-patterns) described in the book, the author does a great job explaining the reasons of doing something in a particular way, also through examples and benchmarks, where possible.&lt;/p&gt;


	&lt;p&gt;Furthermore, this &lt;em&gt;shortcut&lt;/em&gt; can really be useful to grasp a few difference between Ruby 1.8.5, 1.8.6 and 1.9 in terms of performance: not all the patters apply to all Ruby implementations, and when that&amp;#8217;s the case it is clearly stated.&lt;/p&gt;


	&lt;h3&gt;The Bad&lt;/h3&gt;


	&lt;p&gt;My only complaint about the book is probably the lack of details and more &amp;#8220;specialized&amp;#8221; patterns. Everything (except for a few Rails-specific tips) normally apply to Ruby &lt;em&gt;as a whole&lt;/em&gt;, without going deeply to analyze specific libraries or third-party gems. As a result, once you get the general idea, some of the patters may seem pretty obvious or a logic consequence of others.&lt;/p&gt;


	&lt;p&gt;It is also true that this is meant to be a &lt;em&gt;shortcut&lt;/em&gt;, not a comprehensive analysis on code optimization techniques which can be applied to specific cases: something like this would require much more than 50 pages!&lt;/p&gt;


	&lt;h3&gt;The Bottom Line&lt;/h3&gt;


	&lt;p&gt;Read it, re-read a few bits of it to make sure you grasp the most important concepts, and keep its table of contents in front of you as a reminder when refactoring your code!&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jan 2008 05:47:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:6712bd59-655a-4682-a2f5-4dafc0c1a69b</guid>
      <author>h3rald</author>
      <link>http://www.h3rald.com/articles/efficient-ruby-code-shortcut-review</link>
      <category>Articles</category>
      <category>ruby</category>
      <category>review</category>
      <category>books</category>
      <trackback:ping>http://www.h3rald.com/trackback/entries/151</trackback:ping>
    </item>
    <item>
      <title>"Book Review: Writing Efficient Ruby Code" by Fabio Cevasco</title>
      <description>&lt;p&gt;Matthew: if you can give me your email address (e.g. via your next comment, it won&amp;#8217;t be displayed publicly) I&amp;#8217;ll put you in contact with one of AW editor, and she&amp;#8217;ll try sorting out your problem. I&amp;#8217;m sure you&amp;#8217;ll get your PDF pretty fast&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Tue, 22 Jan 2008 23:28:02 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:f553badc-723b-4350-a910-f220bc5014df</guid>
      <link>http://www.h3rald.com/articles/efficient-ruby-code-shortcut-review#comment-205</link>
    </item>
    <item>
      <title>"Book Review: Writing Efficient Ruby Code" by Matthew Williams</title>
      <description>&lt;p&gt;Thanks for the review, I immediately headed over to the InformIT page and purchased the PDF but it still hasn&amp;#8217;t shown up in my account for download.  Any idea how long I should be waiting for my copy?  InformIT has the day off and I&amp;#8217;m getting anxious.  Especially compared to the Peepcode.com PDF&amp;#8217;s that get customized with your name in the footer on every page of the PDF and are delivered immediately.  But it&amp;#8217;s been over an hour now and I&amp;#8217;m starting to get anxious.  Hopefully InformIT will pull through the PDF will show up in my account!&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jan 2008 07:49:30 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:af352c9b-b28a-4806-8995-0dd06983ee6a</guid>
      <link>http://www.h3rald.com/articles/efficient-ruby-code-shortcut-review#comment-204</link>
    </item>
  </channel>
</rss>
