<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>in two places at once</title>
	
	<link>http://intwoplacesatonce.com</link>
	<description>instead of a rewrite</description>
	<lastBuildDate>Thu, 04 Apr 2013 18:34:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/InTwoPlacesAtOnce" /><feedburner:info uri="intwoplacesatonce" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Pushing media to Android’s gallery using adb</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/5zHZeT_38ts/</link>
		<comments>http://intwoplacesatonce.com/2013/04/pushing-media-to-androids-gallery-using-adb/#comments</comments>
		<pubDate>Thu, 04 Apr 2013 04:33:55 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[adb]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[ContentProvider]]></category>
		<category><![CDATA[gallery]]></category>
		<category><![CDATA[intent]]></category>
		<category><![CDATA[media scanner]]></category>
		<category><![CDATA[MediaScanner]]></category>
		<category><![CDATA[MEDIA_MOUNTED]]></category>
		<category><![CDATA[MEDIA_SCANNER_SCAN_FILE]]></category>
		<category><![CDATA[scan]]></category>
		<category><![CDATA[media]]></category>
		<category><![CDATA[scanner]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=155</guid>
		<description><![CDATA[On my current project, we need to &#8220;pre-load&#8221; many tablets with a set of videos. We want to script the loading of the videos, so we started with adb. The videos should appear in Android&#8217;s gallery app so they can be viewed like any other video. Finally, we have some folder structure for the videos [...]]]></description>
				<content:encoded><![CDATA[<p>On my current project, we need to &#8220;pre-load&#8221; many tablets with a set of videos. We want to script the loading of the videos, so we started with adb. The videos should appear in Android&#8217;s gallery app so they can be viewed like any other video. Finally, we have some folder structure for the videos that should also appear in the gallery.</p>
<p>Getting this working was a couple hours&#8217; challenge. Copying the files on to the tablet was easy. We did that in bulk using adb&#8217;s push command: <code>adb push Movies/ /mnt/sdcard/Movies/remote_video.mp4</code></p>
<p>This is not enough to make the video appear in the gallery. Rather than scanning or watching the filesystem, Android&#8217;s gallery reads a ContentProvider that lists the media on the device. The ContentProvider is populated by a MediaScannerService that looks for media at particular times and saves information about any discovered music, videos or images.</p>
<p>We needed a way to trigger the MediaScanner. Naively, we tried restarting the tablet, but that didn&#8217;t get the videos in to the gallery. Next, we asked google. We found several general descriptions of the process, but not in enough detail to easily duplicate it. Here is what we discovered after experimentation.</p>
<p>The basic form of the command is:<br />
<code>adb shell "am broadcast -a <ACTION> -d <DATA_URI>"</code><br />
or you can run it within an dab shell on a device:<br />
<code>adb shell<br />
am broadcast -a <ACTION> -d <DATA_URI></code></p>
<p>What should the &lt;ACTION&gt; and &lt;DATA_URI&gt; values be?</p>
<p>Action needs to be a recognized constant value from the Android documentation. I kept tripping myself up by using the constant name, rather than the value. Values are in the documentation for the constants:<br />
<img src="http://intwoplacesatonce.com/wp-content/uploads/2012/12/Screen-Shot-2012-12-15-at-9.00.28-PM.png" alt="Screen Shot 2012 12 15 at 9 00 28 PM" title="Screen Shot 2012-12-15 at 9.00.28 PM.png" border="0" width="403" height="148" /></p>
<p>The two that we found useful were android.intent.action.MEDIA_MOUNTED and android.intent.action.MEDIA_SCANNER_SCAN_FILE. As described in their respective documentation, MEDIA_MOUNTED takes a path to scan as its data_uri and MEDIA_SCANNER_SCAN_FILE takes a path to a single file. For our use case, we ran the command:<br />
<code>adb -d shell "am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///mnt/sdcard/Movies/"</code></p>
<p>We had originally tried to use /mnt/sdcard/ as the full path, because it didn&#8217;t seem that there would be any harm in scanning the whole card. However, because different system apps watch paths on the sdcard, trying to say the whole card was mounted causes a permissions error.</p>
<p>Because of that error, we had to be specify /Movies/, which did not trigger the error. If we do need to load music or images, we will be able to pass those specific paths with the same intent.</p>
<p>(Note: I&#8217;ve been unable to reproduce the permissions error more recently, so there is another factor involved. One difference could be that the tablets reporting the error were running 4.0.3, but we have since moved to tablets running 4.1.1.)</p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/5zHZeT_38ts" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2013/04/pushing-media-to-androids-gallery-using-adb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2013/04/pushing-media-to-androids-gallery-using-adb/</feedburner:origLink></item>
		<item>
		<title>the other thing about “using” blocks</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/1ZY3IBB98cY/</link>
		<comments>http://intwoplacesatonce.com/2011/09/the-other-thing-about-using-blocks/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 03:39:57 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[blocks]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[dispose]]></category>
		<category><![CDATA[lean on the compiler]]></category>
		<category><![CDATA[object instantiation]]></category>
		<category><![CDATA[resource acquisition]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[using]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=145</guid>
		<description><![CDATA[C#&#8217;s using statement is well recognized for one thing: calling dispose on objects so that you don&#8217;t have to. That bit is wonderful: string contents; using &#40;var f = File.OpenText&#40;&#34;/path/to/file&#34;&#41; &#123; contents = f.ReadToEnd&#40;&#41;; &#125; This is much simpler than the fully spelled out alternative: string contents; var f = File.OpenText&#40;&#34;/path/to/file&#34;&#41;; try &#123; contents = [...]]]></description>
				<content:encoded><![CDATA[<p>C#&#8217;s <code>using</code> statement is well recognized for one thing: calling dispose on objects so that you don&#8217;t have to. That bit is wonderful:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> contents<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">var</span> f <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;/path/to/file&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	contents <span style="color: #008000;">=</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This is much simpler than the fully spelled out alternative:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> contents<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">var</span> f <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;/path/to/file&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
	contents <span style="color: #008000;">=</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">finally</span> <span style="color: #008000;">&#123;</span>
	f<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And even this longer form actually misses one of the more interesting aspects of the using statement…</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> contents<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">var</span> f <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;/path/to/file&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
	contents <span style="color: #008000;">=</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">finally</span> <span style="color: #008000;">&#123;</span>
	f<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// oops! ObjectDisposedException at runtime</span>
f <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// you could set it to null</span>
f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// but now you have a NullReferenceException, even more mysterious</span></pre></td></tr></table></div>

<p>The using statement on the other hand, creates a scope, so its variable can&#8217;t be referenced at all after it is disposed:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> contents<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">var</span> f <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;/path/to/file&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	contents <span style="color: #008000;">=</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// unknown identifier error at compile-time</span></pre></td></tr></table></div>

<p>An entirely equivalent bit of code can be written, using an anonymous scope, but it starts to look quite baroque:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> contents<span style="color: #008000;">;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">var</span> f <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;/path/to/file&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
		contents <span style="color: #008000;">=</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">finally</span> <span style="color: #008000;">&#123;</span>
		f<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
f<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Unknown identifier at compile-time again, but 2x the lines and 2x the scopes!</span></pre></td></tr></table></div>

<p>using provides the try-finally-dispose structure, and also provides a scope. The scope means a whole class of errors where resources are accessed after being released is transformed from run-time to compile-time errors. Dealing with errors at compile-time is quicker, and with the right tools to highlight problems, the compile problems can be seen directly in the code as it is being edited.</p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/1ZY3IBB98cY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2011/09/the-other-thing-about-using-blocks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2011/09/the-other-thing-about-using-blocks/</feedburner:origLink></item>
		<item>
		<title>the viewport meta tag, and iPhone</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/PX1P8hEXJ4E/</link>
		<comments>http://intwoplacesatonce.com/2011/06/the-viewport-meta-tag-and-iphone/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 04:37:58 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[meta]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[viewport]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=126</guid>
		<description><![CDATA[Mobile phones have a couple of options when rendering existing web pages: they can render a page at the native resolution of the screen, or they can render it on a larger virtual screen and then &#8220;zoom out&#8221; so that the whole page fits on the screen. The virtual screen is called the &#8220;viewport.&#8221; Ideally [...]]]></description>
				<content:encoded><![CDATA[<p>Mobile phones have a couple of options when rendering existing web pages: they can render a page at the native resolution of the screen, or they can render it on a larger virtual screen and then &#8220;zoom out&#8221; so that the whole page fits on the screen. The virtual screen is called the &#8220;viewport.&#8221; </p>
<p>Ideally screen resolution shouldn&#8217;t matter for the web, but many existing pages on the web won&#8217;t render well at a smartphone&#8217;s native resolution. After all, even in 1993 when the web started, most screens were at least 640 pixels wide. The iPhone by contrast is 320 pixels by 480 pixels[1]. Over the years, web designers have assumed that there will be at least that much width to lay out their pages across, and don&#8217;t consider how layouts break when the screen is narrower. In order to render all these existing web pages well, mobile browsers choose the path of rendering to a larger viewport and scaling the content to fit the page on screen. Safari on iOS uses 980 pixels as the default viewport width.</p>
<p>When targeting a web page at small screen devices, it could be nice to have the screen resolution match the viewport size. The <a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html">viewport meta tag</a> was introduced to allow a designer to request exactly that, that the phone should render the page at its native resolution.</p>
<p>As explained in the <a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html%23//apple_ref/doc/uid/TP40008193-SW1">Safari html reference</a>, there are six things that can be specified on the meta tag:</p>
<ul>
<li>width
<li>height
<li>initial-scale
<li>minimum-scale
<li>maximum-scale
<li>user-scalable
</ul>
<p>We found out early on in the <a href="http://m.democracynow.org">Democracy Now! mobile site</a> project that some of the attributes in combination have some surprising effects when combined with rotating the device. Ultimately, initial-scale turned out to be the culprit.</p>
<p>My initial guess based on the documentation was to set both width and initial-scale, but this causes the site to be &#8220;too wide&#8221; when the phone is rotated from portrait to landscape:</p>
<p><img src="http://intwoplacesatonce.com/wp-content/uploads/2011/06/width_and_scale.png" alt="results of specifying both width and scale" title="width_and_scale.png" border="0" width="600" height="302" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;meta name='viewport' 
      content='width=device-width,initial-scale=1.0'&gt;</pre></td></tr></table></div>

<p>Second thought was to set height as well as width, and initial-scale. This makes the portrait to landscape rotation work as expected, but causes a similar &#8220;too wide problem&#8221; when rotating from landscape to portrait:</p>
<p><img src="http://intwoplacesatonce.com/wp-content/uploads/2011/06/width_and_height.png" alt="results of specifying both width and height" title="width_and_height.png" border="0" width="600" height="302" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;meta name='viewport' 
      content='width=device-width,height=device-height,initial-scale=1.0'&gt;</pre></td></tr></table></div>

<p>The solution turned out to be not setting initial-scale at all. Device-width is enough to set the viewport width to the real width of the device, and a scale is not needed &#8212; 1.0 is assumed it would seem.</p>
<p><img src="http://intwoplacesatonce.com/wp-content/uploads/2011/06/device_width_only.png" alt="specifying only device width gives the expected result" title="device_width_only.png" border="0" width="600" height="302" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;meta name='viewport' 
      content='width=device-width'&gt;</pre></td></tr></table></div>

<p>When I come across a bug like this, I like trying to come up with a mental model of what is going wrong in the code to create the undesired behaviour. This is useful when debugging my own code and useful when trying to work around apparent bugs in other people&#8217;s code.</p>
<p>I haven&#8217;t built a good mental model of what is going wrong here. My best guess is that the width and the scale are being decided at different points during a rotation, and are getting out of sync. In the first example it could be that the width and scale are decided before rotation: 320 and 1.0. Then during rotation scaling and width are both changed, but independently. For scale, it is decided that 320 viewport pixels are now being rendered across 480 physical pixels, so scale factor can be 3/2 (three physical pixels for every 2 virtual pixels). Independently, it is decided that the page can now be rendered across 480 pixels. We end up with a viewport that is 480 virtual pixels wide, scaled by 3/2 so that only 320 of those pixels are in the visible area.</p>
<p>This model suggests another possible fix. If the model is correct, constraining the scale-factor to 1.0 would fix the problem. Only the number of available pixels will change, rather than both scale and width at the same time. The downside of this solution is that the user would no longer be able to zoom in. Trying it out, it works:</p>
<p><img src="http://intwoplacesatonce.com/wp-content/uploads/2011/06/with_max_scale.png" alt="results of specifying max scale" title="with_max_scale.png" border="0" width="600" height="302" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;meta name='viewport' 
      content='maximum-scale=1.0,width=device-width,initial-scale=1.0'&gt;</pre></td></tr></table></div>

<p>The movement after a rotation as the page recombobulates itself seems revealing. It is different for each of the above combinations. I can&#8217;t show transitions here; you&#8217;ll have to try them out yourself to see.</p>
<p>In the end, we went with the solution of only specifying &#8216;width=device-width&#8217;. This worked well on the iPhone and on other browsers. Other browsers have different freaky scale things that happen after a rotation, including one bug that is still affecting us on Android 2.1.</p>
<p>Thanks to Dani Schufeldt for testing and raising this defect until it was fixed properly, and Ted Nielsen for working the layouts around it.</p>
<p>[1] I&#8217;m ignoring the double-density iPhone 4 screen, because I can&#8217;t remember the separate terminology to keep it all straight. I might come back later and edit the blog post.</p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/PX1P8hEXJ4E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2011/06/the-viewport-meta-tag-and-iphone/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2011/06/the-viewport-meta-tag-and-iphone/</feedburner:origLink></item>
		<item>
		<title>restoring recovered files identity</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/xZ_6DV3x-SU/</link>
		<comments>http://intwoplacesatonce.com/2010/10/restoring-recovered-files-identity/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 05:08:43 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[fat32]]></category>
		<category><![CDATA[recovery]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=118</guid>
		<description><![CDATA[I have a couple of one terabyte external hard drives. When I first got them I formatted them with FAT32 so that I could read and write them from both Windows and OS X. FAT32 is not a journaling filesystem, which means if a write operation is interrupted the drive can end up in an [...]]]></description>
				<content:encoded><![CDATA[<p>I have a couple of one terabyte external hard drives. When I first got them I formatted them with FAT32 so that I could read and write them from both Windows and OS X.</p>
<p>FAT32 is not a <a href="http://en.wikipedia.org/wiki/Journaling_file_system">journaling filesystem</a>, which means if a write operation is interrupted the drive can end up in an inconsistent state. Within the first month of using the drive, I corrupted the filesystem. I forget exactly what happened: did I knock the power cord out? forget to eject the drive?</p>
<p>Luckily when this happens to a FAT32 disk the files can be recovered. Unfortunately, the filenames are lost. You get a folder called <code>FOUND.000</code> with files named <code>FILE0000.CHK</code> up to, in my case, <code>FILE0820.CHK</code>. The extension on filenames is important on Windows and on OS X as well because Finder relies on it.</p>
<p>Unix-like systems come with a utility, called <code>file</code>, that can determine file types by examining the contents. When I first read about file it was described as checking the <a href="http://en.wikipedia.org/wiki/File_format#Magic_number">first 2-bytes</a>, called the magic number or magic cookie. More modern versions must check more of the file, because they offer more information than can be contained in 2-bytes.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">➜ <span style="color: #7a0874; font-weight: bold;">&#123;</span>ninja<span style="color: #7a0874; font-weight: bold;">&#125;</span> FOUND.000 $ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">*</span>.CHK
FILE0004.CHK: RIFF <span style="color: #7a0874; font-weight: bold;">&#40;</span>little-endian<span style="color: #7a0874; font-weight: bold;">&#41;</span> data, AVI, <span style="color: #000000;">628</span> x <span style="color: #000000;">254</span>, <span style="color: #000000;">25.00</span> fps, video: DivX <span style="color: #000000;">5</span>, audio: MPEG-<span style="color: #000000;">1</span> Layer <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>stereo, <span style="color: #000000;">48000</span> Hz<span style="color: #7a0874; font-weight: bold;">&#41;</span>
FILE0007.CHK: RIFF <span style="color: #7a0874; font-weight: bold;">&#40;</span>little-endian<span style="color: #7a0874; font-weight: bold;">&#41;</span> data, AVI, <span style="color: #000000;">580</span> x <span style="color: #000000;">306</span>, <span style="color: #000000;">23.98</span> fps, video: DivX <span style="color: #000000;">5</span>, audio: MPEG-<span style="color: #000000;">1</span> Layer <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>stereo, <span style="color: #000000;">48000</span> Hz<span style="color: #7a0874; font-weight: bold;">&#41;</span>
FILE0009.CHK: RIFF <span style="color: #7a0874; font-weight: bold;">&#40;</span>little-endian<span style="color: #7a0874; font-weight: bold;">&#41;</span> data, AVI, <span style="color: #000000;">576</span> x <span style="color: #000000;">320</span>, <span style="color: #000000;">23.98</span> fps, video: XviD, audio: MPEG-<span style="color: #000000;">1</span> Layer <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>stereo, <span style="color: #000000;">48000</span> Hz<span style="color: #7a0874; font-weight: bold;">&#41;</span>
FILE0013.CHK: RIFF <span style="color: #7a0874; font-weight: bold;">&#40;</span>little-endian<span style="color: #7a0874; font-weight: bold;">&#41;</span> data, AVI, <span style="color: #000000;">612</span> x <span style="color: #000000;">250</span>, <span style="color: #000000;">23.98</span> fps, video: DivX <span style="color: #000000;">5</span>, audio: MPEG-<span style="color: #000000;">1</span> Layer <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>stereo, <span style="color: #000000;">48000</span> Hz<span style="color: #7a0874; font-weight: bold;">&#41;</span>
FILE0016.CHK: RIFF <span style="color: #7a0874; font-weight: bold;">&#40;</span>little-endian<span style="color: #7a0874; font-weight: bold;">&#41;</span> data, AVI, <span style="color: #000000;">592</span> x <span style="color: #000000;">320</span>, <span style="color: #000000;">25.00</span> fps, video: XviD, audio: MPEG-<span style="color: #000000;">1</span> Layer <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>stereo, <span style="color: #000000;">48000</span> Hz<span style="color: #7a0874; font-weight: bold;">&#41;</span>
FILE0019.CHK: RIFF <span style="color: #7a0874; font-weight: bold;">&#40;</span>little-endian<span style="color: #7a0874; font-weight: bold;">&#41;</span> data, AVI, <span style="color: #000000;">608</span> x <span style="color: #000000;">288</span>, <span style="color: #000000;">25.00</span> fps, video: XviD, audio: MPEG-<span style="color: #000000;">1</span> Layer <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>stereo, <span style="color: #000000;">48000</span> Hz<span style="color: #7a0874; font-weight: bold;">&#41;</span>
FILE0022.CHK: RIFF <span style="color: #7a0874; font-weight: bold;">&#40;</span>little-endian<span style="color: #7a0874; font-weight: bold;">&#41;</span> data, AVI, <span style="color: #000000;">640</span> x <span style="color: #000000;">272</span>, <span style="color: #000000;">23.98</span> fps, video: XviD, audio: MPEG-<span style="color: #000000;">1</span> Layer <span style="color: #000000;">3</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>stereo, <span style="color: #000000;">44100</span> Hz<span style="color: #7a0874; font-weight: bold;">&#41;</span>
FILE0026.CHK: PNG image, <span style="color: #000000;">640</span> x <span style="color: #000000;">272</span>, <span style="color: #000000;">8</span>-bit<span style="color: #000000; font-weight: bold;">/</span>color RGB, non-interlaced
FILE0027.CHK: PC bitmap, Windows <span style="color: #000000;">3</span>.x format, <span style="color: #000000;">640</span> x <span style="color: #000000;">272</span> x <span style="color: #000000;">32</span>
FILE0028.CHK: PNG image, <span style="color: #000000;">640</span> x <span style="color: #000000;">272</span>, <span style="color: #000000;">8</span>-bit<span style="color: #000000; font-weight: bold;">/</span>color RGB, non-interlaced</pre></td></tr></table></div>

<p>I wanted to at least check what the contents of my FOUND files were before deleting them. I needed to get the appropriate extensions back on the filenames so that finder and other tools would work with them properly. I thought I should be able to do it completely in the shell, and best of all, it&#8217;s already a REPL!</p>
<p>First step was to sort them by type.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">➜ <span style="color: #7a0874; font-weight: bold;">&#123;</span>ninja<span style="color: #7a0874; font-weight: bold;">&#125;</span> FOUND.000 $ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">*</span>.CHK <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> PNG
FILE0026.CHK: PNG image, <span style="color: #000000;">640</span> x <span style="color: #000000;">272</span>, <span style="color: #000000;">8</span>-bit<span style="color: #000000; font-weight: bold;">/</span>color RGB, non-interlaced
FILE0028.CHK: PNG image, <span style="color: #000000;">640</span> x <span style="color: #000000;">272</span>, <span style="color: #000000;">8</span>-bit<span style="color: #000000; font-weight: bold;">/</span>color RGB, non-interlaced</pre></td></tr></table></div>

<p>Next I needed the filename by cutting the first 8 characters. I did not know about <code>cut</code> before but I&#8217;ve certainly needed to pull a section out of a line before. I expect I&#8217;ll be using it again.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">➜ <span style="color: #7a0874; font-weight: bold;">&#123;</span>ninja<span style="color: #7a0874; font-weight: bold;">&#125;</span> FOUND.000 $ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">*</span>.CHK <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> PNG <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">1</span>-<span style="color: #000000;">8</span>
FILE0026
FILE0028</pre></td></tr></table></div>

<p>I needed to turn these lines in to separate <code>mv</code> commands and <code>xargs</code> does exactly that. I had not used <code>xargs</code> beyond entirely simple commands before but the <code>man</code> page was enough to get me started. Since I was about to move files without a safety net, I wanted to do a quick test first.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">➜ <span style="color: #7a0874; font-weight: bold;">&#123;</span>ninja<span style="color: #7a0874; font-weight: bold;">&#125;</span> FOUND.000 $ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">*</span>.CHK <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> PNG <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">1</span>-<span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-I</span> filename <span style="color: #7a0874; font-weight: bold;">echo</span> filename.CHK filename.png
FILE0026.CHK FILE0026.png
FILE0028.CHK FILE0028.png</pre></td></tr></table></div>

<p>Looked good, so it was time for the real command.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">➜ <span style="color: #7a0874; font-weight: bold;">&#123;</span>ninja<span style="color: #7a0874; font-weight: bold;">&#125;</span> FOUND.000 $ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">*</span>.CHK <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> PNG <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">1</span>-<span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-I</span> filename <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #660033;">-v</span> filename.CHK filename.png
FILE0026.CHK -<span style="color: #000000; font-weight: bold;">&gt;</span> FILE0026.png
FILE0028.CHK -<span style="color: #000000; font-weight: bold;">&gt;</span> FILE0028.png</pre></td></tr></table></div>

<p>With the extensions corrected Finder is once again previewing and opening the files correctly. It&#8217;s working well for videos and images. I think multi-volume rars are going to be more of a challenge.</p>
<p>This was all prompted by <a href="http://manytricks.com/usher/">Usher</a> being released today. I&#8217;ve wanted an application to manage videos on OS X for a while.</p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/xZ_6DV3x-SU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2010/10/restoring-recovered-files-identity/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2010/10/restoring-recovered-files-identity/</feedburner:origLink></item>
		<item>
		<title>objective-c test doubles on the cheap with brutal cast</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/qlbS7ulVU3U/</link>
		<comments>http://intwoplacesatonce.com/2010/09/objective-c-test-doubles-on-the-cheap-with-brutal-cast/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 06:56:39 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[brutal cast]]></category>
		<category><![CDATA[casting]]></category>
		<category><![CDATA[dispatch]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[interactionist]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[test doubles]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit tests]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=113</guid>
		<description><![CDATA[Objective-C has the power of Ruby, with duck-typing and dynamic dispatch in the object layer. At the same time it has the power of C, with direct access to memory layouts and static-weak typing below the object layer. Sometimes, the two powers can be combined for some unexpected results. On my current project we are [...]]]></description>
				<content:encoded><![CDATA[<p>Objective-C has the power of Ruby, with duck-typing and dynamic dispatch in the object layer. At the same time it has the power of C, with direct access to memory layouts and static-weak typing below the object layer. Sometimes, the two powers can be combined for some unexpected results.</p>
<p>On my current project we are trying to unit test as much functionality as we reasonably can. I am quite happy to write interactionist tests, so I need test doubles. Although the Objective-C compiler does static type checking at compile time, at run-time Objective-C objects will respond to any message for which they have a method defined.</p>
<p>This makes creating test doubles very easy. Consider a controller that accepts an error delegated from a CLLocationManager, and delegates it on to a logging class. Fragments of the classes involved might look like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objective-c" style="font-family:monospace;">@interface Logger : NSObject
&nbsp;
- (void)log:(NSError *)error;
&nbsp;
@end
&nbsp;
@interface LocationSensitiveController &lt;CLLocationManagerDelegate&gt; : NSObject
&nbsp;
- (id)initWithLogger:(Logger *)logger;
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
&nbsp;
@end</pre></td></tr></table></div>

<p>In my test I would like to use a test double in place of the logger, and assert that the same error gets passed along:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objective-c" style="font-family:monospace;">- (void)testShouldPassErrorToLogger;
{
  Logger *stubLogger = // how to create the stub logger?
  LocationSensitiveController *controller = [[[LocationSensitiveController alloc] initWithLogger:stubLogger] autorelease];</pre></td></tr></table></div>

<p>The stub logger need only understand the log: message to serve its purpose. It does not need to have any relationship to the Logger class. I&#8217;ve been calling these classes &#8220;Pretend&#8230;&#8221; because the class is only pretending to be the other type. They would be stubs in the <a href="http://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs">Test Double taxonomy</a> that Martin Fowler popularised.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objective-c" style="font-family:monospace;">@interface PretendLogger : NSObject
&nbsp;
- (void)log:(NSError *)error;
- (NSError *)receivedError;
&nbsp;
@end</pre></td></tr></table></div>

<p>The compiler will reject a straight assignment:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objective-c" style="font-family:monospace;">  Logger *stubLogger = [[[PretendLogger alloc] init] autorelease]; // type error</pre></td></tr></table></div>

<p>The low-level C power can be used to convince the compiler otherwise:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objective-c" style="font-family:monospace;">  Logger *stubLogger = (Logger *)[[[PretendLogger alloc] init] autorelease];</pre></td></tr></table></div>

<p>In C this type of cast is sometimes called a brutal cast. The cast tells the compiler to interpret the same area of memory as a different type. All Objective-C classes share the same basic memory layout, so in the example the cast &#8220;sneaks&#8221; the PretendLogger past the compile-time static checking and in to the LocationSensitiveController instance. There it will receive messages intended for Logger, and because it implements a method for the same selector (log:), the code will run successfully.</p>
<p>Using a cast, I can write the test using my PretendLogger class:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objective-c" style="font-family:monospace;">@implementation LocationSensitiveControllerTest
&nbsp;
- (void)testShouldPassErrorToLogger;
{
  Logger *stubLogger = [[[PretendLogger alloc] init] autorelease];
  LocationSensitiveController *controller = [[[LocationSensitiveController alloc] initWithLogger:stubLogger] autorelease];
  NSError *expectedError = [NSError errorWithDomain:@&quot;domain string&quot; code:666 userInfo:nil];
&nbsp;
  [controller locationManager:nil didFailWithError:expectedError];
&nbsp;
  NSError *actualError = [(PretendLogger *)stubLogger receivedError];
  GHAssertEquals(expectedError, actualError, @&quot;error should be received by logger&quot;);
}
&nbsp;
@end</pre></td></tr></table></div>

<p>Eventually a <a href="http://www.mulle-kybernetik.com/software/OCMock/">mocking framework</a> makes sense, or real classes can be used with <a href="http://www.cocoadev.com/index.pl?MethodSwizzling">method swizzling</a>. When getting started on a project or a new area of code, this is a very simple approach to get some interaction tests going.</p>
<p>I&#8217;ve posted a complete xcode project incorporating the example test to <a href="http://github.com/davcamer/cheap_objc_mocks">github</a>.</p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/qlbS7ulVU3U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2010/09/objective-c-test-doubles-on-the-cheap-with-brutal-cast/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2010/09/objective-c-test-doubles-on-the-cheap-with-brutal-cast/</feedburner:origLink></item>
		<item>
		<title>multiple return values and refactoring javascript</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/HoxbMcIdT7E/</link>
		<comments>http://intwoplacesatonce.com/2010/04/multiple-return-values-and-refactoring-javascript/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 05:57:44 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[dynamic]]></category>
		<category><![CDATA[gmaps]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=103</guid>
		<description><![CDATA[I&#8217;ve been working on an entry for Victoria&#8217;s AppMyState competition. Although I started by doing some javascript and some Ruby on Rails, the RoR part quickly became superfluous and I ended up writing a purely javascript app. It&#8217;s a mash-up of Google Maps, Google&#8217;s geocoding and hopefully some Street View too, so javascript is a [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been working on an entry for Victoria&#8217;s AppMyState competition. Although I started by doing some javascript and some Ruby on Rails, the RoR part quickly became superfluous and I ended up writing a purely javascript app. It&#8217;s a mash-up of Google Maps, Google&#8217;s geocoding and hopefully some Street View too, so javascript is a good fit. There&#8217;s also a python script to massage the government data in to a useable form, but that&#8217;s behind the scenes.</p>
<p>The data I&#8217;m displaying is divided in to 11 categories and I want to treat each category separately, so I was ending up with repetitive code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> horseTroughMgr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MarkerManager<span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> horseTroughMarkers <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> Horse_Trough.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
while <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> trough <span style="color: #339933;">=</span> Horse_Trough<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>trough.<span style="color: #660066;">latitude</span><span style="color: #339933;">,</span> trough.<span style="color: #660066;">longitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> createMarker<span style="color: #009900;">&#40;</span>point<span style="color: #339933;">,</span><span style="color: #3366CC;">'&lt;h4&gt;'</span> <span style="color: #339933;">+</span> trough.<span style="color: #660066;">category</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/h4&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  horseTroughMarkers.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  horseTroughMgr.<span style="color: #660066;">addMarker</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
horseTroughMgr.<span style="color: #660066;">refresh</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> litterBinMgr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MarkerManager<span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> Litter_Bin.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
while <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> bin <span style="color: #339933;">=</span> Litter_Bin<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>bin.<span style="color: #660066;">latitude</span><span style="color: #339933;">,</span> bin.<span style="color: #660066;">longitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> createMarker<span style="color: #009900;">&#40;</span>point<span style="color: #339933;">,</span><span style="color: #3366CC;">'&lt;h4&gt;'</span> <span style="color: #339933;">+</span> bin.<span style="color: #660066;">category</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/h4&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  litterBinMgr.<span style="color: #660066;">addMarker</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #CC0000;">19</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
litterBinMgr.<span style="color: #660066;">refresh</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> hoopMgr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MarkerManager<span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> Hoop.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
while <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> hoop <span style="color: #339933;">=</span> Hoop<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>hoop.<span style="color: #660066;">latitude</span><span style="color: #339933;">,</span> hoop.<span style="color: #660066;">longitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> createMarker<span style="color: #009900;">&#40;</span>point<span style="color: #339933;">,</span><span style="color: #3366CC;">'&lt;h4&gt;'</span> <span style="color: #339933;">+</span> hoop.<span style="color: #660066;">category</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/h4&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  hoopMgr.<span style="color: #660066;">addMarker</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #CC0000;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
hoopMgr.<span style="color: #660066;">refresh</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8230; and 8 more similar blocks.</p>
<p>The first block has an extra list of markers, and I wanted to add that to the other blocks. I also had an intuition that I didn&#8217;t need both the list and the manager. I was holding back because I wasn&#8217;t sure, and with the duplication I didn&#8217;t feel that confident about refactoring. Then I remembered I could have two return values, and things started getting easy:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> setupCategory<span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> minZoom<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> mgr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MarkerManager<span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> list <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> data.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
  while <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> item <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>item.<span style="color: #660066;">latitude</span><span style="color: #339933;">,</span> item.<span style="color: #660066;">longitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> createMarker<span style="color: #009900;">&#40;</span>point<span style="color: #339933;">,</span><span style="color: #3366CC;">'&lt;h4&gt;'</span> <span style="color: #339933;">+</span> item.<span style="color: #660066;">category</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/h4&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    list.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mgr.<span style="color: #660066;">addMarker</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> minZoom<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  mgr.<span style="color: #660066;">refresh</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span> manager<span style="color: #339933;">:</span> mgr<span style="color: #339933;">,</span> list<span style="color: #339933;">:</span> list <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">37.8062649904</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">144.96165842</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  map.<span style="color: #660066;">setUIToDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">var</span> trough <span style="color: #339933;">=</span> setupCategory<span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> Horse_Trough<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> bin <span style="color: #339933;">=</span> setupCategory<span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> Litter_Bin<span style="color: #339933;">,</span> <span style="color: #CC0000;">19</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> hoop <span style="color: #339933;">=</span> setupCategory<span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> Hoop<span style="color: #339933;">,</span> <span style="color: #CC0000;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Having it in this form made it <b>very</b> easy to figure out that I didn&#8217;t need the manager returned from setupCategory(), and I could only return the list. And because it was only in one place, it was easy to change.</p>
<p>This progression works well for me when I&#8217;m refactoring: eliminate the duplication, which makes it easier to see a way to simplify the code. Sometimes simplifying the code exposes more duplication and it turns in to a cycle, but not always. I&#8217;m often tempted to try to do this in two steps, but that usually ends up in trouble where I&#8217;m making mistakes, breaking things, and then hacking them back together.</p>
<p>Javascript&#8217;s object literals made it easy to return two values which is what I needed here. In C# I would have needed a new class or some anonymous class and reflection. (Or maybe the dynamic keyword in C#4?) It is higher friction in static languages. This must be the reduced friction dynamic language enthusiasts brag about!</p>
<p>I should note that this seems not to be the best way to handle markers in many categories. It&#8217;s better to have a manager per-category. </p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/HoxbMcIdT7E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2010/04/multiple-return-values-and-refactoring-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2010/04/multiple-return-values-and-refactoring-javascript/</feedburner:origLink></item>
		<item>
		<title>remote desktop connection to localhost: a regression in Windows 7?</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/63cbkBvXK0w/</link>
		<comments>http://intwoplacesatonce.com/2010/01/remote-desktop-connection-to-localhost-a-regression-in-windows-7/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 12:57:16 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[putty]]></category>
		<category><![CDATA[rdc]]></category>
		<category><![CDATA[remote desktop connection]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[ssh tunnel]]></category>
		<category><![CDATA[tcpip]]></category>
		<category><![CDATA[win7]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[winxp]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=101</guid>
		<description><![CDATA[I maintain a Windows server. It is web-facing, and lives in a DMZ on the other side of the world from me. I have to install new programs every now and then. Windows being Windows, it&#8217;s easiest to do this with a desktop session. Remote Desktop Connection is the key tool for doing this. Since [...]]]></description>
				<content:encoded><![CDATA[<p>I maintain a Windows server. It is web-facing, and lives in a DMZ on the other side of the world from me. I have to install new programs every now and then. Windows being Windows, it&#8217;s easiest to do this with a desktop session. Remote Desktop Connection is the key tool for doing this. Since the version of Remote Desktop Protocol (RDP) I&#8217;m connecting to <a href="http://en.wikipedia.org/wiki/Remote_Desktop_Protocol">isn&#8217;t secure</a> over the public Internet I use an <a href="http://en.wikipedia.org/wiki/Ssh_tunnel#SSH_tunneling">ssh tunnel</a> to connect. This is easy to <a href="http://oldsite.precedence.co.uk/nc/putty.html">set-up in Putty</a>.</p>
<p><img src="http://intwoplacesatonce.com/wp-content/uploads/2010/01/01_initial_settings.png" alt="01_initial_settings.png" border="0" width="466" height="448" /></p>
<p>An ssh tunnel works by accepting packets on one side of the ssh connection, and putting them back in to the TCP/IP stack on the other side of the tunnel &#8212; as if the packets originated from the &#8220;far&#8221; computer. This can be done in either direction. In the screenshot above I&#8217;ve configured a tunnel accepting packets on my local machine. They will be re-injected on the remote machines stack addressed to &#8220;localhost:3389&#8243;. In other words a program connecting to my computer&#8217;s port 3390 will actually connect to the remote computer&#8217;s port 3389. Port 3389 is Remote Desktop Protocol, so if I point RDC at localhost:3390, I&#8217;ll connect to the remote computer&#8217;s RDP server.  </p>
<p><img src="http://intwoplacesatonce.com/wp-content/uploads/2010/01/02_RDC_connection_localhost.png" alt="02_RDC_connection_localhost.png" border="0" width="421" height="259" /></p>
<p>I recently started using Windows 7 and this set up broke. It seems in Windows 7, Remote Desktop Connection prevents connections to localhost. Trying to work around the limit using 127.0.0.1 or your public IP address or computer name does not work either. RDC still recognises that you are, apparently, connecting to the computer you are already connected to. This is an awkward limitation when using an ssh tunnel or some other connection forwarding.</p>
<p>Luckily there is a workaround.</p>
<p>Apparently Windows XP before service pack 2 had <a href="http://www.bitvise.com/remote-desktop">this same limitation</a>. People worked around it by pointing RDC at 127.0.0.2. It&#8217;s not used that often, but the whole range of addresses starting with 127 are all routed back to the local machine. In other words you always <a href="http://en.wikipedia.org/wiki/Localhost#IPv4">have a /8 network</a> running on your own machine. To make this work, I had to check the &#8220;Local ports accept connections from other hosts&#8221; option for putty. Without the option putty will only listen for connections to address 127.0.0.1. With the option it accepts connections on any address. Now I can point RDC at 127.0.0.2:3390 and get connected to the remote desktop, securely.</p>
<p><img src="http://intwoplacesatonce.com/wp-content/uploads/2010/01/03_revised_settings.png" alt="03_revised_settings.png" border="0" width="466" height="448" /></p>
<p>It seems a strange limitation for RDC to refuse to connect to localhost. I can understand the initial idea; having this limit would prevent remoting to a computer you are already remoted to. That&#8217;s an easy enough mistake to make if you are managing several servers, and it&#8217;s a nice save. The strange bit is that someone repealed the limit in XP SP2, but now it is back again. How does that happen? Was SP2 on a branch, and they forgot to merge it back? Was the limit in the original spec, and the spec didn&#8217;t get updated when the limitation was removed? Did they just decide the limit feature was back in? As someone stung by the reintroduction of the feature, it feels like an accidental regression.</p>
<p><img src="http://intwoplacesatonce.com/wp-content/uploads/2010/01/04_RDC_connection_127_0_0_21.png" alt="04_RDC_connection_127_0_0_2.png" border="0" width="421" height="259" /></p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/63cbkBvXK0w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2010/01/remote-desktop-connection-to-localhost-a-regression-in-windows-7/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2010/01/remote-desktop-connection-to-localhost-a-regression-in-windows-7/</feedburner:origLink></item>
		<item>
		<title>moving on to go, but ending up much further afield</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/QPb5_KzKE3Y/</link>
		<comments>http://intwoplacesatonce.com/2010/01/moving-on-to-go-but-ending-up-much-further-afield/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 12:51:11 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[broken code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code on bitbucket]]></category>
		<category><![CDATA[composition over inheritance]]></category>
		<category><![CDATA[duplication]]></category>
		<category><![CDATA[go]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=90</guid>
		<description><![CDATA[While I was preparing my last blog post about mixins in C#, I was also reading about go. From looking at go&#8217;s syntax, I thought I would be able to replace the C# code one-for-one with go code and end up with a valid program. I thought this would be the code: // Not actually [...]]]></description>
				<content:encoded><![CDATA[<p>While I was preparing my last blog post about mixins in C#, I was also reading about go. From looking at go&#8217;s syntax, I thought I would be able to replace the C# code one-for-one with go code and end up with a valid program. I thought <a href="http://bitbucket.org/davcamer/csharp_to_go/changeset/cd1b05edfddb/">this would be the code</a>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="none" style="font-family:monospace;">// Not actually valid go!
&nbsp;
package main
&nbsp;
import &quot;fmt&quot;
&nbsp;
type IAddress interface {
	StreetNumber string;
	StreetName string;
}
func (a IAddress) ToOneLineFormat() string {
	return a.StreetNumber() + &quot; &quot; + a.StreetName()
}
&nbsp;
type Address1 struct {
	StreetNumber, StreetName string
}
type Address2 struct {
	StreetNumber, StreetName string
}
&nbsp;
func main() {
	address1 := &amp;Address1{&quot;12A&quot;, &quot;Spencer Street&quot;};
	fmt.Println(address1.ToOneLineFormat());
&nbsp;
	address2 := &amp;Address2{&quot;12A&quot;, &quot;Spencer Street&quot;};
	fmt.Println(address2.ToOneLineFormat())
}</pre></td></tr></table></div>

<p>I liked this code. It&#8217;s slightly more lightweight than the equivalent C# because the interfaces don&#8217;t need to be explicitly declared on the implementing classes. Otherwise it&#8217;s quite similar. Declaring funcs away from types seemed a natural analogue to the interface + extension methods approach I described in the last post. </p>
<p>But this is not valid go code. Why not?</p>
<p>The first point is, that I&#8217;ve confused C#&#8217;s concept of properties with both fields and methods in my go code. The declarations in the structs can remain as fields, but the declarations in the interface must <a href="http://bitbucket.org/davcamer/csharp_to_go/changeset/38a29a5faf45/">change to be methods</a>. My interface needs to be:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="none" style="font-family:monospace;">type IAddress interface {
	StreetNumber() string;
	StreetName() string;
}</pre></td></tr></table></div>

<p>Now, to conform to the interface the two Address types need to have <a href="http://bitbucket.org/davcamer/csharp_to_go/changeset/512eecb12230/">methods that correspond to the interface</a>. Not fields.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="none" style="font-family:monospace;">type Address1 struct {
	streetNumber, streetName string
}
func (a Address1) StreetNumber() { return a.streetNumber }
func (a Address1) StreetName() { return a.streetName }
type Address2 struct {
	streetNumber, streetName string
}
func (a Address2) StreetNumber() { return a.streetNumber }
func (a Address2) StreetName() { return a.streetName }</pre></td></tr></table></div>

<p>Address1 and Address2 now both conform to the IAddress interface, though at the price of duplicate property/accessor/getter code. Accessors like this aren&#8217;t particularly idiomatic for go, so there is no syntactic sugar to support them. Members are intended to either be fields, possibly public, or methods implementing significant behaviour.</p>
<p>The next problem arises because in go methods cannot be defined on interfaces. The syntax would seem to allow it, but it is simply illegal. The receive of a method must be a pointer to a named type or a named type itself. No interfaces. And also none of the familiar basic types like int, float and so on because they are unnamed types. A particular named type that aliases a basic type can have methods defined on it however. Coming back to this experiment, ToOneLineFormat needs to <a href="http://bitbucket.org/davcamer/csharp_to_go/changeset/c364216640fc/">have a concrete receiver</a>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="none" style="font-family:monospace;">func (a Address1) ToOneLineFormat() string {
	return a.StreetNumber() + &quot; &quot; + a.StreetName()
}
func (a Address2) ToOneLineFormat() string {
	return a.StreetNumber() + &quot; &quot; + a.StreetName()
}</pre></td></tr></table></div>

<p>At this point I have brought back all the duplication that I was hoping to eliminate. On the up side, I have working go code.</p>
<p>Go has its own mechanism to reduce duplication. Its based on composing new types from existing types. A type can have an unnamed field of another type. The properties of the second, contained type can be accessed as if they were properties of the containing type. Address1 and Address2 could be <a href="http://bitbucket.org/davcamer/csharp_to_go/changeset/34e541d9a3cf/">defined in terms of</a> a BaseAddress type.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="none" style="font-family:monospace;">type BaseAddress struct {
	streetNumber, streetName string
}
type Address1 struct {
	BaseAddress
}
type Address2 struct {
	BaseAddress
}</pre></td></tr></table></div>

<p>These new versions of Address1 and 2 will have exactly the same fields as the old type. An object composed like this can also receive methods as if it were an object of the anonymous field&#8217;s type. This allows us to move the  ToOneLineFormat method on to BaseAddress directly. Also, since StreetNumber() and StreetName() simply return the value of fields which are available on BaseAddress, we can remove them. This in turn means IAddress is no longer useful. The complete code for Address1 and Address2 is significantly more compact. Note that the initialisation expression does need to change now, to recognise the anonymous BaseAddress field.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="none" style="font-family:monospace;">type BaseAddress struct {
	streetNumber, streetName string
}
func (BaseAddress a) ToOneLineFormat() {
	return a.streetNumber + &quot; &quot; + a.streetName
}
type Address1 struct {
	BaseAddress
}
type Address2 struct {
	BaseAddress
}
&nbsp;
func main() {
	address1 := &amp;Address1{BaseAddress{&quot;12A&quot;, &quot;Spencer Street&quot;, &quot;Melbourne&quot;, &quot;VIC&quot;, &quot;3000&quot;}};
	fmt.Println(address1.ToOneLineFormat());
&nbsp;
	address2 := &amp;Address2{BaseAddress{&quot;12A&quot;, &quot;Spencer Street&quot;, &quot;Melbourne&quot;, &quot;VIC&quot;, &quot;3000&quot;}};
	fmt.Println(address2.ToOneLineFormat())
}</pre></td></tr></table></div>

<p>Address1 and Address2 themselves are looking redundant now. Having a BaseAddress with two classes that &#8220;inherit&#8221; from it seems to clash strongly with the ideas of go. Based on this exercise, I believe an anonymous field still needs to capture some freestanding meaning of its own. The two types are a somewhat artificial constraint anyway. I&#8217;ll leave them here, as they were the two classes that motivated this experiment originally.</p>
<p>Hopefully in go, you won&#8217;t often end up in the same situation we faced in C#, needing two structurally identical types.</p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/QPb5_KzKE3Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2010/01/moving-on-to-go-but-ending-up-much-further-afield/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2010/01/moving-on-to-go-but-ending-up-much-further-afield/</feedburner:origLink></item>
		<item>
		<title>in C# 3.5: interface + extension methods = mixin</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/oCzXisXW5CE/</link>
		<comments>http://intwoplacesatonce.com/2009/12/in-c-3-5-interface-extension-methods-mixin/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 01:34:21 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[duplication]]></category>
		<category><![CDATA[extension methods]]></category>
		<category><![CDATA[mixin]]></category>
		<category><![CDATA[partial classes]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[svcutil]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=81</guid>
		<description><![CDATA[On my current project, we have ended up with several classes that have the same, or nearly the same fields. The classes are generated from xsds that describe a set of SOAP services that we integrate with. We have tried avoiding the generation or tweaking the xsds to avoid the situation, but accepting the duplicate [...]]]></description>
				<content:encoded><![CDATA[<p>On my current project, we have ended up with several classes that have the same, or nearly the same fields. The classes are generated from xsds that describe a set of SOAP services that we integrate with. We have tried avoiding the generation or tweaking the xsds to avoid the situation, but accepting the duplicate classes actually seemed to be the best way forward. So, we have code in a generated file like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> ServiceClients<span style="color: #008000;">.</span><span style="color: #0000FF;">Generated</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Address1
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> StreetNumber <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> StreetName <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Suburb <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> State <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> PostCode <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Address2
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> StreetNumber <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> StreetName <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Suburb <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> State <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> PostCode <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">set</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Besides the obvious problem with duplication, this code is also difficult to extend. As just one example, we wanted to display addresses in a one-line format:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">var</span> address <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Address1
                  <span style="color: #008000;">&#123;</span>
                      StreetNumber <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;12A&quot;</span>,
                      StreetName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Spencer Street&quot;</span>,
                      Suburb <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Melbourne&quot;</span>,
                      State <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;VIC&quot;</span>,
                      PostCode <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;3000&quot;</span>,
                  <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;12A Spencer Street, Melbourne, VIC 3000&quot;</span>,
                address<span style="color: #008000;">.</span><span style="color: #0000FF;">ToOneLineFormat</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The implementation for this method is fairly simple, but where can we implement it so that we will only need to write it once? Ideally what we would like is a <a href="http://en.wikipedia.org/wiki/Mixin">mixin</a>: a way of adding new methods to a class without adding any fields, and without necessarily changing the type. Although C# does not have a language facility for mixins, we can get a similar effect by using an interface and an extension method.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> ServiceClients<span style="color: #008000;">.</span><span style="color: #0000FF;">Generated</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Extensions</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IAddress
	<span style="color: #008000;">&#123;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> StreetNumber <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> StreetName <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> Suburb <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> State <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> PostCode <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">get</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> AddressExtensions
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> ToOneLineFormat<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IAddress address<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> format <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;{0} {1}, {2}, {3} {4}&quot;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>format,
					address<span style="color: #008000;">.</span><span style="color: #0000FF;">StreetNumber</span>,
					address<span style="color: #008000;">.</span><span style="color: #0000FF;">StreetName</span>,
					address<span style="color: #008000;">.</span><span style="color: #0000FF;">Suburb</span>,
					address<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span>,
					address<span style="color: #008000;">.</span><span style="color: #0000FF;">PostCode</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>There is one more step. In C# the two concrete types need to explicitly implement the <code>IAddress</code> interface so that we can use the <code>ToOneLineFormat</code> method on them. I&#8217;ve never had much use for partial classes, but they were a lifesaver in this case. In another file away from the mammoth 40,000 line long svcutil generated file, the interface can be easily added to both classes.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> ServiceClients<span style="color: #008000;">.</span><span style="color: #0000FF;">Generated</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Address1 <span style="color: #008000;">:</span> IAddress <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Address2 <span style="color: #008000;">:</span> IAddress <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And there it is: a mixin! The ToOneLineFormat method is defined in one place, can be used with either Address class, and there is no need to change the generated code or the inheritance hierarchy.</p>
<p>For a time I was quite sure I had heard that methods implemented directly on interfaces would be part of C# 4. I must have been delusional though, because it is not on the list of new features. If it were, it seems it would just be syntax sugar for the above approach.</p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/oCzXisXW5CE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2009/12/in-c-3-5-interface-extension-methods-mixin/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2009/12/in-c-3-5-interface-extension-methods-mixin/</feedburner:origLink></item>
		<item>
		<title>sleeping in batch files</title>
		<link>http://feedproxy.google.com/~r/InTwoPlacesAtOnce/~3/yz_fyazGoes/</link>
		<comments>http://intwoplacesatonce.com/2009/11/sleeping-in-batch-files/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 12:48:52 +0000</pubDate>
		<dc:creator>DCam</dc:creator>
				<category><![CDATA[automation]]></category>
		<category><![CDATA[batch files]]></category>
		<category><![CDATA[cmd scripts]]></category>
		<category><![CDATA[CruiseControl.net]]></category>
		<category><![CDATA[debugging]]></category>

		<guid isPermaLink="false">http://intwoplacesatonce.com/?p=73</guid>
		<description><![CDATA[I was recently debugging a situation involving CruiseControl.net where one &#60;exec&#62; task would not complete until a second &#60;exec&#62; task in an unrelated project completed. When it was originally described on the mailing list, I thought it might be some interaction between batch files and the parallel task in the ProcessExecutor class. ProcessExecutor encapsulates the [...]]]></description>
				<content:encoded><![CDATA[<p>I was recently debugging a situation involving CruiseControl.net where one <a href="http://confluence.public.thoughtworks.org/display/CCNET/Executable+Task">&lt;exec&gt; task</a> would not complete until a second &lt;exec&gt; task in an unrelated project completed.</p>
<p>When it was originally <a href="http://groups.google.com/group/ccnet-devel/browse_thread/thread/fdbc5228726a122d">described on the mailing list</a>, I thought it might be some interaction between batch files and the parallel task in the <a href="http://ccnet.svn.sourceforge.net/viewvc/ccnet/trunk/project/core/util/ProcessExecutor.cs?revision=6352&#038;view=markup">ProcessExecutor class</a>. ProcessExecutor encapsulates the concurrency aspect of running an external process and if there is a concurrency problem with processes, it&#8217;s probably in that class.</p>
<p>In any case, at that point I was seriously misunderstanding the problem, but it led me to some interesting debugging. For the debugging, I wanted to create a few long running batch files to run in parallel. But what is the batch equivalent of Thread.Sleep?</p>
<p>The <code>pause</code> command came to mind. It has no capabilities beyond displaying a &#8220;press any key&#8221; prompt and waiting for input. No parameters or switches at all. There is no <a href="http://bashcurescancer.com/man/cmd/sleep"><code>sleep</code></a> command, as there is in bash. <code>wait</code> came to mind, but it is not a command either. I went to google&#8230;</p>
<p>And met <a href="http://www.velocityreviews.com/forums/t172496-bat-file-pauses.html">with success</a>. The <code>choice</code> command can be used as a timed delay. The forum I found it in made it sound as if  <code>choice</code> is not available on every platform, but my Windows Server 2003 development image has it. The exact syntax of the command does seem to vary between versions, as some example on the web do not work on my system. For me, a command to wait five seconds is</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cmd" style="font-family:monospace;">choice /M:&quot;Waiting for 5 seconds&quot; /T:5 /D:Y /C:Y</pre></td></tr></table></div>

<p>A simple one-line batch file that waits for a given number of seconds is</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cmd" style="font-family:monospace;">@choice /M:&quot;Waiting for %1 seconds&quot; /T:%1 /D:Y /C:Y</pre></td></tr></table></div>

<p>As with many command-prompt commands, you can see more information about the command including the exact syntax for your version with</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cmd" style="font-family:monospace;">choice /?</pre></td></tr></table></div>

<p>If you are writing a cmd or bat script and need to wait or sleep for a short time, <code>choice</code> is well, choice. </p>
<img src="http://feeds.feedburner.com/~r/InTwoPlacesAtOnce/~4/yz_fyazGoes" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intwoplacesatonce.com/2009/11/sleeping-in-batch-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://intwoplacesatonce.com/2009/11/sleeping-in-batch-files/</feedburner:origLink></item>
	</channel>
</rss>
