Just A Summary : Tag refactoring, everything about refactoring http://www.bofh.org.uk/articles/tag/refactoring.rss en-us 40 Piers Cawley Practices Punditry Just a thought <p>There&#8217;s a refactoring principle that states that, when you start doing the same thing for the third time, you should refactor to remove the duplication.</p> <p>I&#8217;m starting to wonder if there&#8217;s a Smalltalk principle which states that, when you start doing the same thing the second time, you should search the image for the obviously named method (or use the method finder to find some candidate by feeding it some inputs and an expected answer), because the odds are good that it&#8217;s only the second time for you &#8211; it might be the millionth time for the image you&#8217;re working in.</p> Thu, 25 Sep 2008 23:46:00 -0500 urn:uuid:3acc824f-f1f6-4328-bf97-ab310523115a pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2008/09/25/just-a-thought#comments The Practice of Programming practiceofprogramming smalltalk refactoring http://www.bofh.org.uk/articles/2008/09/25/just-a-thought Cheat all you want, but don't get caught <p>As far as I can tell, one of the Smalltalk optimizers&#8217; mottoes is &#8220;Cheat all you want, but don&#8217;t get caught&#8221;.</p> <p>Well, this morning, I caught Squeak with its hand in the till.</p> <p>One way I attempt to bootstrap myself towards understanding of code is to try and make it better, if that makes sense. So, I&#8217;d run SLint over the OmniBrowser package and was trying to shorten a method. One thing that struck me as rather ugly was a piece of code that ran like this:</p> <div class="typocode"><pre><code class="typocode_default ">|selection| ... selection := OBChoiceRequest prompt: nil labels: usersNames values: users. selection ifNotNil: [selection browse].</code></pre></div> <p>So I thought I&#8217;d tweak <code>ifNotNil:</code> so that, if its receiver isn&#8217;t nil, it will pass itself as an argument into the block, which will let me rewrite that ugly code with:</p> <div class="typocode"><pre><code class="typocode_default ">(OBChoiceRequest prompt: nil labels: usersNames values: users) ifNotNil: [:selection| selection browse].</code></pre></div> <p>So, I went to have a look at the implementation of <code>ifNotNil:</code> and found that it was already doing exactly what I was after.</p> <p>At this point, I had a slight premonition of danger, so I brought up a workspace and tried to print the result of running <code>10 ifNotNil: [:i| i + 10]</code> and got a compiler error, complaining that <code>ifNotNil:</code> takes a 0 argument block. Which isn&#8217;t what the implementation of <code>ifNotNil:</code> thinks.</p> <p>I&#8217;d caught Squeak&#8217;s optimizer cheating.</p> <p>What appears to happen is that Squeak catches conditional code and rewrites it before passing it to the compiler. The rewritten code uses VM level primitives where possible. I needed to fix it so that it would only rewrite any calls to <code>ifNotNil:</code> with a zero argument block.</p> <p>It took a while, but my local image now optimizes <code>ifNotNil:</code> correctly (the <code>ifNotNil:ifNil:</code> and <code>ifNil:ifNotNil:</code> forms are another matter though, but I shall live. Now, if I can just work out where to submit the changeset to&#8230;</p> <p>I have mixed feelings about this. On the one hand, I&#8217;ve just changed something in the workings of Squeak, on the other, it&#8217;s not been quite as easy as I&#8217;d expected it to be. It seems that, if you go poking around in methods that are defined on ProtoObject, don&#8217;t be surprised if changing things doesn&#8217;t quite do what you expect.</p> <p>Maybe I should have just written a <code>ifNotNilDo:</code> taking a single argument block, but that just felt ugly&#8230; Ho hum.</p> Wed, 20 Jun 2007 17:16:00 -0500 urn:uuid:9a1f0bdb-7908-4f39-9ec8-4e8091c5167d pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2007/06/20/cheat-all-you-want-but-dont-get-caught#comments The Practice of Programming Squeak smalltalk refactoring http://www.bofh.org.uk/articles/2007/06/20/cheat-all-you-want-but-dont-get-caught