<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Garbled &#187; tips</title>
	<atom:link href="http://garbled.benhamill.com/category/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://garbled.benhamill.com</link>
	<description>garbled = Blog.new(:author =&#62; &#039;Ben Hamill&#039;)</description>
	<lastBuildDate>Fri, 03 Feb 2012 20:50:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Parameter Object Pattern</title>
		<link>http://garbled.benhamill.com/2012/01/the-parameter-object-pattern/</link>
		<comments>http://garbled.benhamill.com/2012/01/the-parameter-object-pattern/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 14:00:41 +0000</pubDate>
		<dc:creator>benhamill</dc:creator>
				<category><![CDATA[otherinbox]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://garbled.benhamill.com/?p=45</guid>
		<description><![CDATA[I have been paying more attention to software design patterns recently. It all started with my noticing the Null Object pattern and the Ruby Rogues talking about Smalltalk Best Practice Patterns. My most recent discovery is of the Parameter Object pattern. Thanks, by the way, to @timtyrrell for telling me the name of it. As I&#8217;ve mentioned [...]]]></description>
			<content:encoded><![CDATA[<p>I have been paying more attention to software design patterns recently. It all started with my noticing the <a title="Falsiness and Null Objects" href="http://garbled.benhamill.com/2011/06/falsiness-and-null-objects/">Null Object pattern</a> and the <a title="Ruby Rogues podcast" href="http://rubyrogues.com/" target="_blank">Ruby Rogues</a> talking about <a title="Amazon Link" href="www.amazon.com/Smalltalk-Best-Practice-Patterns-Kent/dp/013476904X/" target="_blank">Smalltalk Best Practice Patterns</a>. My most recent discovery is of the <a title="Parameter Object pattern @ WikiWiki" href="http://c2.com/cgi/wiki?ParameterObject" target="_blank">Parameter Object pattern</a>. Thanks, by the way, to @<a title="Tim's Twitter page" href="http://twitter.com/timtyrrell" target="_blank">timtyrrell</a> for telling me the name of it.</p>
<p>As I&#8217;ve <a title="A Smarter has_many :through?" href="http://garbled.benhamill.com/2011/08/a-smarter-has_many-through/">mentioned before</a>, I&#8217;ve been spending some time <a title="OtherInbox" href="http://otherinbox.com/" target="_blank">at work</a> with a search engine. It works sort of like NewEgg&#8217;s search in that you might type a string in (&#8220;desktop ram&#8221;) and then narrow your search by clicking on links in a side bar (DDR3 or Corsair). The search engine in question powers a product called <a title="SubjectLin.es" href="http://subjectlin.es" target="_blank">SubjectLin.es</a> (it&#8217;s in beta; you can sign up for a free account to try it out, if you like) where you can search for words that appear in an email subject and then further limit by, say, who sent it (&#8220;sender&#8221;), what kind of email it is (like coupon or receipt, we call it a &#8220;tag&#8221;) or what business the sender was in (&#8220;market&#8221;).</p>
<p>To date, I&#8217;ve rewritten the search engine a few times and refactored it a few times. It and one other area of the code base are, by far, the most complex bits of the application, so it has gotten it&#8217;s fair share of attention. And it&#8217;s gotten more recently when an exception brought our attention to something: We don&#8217;t have one search engine&#8230; we have <em>four</em>. It&#8217;s just that they&#8217;re all in one class. Finding the email subjects that a user is interested in is not really related to finding the senders who sent them except that the user&#8217;s parameters are the same. So I thought what we&#8217;d do is factor out our current search object into 4 smaller ones (subject, sender, tag and market) that each centers around the object it&#8217;s pulling out of the DB. Besides code clarity, there are some other benefits we get from this modularity, but I don&#8217;t want to get into that in this post.</p>
<p>So now we&#8217;re faced with the fact that we&#8217;ll have 4 objects we want to create that all depend on the same parameters to build themselves. Add to that the fact that we do some fancy Google-style keyword parsing (you can type &#8220;some key words sender:Company&#8221; and we pull out the company name) and the fact that if the user is interested in limiting by sender, we want to display things differently than if they aren&#8217;t. Now, I thought, it looked as if we should have an object just about the user&#8217;s search parameters and that each of our search engines should take only that as an argument.</p>
<p>This way, our view (or presenter&#8230; whatever) can ask the search parameter object if the user limited based on sender or not. We can build the one object and then pass it to each of the four engines. We can put all the keyword parsing logic into the search parameter object, too. When I had it, it seemed like such a good idea that it had to have been thought of before. And, lo, it is called the Parameter Object pattern. Or, anyway, I think this is a case of that pattern.</p>
<p>What do you think? Is this Parameter Object? Is it something else? Does my example make any sense at all (I honestly have no idea, since I&#8217;m so deep in the system; I tried to balance high-level with need-to-know)? Note: as of this writing, none of this is implemented yet. This is all just design work with @<a title="Macors's Twitter page" href="http://twitter.com/marcosacosta" target="_blank">marcosacosta</a>, so far. If anything interesting crops up while we&#8217;re writing it, I&#8217;ll try to remember to update this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://garbled.benhamill.com/2012/01/the-parameter-object-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Falsiness and Null Objects</title>
		<link>http://garbled.benhamill.com/2011/06/falsiness-and-null-objects/</link>
		<comments>http://garbled.benhamill.com/2011/06/falsiness-and-null-objects/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 13:00:18 +0000</pubDate>
		<dc:creator>benhamill</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[naval gazing]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://garbled.benhamill.com/?p=39</guid>
		<description><![CDATA[Recently, I went to RailsConf. I saw a bunch of talks and met some cool people. This is not a RailsConf post-mortem post (if you&#8217;re interested, though, I&#8217;ve collected some notes from myself and some others here). This post is about what, in retrospect, was probably the best talk I went to (and I went [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I went to <a href="http://railsconf.com">RailsConf</a>. I saw a bunch of talks and met some cool people. This is not a RailsConf post-mortem post (if you&#8217;re interested, though, I&#8217;ve collected some notes from myself and some others <a href="https://github.com/benhamill/railsconf_2011">here</a>). This post is about what, in retrospect, was probably the best talk I went to (and I went to several really awesome talks). I&#8217;ve been mulling over it since I got back, basically, and that seems like a good result from a talk. That talk was <a href="http://avdi.org">Avdi Grimm</a>&#8216;s Confident Code (<a href="http://avdi.org/talks/confident-code-railsconf-2011/">slides</a>, <a href="https://github.com/benhamill/railsconf_2011/blob/master/confident_code.txt">my notes</a>).</p>
<p>One thing in particular sort of caught at the edge of my thought patterns: <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">The Null Object Pattern</a>. I don&#8217;t have a CS degree and so I&#8217;m missing a lot of the formal training about design patterns that many programmers have (and, probably, forget), so I&#8217;d never heard of it. When Avdi started talking about how ActiveRecord&#8217;s <code>try</code> method is a code smell, I was like, &#8220;Yes!&#8221; I would not say that I hate it, but I have seen several times some line of code that looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">try</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:posts</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">try</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:recent</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">try</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>I mean&#8230; bleh. But I didn&#8217;t know a way that looked any better to me, really. Anyway, you can look at <a href="https://github.com/benhamill/railsconf_2011/blob/master/confident_code.txt">the notes</a> and <a href="http://avdi.org/talks/confident-code-railsconf-2011/">slides</a> to learn about what Avdi says about the Null Object Pattern. I thought it was awesome and so when I went home, I decided to take it for a test drive.</p>
<h4>The Test Course</h4>
<p>So in a project at work, we have Users and they may or may not have one Subscription. Hopefully, you can picture this complex object graph. Subscriptions may or may not be &#8220;current&#8221; based on various business rules mostly to do with whether you paid us or not. So, naturally, we have a <code>Subscrption#current?</code> method. But we&#8217;re using the User as a sort of presenter for Subscriptions. So you don&#8217;t want to call <code>@user.subscription.current?</code>. That&#8217;s a code smell. So on User we had this method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> current?
  subscription.<span style="color:#9900CC;">try</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:current</span>?<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>There&#8217;s that rascal <code>try</code>. &#8220;This,&#8221; I thought, &#8220;is a perfect spot for that <code>Maybe</code> method from Avdi&#8217;s talk.&#8221; So I rewrote it thusly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> current?
  Maybe<span style="color:#006600; font-weight:bold;">&#40;</span>subscription<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">current</span>?
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>W00t, right? Wrong. The accompanying <code>NullObject</code> class looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> NullObject
  <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">nil</span>?
    <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>That method missing treatment, so handy in avoiding the chain of <code>try</code>s in my first example, is the gotcha. It means that if I have a User without a Subscription for whatever reason, calling <code>User#current?</code> returns an instance of <code>NullObject</code>, which will pass, say, the boolean clause of an <code>if</code> statement.</p>
<p>So, not sure as to whether I&#8217;d misunderstood something, was making some dumb mistake or what, I emailed Avdi. He said, basically, &#8220;Awesome question. I will answer it in a blog post.&#8221; And, lo, <a href="http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness/">he did</a>. Go read <a href="http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness/">that post</a> to see <a href="http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness/">what he said</a>. The comments also have some good ideas.</p>
<h4>Noodles</h4>
<p>If you read my comment, I said I was going to noodle on stuff and post again. I had more thoughts than it seemed like would fit in a blog comment. Hence this post. So my initial thought was disappointment. It turned out the Null Object Pattern wasn&#8217;t as powerful (in Ruby) as I&#8217;d hoped, since if you might have something (calling <code>Maybe</code>) the chances that you&#8217;ll have some conditional asking a boolean business-rule question about it is not low.</p>
<p>So I thought about how to get around that. You could, for instance, make a more complex <code>method_missing</code> definition that grepped the message name for <code>/\?$/</code> and returned false. That&#8217;s fail, though. It falls down the moment you have something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> Maybe<span style="color:#006600; font-weight:bold;">&#40;</span>@posts<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">empty</span>?
  <span style="color:#008000; font-style:italic;"># Intuitively, you'd expect NullObject#empty? to have put you in here.</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>But then I realized that Avdi was making a higher-level point: since it is not possible to make your own objects look falsey in Ruby, you have to have another solution. Trying to define various question-mark methods on <code>NullObject</code> is trying to untie the knot, but I should be looking for a way to cut it. So it got me thinking: Why the hell to I have Users without Subscriptions, anyway? Shouldn&#8217;t <code>User#current?</code> express that business logic clearly, rather than just express the logic that enforces it? Yes. Yes, it should.</p>
<p>We have some Users who are also admins, who have special rights. It&#8217;s also conceivable that we could give away a free account for whatever reason. So, really, we want something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> current?
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">free_account</span>? <span style="color:#006600; font-weight:bold;">||</span> subscription.<span style="color:#9900CC;">current</span>?
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>But, this thought it incomplete. It expresses the business logic cleanly: The User is current if they&#8217;re flagged as free or if their Subscription is up to date. However, if the weird case of a User who is neither free nor has an associated Subscription crops up, we still have to hunt down the &#8220;Undefined method &#8216;current?&#8217; for nil&#8221; error. It sort of has be reaching for <code>Maybe</code> again.</p>
<p>Or maybe (heh, you see what I did there) I want to steal another trick from Avdi&#8217;s presentation and have <code>User#subscription</code> return <code>:no_subscription_defined_for_user</code> so that the error message makes some more sense. I don&#8217;t like redefining <code>ActiveRecord</code>&#8216;s default accessor methods, though, to transparently return the symbol if the real object is missing.</p>
<p>If you&#8217;ve got any thoughts, I&#8217;d love to hear them.</p>
]]></content:encoded>
			<wfw:commentRss>http://garbled.benhamill.com/2011/06/falsiness-and-null-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How Do You Make a Gem?</title>
		<link>http://garbled.benhamill.com/2010/06/how-do-you-make-a-gem/</link>
		<comments>http://garbled.benhamill.com/2010/06/how-do-you-make-a-gem/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 17:00:41 +0000</pubDate>
		<dc:creator>benhamill</dc:creator>
				<category><![CDATA[heygovote]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://garbled.benhamill.com/?p=35</guid>
		<description><![CDATA[This is not intended to be a how-to. I sort of talked a bit about that before. I&#8217;m writing this post because, as I breathlessly blogged before (alliteration!), I released my first gem. I then immediately turned around to start using it. And it was terrible. Yay for version 0.1! So I&#8217;m going to try [...]]]></description>
			<content:encoded><![CDATA[<p>This is not intended to be a how-to. I sort of talked a bit about <em>that</em> <a href="http://garbled.benhamill.com/2010/05/my-first-ruby-gem/">before</a>. I&#8217;m writing this post because, as I breathlessly blogged before (alliteration!), I released my <a href="http://rubygems.org/gems/twitter_alert">first gem</a>. I then immediately turned around to start using it. And it was terrible. Yay for version 0.1! So I&#8217;m going to try something else.</p>
<p>The thing that sucked so bad was the API, basically. I didn&#8217;t <em>really</em> know how I wanted to use it or how best to fit it into a Rails app. I made a reasonable guess and got to work. It wasn&#8217;t a terrible first try, but it <em>was</em> a terrible API. However, I&#8217;m not sure what better looks like yet. And I think that I sort of got ahead of myself; I put the cart before the horse.</p>
<p>It&#8217;s a little like making the transition from drawing on paper to creating art with the GIMP. When you&#8217;re on paper, you draw the stuff that&#8217;s in front first and you only draw as much of the stuff that&#8217;s behind other stuff as can be seen. So you learn to think a certain way about how you build up your picture. In a program that has layers, you can draw in any old order and draw something entirely even if it&#8217;s obscured by something else in the long run.</p>
<p>I was thinking that the obvious path would be to develop my little gem of functionality and then use it in the larger Hey Go Vote application. Now I&#8217;m starting to think that&#8217;s backwards. I&#8217;m going to just work on Hey Go Vote and trust that doing so will give me insight into what parts of the announcement machinery can be made portable. Then I&#8217;ll extract those bits into a gem and refactor Hey Go Vote to use the new gem. I&#8217;m sure I&#8217;ll let you know how that goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://garbled.benhamill.com/2010/06/how-do-you-make-a-gem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sudo on Windows XP&#8230; Sort of</title>
		<link>http://garbled.benhamill.com/2008/12/sudo-on-windows-xp-sort-of/</link>
		<comments>http://garbled.benhamill.com/2008/12/sudo-on-windows-xp-sort-of/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 13:36:51 +0000</pubDate>
		<dc:creator>benhamill</dc:creator>
				<category><![CDATA[os]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://garbled.benhamill.com/?p=5</guid>
		<description><![CDATA[I&#8217;m mostly posting this to document it to myself, since I always forget, but it might be helpful to any readers, as well. You generally don&#8217;t want to be logging in to your computer as a user with admin privileges and a sane OS (like Mac OS or Linux), makes it a fairly painless experience. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m mostly posting this to document it to myself, since I always forget, but it might be helpful to any readers, as well. You generally don&#8217;t want to be logging in to your computer as a user with admin privileges and a sane OS (like Mac OS or Linux), makes it a fairly painless experience. Windows, on the other hand, can be a real bear on the point. Specifically, Windows Explorer doesn&#8217;t like to do the whole &#8220;Run as&#8230;&#8221; thing. I&#8217;ve discovered a wonderful little run line that will solve this problem.</p>
<p>In the run line (Windows key, then R or click Start menu then Run&#8230;) put in <code>runas /u:administrator "explorer.exe /separate"</code>. You&#8217;ll want to replace <code>administrator</code> with an appropriate user name if that&#8217;s not a valid one. A DOS prompt will appear asking for the password and away you go. This tip thanks to <a href="http://stackoverflow.com/questions/13805/opening-explorer-shell-with-admin-priveleges-on-xp-with-ie7-installed">Stack Overflow</a>.</p>
<p>I also found something else handy <a href="http://pcwizkid.blogspot.com/2008/03/create-shorcuts-of-hidden-commands-in.html">here</a>: You can input run-line commands into the &#8220;location&#8221; prompt when creating a shortcut from scratch in Windows. So if you don&#8217;t want to type out all that /u:administrator stuff all day (or, well, probably not that frequently and you&#8217;d forget it), then you can right-click &gt; New &gt; shortcut and paste your command into the location. Call it whatever you want and then you&#8217;ve got a shortcut to an admin Windows Explorer right on your desktop.</p>
<p>I find this so much easier to deal with than any other solution when I need to muck with file permissions or any number of things in Windows. Helpful? Didn&#8217;t work for you? Thoughts on the site design? Let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://garbled.benhamill.com/2008/12/sudo-on-windows-xp-sort-of/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

