<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Avoid using metaprogramming (seriously!)</title>
	<atom:link href="http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/feed/" rel="self" type="application/rss+xml" />
	<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/</link>
	<description>Rails experiments by Matt Aimonetti</description>
	<lastBuildDate>Tue, 23 Feb 2010 22:05:18 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: Henrik N</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1670</link>
		<dc:creator>Henrik N</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1670</guid>
		<description>This example is definitely not the place for metaprogramming. Even if it would run equally fast, the metaprogramming version of the code is pretty hard to read.

One thing I think I would change about your code is to make the methods more readable by defining them in terms of one another:

  def week
    7.days * self
  end

and so on. Especially when it comes to month and year where someone is likely to check the implementation to see just what numbers are used.</description>
		<content:encoded><![CDATA[<p>This example is definitely not the place for metaprogramming. Even if it would run equally fast, the metaprogramming version of the code is pretty hard to read.</p>
<p>One thing I think I would change about your code is to make the methods more readable by defining them in terms of one another:</p>
<p>  def week<br />
    7.days * self<br />
  end</p>
<p>and so on. Especially when it comes to month and year where someone is likely to check the implementation to see just what numbers are used.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik N</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1671</link>
		<dc:creator>Henrik N</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1671</guid>
		<description>Oh, and the 364.25 (as opposed to 365.25) in the very first code snippet is a bug and/or typo.</description>
		<content:encoded><![CDATA[<p>Oh, and the 364.25 (as opposed to 365.25) in the very first code snippet is a bug and/or typo.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dohzya</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1672</link>
		<dc:creator>dohzya</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1672</guid>
		<description>try to put @amount = amount.is_a?(Array) ? amount[0].send(amount[1]) : amount@ out of the define_method. It must be compute one time only, not at each call of method...
Your can try to replace your @define_method@ by an @eval( &quot;&quot;, binding )@ too (more slow one time only)</description>
		<content:encoded><![CDATA[<p>try to put @amount = amount.is_a?(Array) ? amount[0].send(amount[1]) : amount@ out of the define_method. It must be compute one time only, not at each call of method&#8230;<br />
Your can try to replace your @define_method@ by an @eval( &quot;&quot;, binding )@ too (more slow one time only)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Aimonetti</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1673</link>
		<dc:creator>Matt Aimonetti</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1673</guid>
		<description>Matt Jones had a really good post, but my blog indicated that textile was enabled while only Markdown is enabled.

Here is Matt&#039;s post:

dohzya&#039;s comment is on target - the overhead here is in restoring the context (stack frame) that was captured by definemethod, and handling the amount.isa?(Array) case.

Just lifting the Array case above define_method fails pretty badly, as the methods don&#039;t get defined on Numeric until the end. (Also note: the meta-programming code relies on the TimeDsl code in this example - m_days calls TimeDsl&#039;s version of hours)

What&#039;s really needed (if you want to go meta) is something closer to ActiveRecord&#039;s method:

see [pastie](http://pastie.caboo.se/191414)

Please look at the above pastie to see how Matt got really good perf using metaprogramming. Really interesting stuff!</description>
		<content:encoded><![CDATA[<p>Matt Jones had a really good post, but my blog indicated that textile was enabled while only Markdown is enabled.</p>
<p>Here is Matt&#8217;s post:</p>
<p>dohzya&#8217;s comment is on target &#8211; the overhead here is in restoring the context (stack frame) that was captured by definemethod, and handling the amount.isa?(Array) case.</p>
<p>Just lifting the Array case above define_method fails pretty badly, as the methods don&#8217;t get defined on Numeric until the end. (Also note: the meta-programming code relies on the TimeDsl code in this example &#8211; m_days calls TimeDsl&#8217;s version of hours)</p>
<p>What&#8217;s really needed (if you want to go meta) is something closer to ActiveRecord&#8217;s method:</p>
<p>see [pastie](http://pastie.caboo.se/191414)</p>
<p>Please look at the above pastie to see how Matt got really good perf using metaprogramming. Really interesting stuff!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1674</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1674</guid>
		<description>I seem to remember a cartoon from the 80s - GI Joe, maybe? - that always had a moral at the end; like the one in this post, however, the moral presented then often seemed to be the wrong one given the experiences the Joe team had in the preceding half-hour.

Isn&#039;t the correct moral here that you should benchmark your code, metaprogrammed or otherwise?</description>
		<content:encoded><![CDATA[<p>I seem to remember a cartoon from the 80s &#8211; GI Joe, maybe? &#8211; that always had a moral at the end; like the one in this post, however, the moral presented then often seemed to be the wrong one given the experiences the Joe team had in the preceding half-hour.</p>
<p>Isn&#8217;t the correct moral here that you should benchmark your code, metaprogrammed or otherwise?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Aimonetti</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1675</link>
		<dc:creator>Matt Aimonetti</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1675</guid>
		<description>Good point! At least if you care about performances.

Now, if the benchmarks were close and that the slightly slower version was easier to read/understand, I would probably go for readability over performance.</description>
		<content:encoded><![CDATA[<p>Good point! At least if you care about performances.</p>
<p>Now, if the benchmarks were close and that the slightly slower version was easier to read/understand, I would probably go for readability over performance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: allen</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1676</link>
		<dc:creator>allen</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1676</guid>
		<description>Metaprogramming is used to add methods and features to classes at load time, not on each instantiation. Of course, doing this will require more work to dynamically create and install these methods on your Ruby classes.

While the metaprogramming can be slower on load time, it it not significant considering it is only loaded once (in the production environment, though after every change to those classes in development).

This should be the same in merb as rails. In a script/runner invocation or an application off the rails (CGI, command line, etc.) will have a slower load time as the class will be loaded each time your app starts up.

In the end, it does provide more readable/maintainable and DRY code, if written well. Are you optimizing prematurely?</description>
		<content:encoded><![CDATA[<p>Metaprogramming is used to add methods and features to classes at load time, not on each instantiation. Of course, doing this will require more work to dynamically create and install these methods on your Ruby classes.</p>
<p>While the metaprogramming can be slower on load time, it it not significant considering it is only loaded once (in the production environment, though after every change to those classes in development).</p>
<p>This should be the same in merb as rails. In a script/runner invocation or an application off the rails (CGI, command line, etc.) will have a slower load time as the class will be loaded each time your app starts up.</p>
<p>In the end, it does provide more readable/maintainable and DRY code, if written well. Are you optimizing prematurely?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DaveS</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1677</link>
		<dc:creator>DaveS</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1677</guid>
		<description>You might as well go back to assembly programming...</description>
		<content:encoded><![CDATA[<p>You might as well go back to assembly programming&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken Miller</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1678</link>
		<dc:creator>Ken Miller</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1678</guid>
		<description>It&#039;s not metaprogramming per se that makes this slow, it&#039;s the use of define_method.  methods so defined are always slower to call than methods defined the usual way.  If you just use class_eval and a string containing a standard method definition, you&#039;ll get results more in line with the written out version.

Of course, then it&#039;s even harder to read.  Metaprogramming should really only be used if it&#039;s the only way to solve a problem, as you say, but performance is not really a reason not to.</description>
		<content:encoded><![CDATA[<p>It&#8217;s not metaprogramming per se that makes this slow, it&#8217;s the use of define_method.  methods so defined are always slower to call than methods defined the usual way.  If you just use class_eval and a string containing a standard method definition, you&#8217;ll get results more in line with the written out version.</p>
<p>Of course, then it&#8217;s even harder to read.  Metaprogramming should really only be used if it&#8217;s the only way to solve a problem, as you say, but performance is not really a reason not to.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken Miller</title>
		<link>http://railsontherun.com/2008/05/04/avoid-using-metaprogramming/comment-page-1/#comment-1679</link>
		<dc:creator>Ken Miller</dc:creator>
		<pubDate>Sun, 04 May 2008 08:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://railsontherun.com/2008/05/04/avoid-using-metaprogramming#comment-1679</guid>
		<description>Amendment to above: in this case, it&#039;s also that you&#039;re doing the array work every time.  That could easily be moved into the generator code and out of the instance method, leaving the instance methods defined identically to the hand-coded methods.

But this really supports the overall point -- metaprogramming is harder to write and understand, and contains more opportunities for bugs and sneaky performance pitfalls.  But if you really need it, it IS possible to make it perform just as well as hand-coded ruby.</description>
		<content:encoded><![CDATA[<p>Amendment to above: in this case, it&#8217;s also that you&#8217;re doing the array work every time.  That could easily be moved into the generator code and out of the instance method, leaving the instance methods defined identically to the hand-coded methods.</p>
<p>But this really supports the overall point &#8212; metaprogramming is harder to write and understand, and contains more opportunities for bugs and sneaky performance pitfalls.  But if you really need it, it IS possible to make it perform just as well as hand-coded ruby.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
