<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Making the Complex Simple</title>
	<atom:link href="http://simpleprogrammer.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://simpleprogrammer.com</link>
	<description>Software Development from John Sonmez&#039;s Perspective</description>
	<lastBuildDate>Thu, 29 Jul 2010 14:48:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='simpleprogrammer.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/01d1cc4142011fffd37f6e47974484f5?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Making the Complex Simple</title>
		<link>http://simpleprogrammer.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://simpleprogrammer.com/osd.xml" title="Making the Complex Simple" />
	<atom:link rel='hub' href='http://simpleprogrammer.com/?pushpress=hub'/>
		<item>
		<title>The Best Way to Unit Test in Android: Part 2</title>
		<link>http://simpleprogrammer.com/2010/07/29/the-best-way-to-unit-test-in-android-part-2/</link>
		<comments>http://simpleprogrammer.com/2010/07/29/the-best-way-to-unit-test-in-android-part-2/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 14:34:35 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=1027</guid>
		<description><![CDATA[In my last post I presented two choices for unit testing in Android. Unit test on the real device or emulator using the real android framework Unit test on our PC using the JVM Each choice has some pros and cons, but for me it came down to the speed and flexibility allowed by running [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1027&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>In my last post I presented two choices for unit testing in Android.</p>
<ul>
<li>Unit test on the real device or emulator using the real android framework</li>
<li>Unit test on our PC using the JVM</li>
</ul>
<p>Each choice has some pros and cons, but for me it came down to the speed and flexibility allowed by running in a real JVM.  I actually tried to create an Android unit testing project using the scaffolding provided by Google but it turned out to be very restrictive, especially since I couldn’t use JMock.</p>
<p>There is also something to be said for being able to execute the unit tests extremely quickly.  If you have to wait for an emulator to come up, or try to run them on a real device, it is less likely the unit tests will be run.</p>
<h2>How to do it</h2>
<p>The basic idea of this approach is fairly simple.</p>
<p>We are going to try and abstract away any Android specific calls from our application and then unit test the plain Java logic in the application just like we would any other Java project.</p>
<h4>Step 1:  Create a JUnit project</h4>
<p><strong></strong>Create a new JUnit project just like you would for unit testing any Java application.</p>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/newjavaproject.png"><img style="display:inline;border-width:0;" title="NewJavaProject" src="http://complextosimple.files.wordpress.com/2010/07/newjavaproject_thumb.png?w=498&#038;h=477" border="0" alt="NewJavaProject" width="498" height="477" /></a></p>
<p>Make sure you chose <strong>Java Project</strong> rather than <strong>Android Test Project.</strong></p>
<p>From here you can add your JUnit references to your build path.  <em>I would also recommend adding </em><a href="http://code.google.com/p/instinct/"><em>Instinct</em></a><em> for doing BDD style testing.  Instinct uses JMock internally to create mocks for you using a declarative approach.</em></p>
<p>The only other thing you will want to do with this project is to add the android.jar to the build path.  The android.jar can be found in your Android SDK directory under the platform directory that corresponds to the API version you are targeting with your application.</p>
<p>For example, on my machine, I am targeting Android 1.6, so my android.jar is at:  C:\Android\android-sdk-windows\platforms\android-4.</p>
<p>Now remember, you are including the android.jar in the project only so your classes can understand what types exist in the Android framework.  <strong>If you even try to call a constructor on a class in android.jar, you will get an exception.</strong></p>
<h4>Step 2: Pull all the code you can out of the Activity.</h4>
<p>The next thing we need to do is clean up our Activity class to make sure that it has the smallest amount of logic in it as possible.</p>
<p>Since we can’t actually instantiate any types that exist in the Android framework, we are not going to be able to create an instance of our Activity in the test project.  So we are not going to be able to unit test our Activity.  This isn’t a problem if most of the logic we care about testing exists in other layers in our system.</p>
<p>You can design the lower layers however you want.  I am still not quite sure the best approach here, but what I did was put a presentation layer directly below the Activity.  I have a Presenter class directly below the Activity that tells my activity what to do.  The Activity passes on any interesting events or information to the Presenter.</p>
<p>You should leave the Activity with the responsibility of setting the text on its own view objects, but create methods for your lower layers to tell the Activity what text to set the objects to and so forth.  Activity is going to act as an adapter to a large portion of the Android framework for your application.</p>
<p>Because the entrance point into your application is going to be the Activity, you will need to wire up the rest of your layers from the Activity.  This is a little strange, but it works.  In my application, I create a new Presenter and pass <em>this </em>into it on the constructor.</p>
<p>When you are done, your Activity should look pretty thin.  It should delegate all its event handler logic down to the lower layers and act as a pass through for manipulating the view.</p>
<h4>Step 3: Wrap remaining Android object calls.</h4>
<p>You&#8217;re going to find that some of the remaining Android API will need to be called from lower down than the Activity.  This is not as big of a problem as it seems.</p>
<p>In my application I needed to make some calls to android.os.SystemClock.  I needed to make those calls lower down than the activity because it is part of the heart of the logic of my application.</p>
<p>I created a simple SystemClockWrapper class that wraps system clock and delegates any calls to the real SystemClock class.  Then I extracted an interface from that class called ISystemClock (hey, I like the C# convention for interfaces.)</p>
<p>Finally, in my application logic, I allowed the ISystemClock reference to be passed in or “injected” through the constructor of the class that used it.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:e333f287-e382-4a5e-8493-b182750f180f" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: java; pad-line-numbers: true;">
public class SystemClockWrapper implements ISystemClock
{
     public long getElapsedRealTime()
     {
          return SystemClock.elapsedRealtime();
     }

}

public interface ISystemClock
{
     public abstract long getElapsedRealTime();

}
</pre>
</div>
<p>In my unit test I just pass in a mock version of the ISystemClock interface.</p>
<p>One hint here is to use the refactor tool in your IDE to extract an interface.  This can save you some time of trying to manually create interfaces for large pieces of functionality you are wrapping.</p>
<h2>Putting it all together</h2>
<p>Now you should be able to create your unit tests against most of your application logic and run them just like any other unit test project.</p>
<p>The only thing you won’t be able to do is unit test the logic in your Activity classes.  This shouldn’t be much of a problem if you have made them into views, because they should only contain view logic.</p>
<p>If you really want to be able to write some tests for the Activity though, you can create another actual <em>Android Test Project </em>and use Google’s framework to write unit tests from there.</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.  Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1027/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1027&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/29/the-best-way-to-unit-test-in-android-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/newjavaproject_thumb.png" medium="image">
			<media:title type="html">NewJavaProject</media:title>
		</media:content>
	</item>
		<item>
		<title>The Best Way to Unit Test in Android: Part 1</title>
		<link>http://simpleprogrammer.com/2010/07/27/the-best-way-to-unit-test-in-android/</link>
		<comments>http://simpleprogrammer.com/2010/07/27/the-best-way-to-unit-test-in-android/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 14:58:14 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=1018</guid>
		<description><![CDATA[I’ve been doing some development in Android lately on a top secret project, one that hopefully will change the way you run with your phone. In the course of building this app, in a previous post I mentioned that I wanted to find the right, or perfect way, to build an Android application. I haven’t [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1018&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I’ve been doing some development in Android lately on a top secret project, one that hopefully will change the way you run with your phone.</p>
<p>In the course of building this app, in <a href="http://simpleprogrammer.com/2010/07/20/the-hardest-thing-i-struggle-with/">a previous post</a> I mentioned that I wanted to find the right, or perfect way, to build an Android application.</p>
<p>I haven’t found the best way to build an Android application into a nice clean design pattern, but have found a way that seems to work, and makes the application testable and easy to maintain.</p>
<p>I do believe though, that I have found the optimal way to do unit testing in Android right now.  Yes, a bold statement, but I have looked high and low for a better solution, and can’t find one.</p>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/htcandroid.png"><img style="display:inline;border:0;" title="HTC-Android" src="http://complextosimple.files.wordpress.com/2010/07/htcandroid_thumb.png?w=455&#038;h=342" border="0" alt="HTC-Android" width="455" height="342" /></a></p>
<h2>The problems</h2>
<p>So first a little bit of background on the problem with unit testing in Android.  Take what I say with a grain of salt here, because I am not an Android platform expert, and feel free to correct me if I misstate something.</p>
<h4>Android.jar</h4>
<p>If you download the Android SDK from google, you will find that the android.jar you get with the SDK is much like a C++ header file; it doesn’t actually contain any working code.  As a matter of fact, all the methods are stubbed out to throw an exception with the message “Stub!” when you call them.  How cute.</p>
<p>The real android.jar implementations live on the emulator, or your real Android device.  So, if you want to actually run any code that is going to call any methods or try to construct real Android framework classes, <strong>you must run that code inside the emulator or a real device.</strong></p>
<h4><strong>Dalvik VM</strong></h4>
<p>When you&#8217;re working with Android, it sure feels like you are writing regular old standard Java SE code, <strong>but you are not. </strong>You probably won’t notice the difference, as almost everything that you need is there in the Dalvik VM implementation of Java.</p>
<p>Dalvik is not even a Java Virtual Machine at all.  That is right, it runs on cross-complied .class files which are converted to a .dex format.  <strong>Yes, is does not use java byte code.</strong></p>
<p>Why do I mention all this?  Because you might want to use something like JMock to mock your dependencies when writing your unit tests.</p>
<p>Well, you can’t.  It just isn’t going to work, because the Dalvik VM doesn’t use java byte code, so the reflective coolness that mocking frameworks like JMock use doesn’t work the same.</p>
<p>Be aware that any external library you try to use in your Android project may not work because Android does not support the full Java SE implementation.  It actually is a subset of the <a href="http://en.wikipedia.org/wiki/Apache_Harmony">Apache Harmony</a> Java implementation.</p>
<h4>There is no main()</h4>
<p>Where does an Android application start running?  From the activity.  Which is basically the view.  (Some people will argue this is the controller or presenter.  And yes, I agree in respect to the Android framework it is, but in regard to your application framework it is the view.)</p>
<p>Android applications define a starting activity and launch from there.  Activities can even be launched from other applications.</p>
<p>This tends to disrupt most of our MVC, MVP, MVVP pattern architectures, as the view is going to be the entry point and will have to be responsible for initializing the rest of the application.  (That is not entirely true, as there is an android.app.Application class that gets called the first time your app is run, to launch the main activity, but for the most part you can’t do much here.)</p>
<p>Essentially though, you have to build your architecture based on each Activity being its own separate little application, with the entry point being in the activity.  This puts some serious constraints on unit testing.</p>
<h2>Putting it all together</h2>
<p>So if I can sum up the problems briefly, I would say:</p>
<ul>
<li>Android framework code has to run in the emulator or on a device.</li>
<li>Dalvik VM doesn’t allow us to use our standard mocking frameworks.</li>
<li>Entry points for our applications are in the views.</li>
</ul>
<p>The first problem, combined with the second, lead us to an interesting choice.  Should we run our unit tests on an actual emulator or device using the Dalvik VM, or should we run them in a JVM?</p>
<p>It is probably not an obvious question, but let me explain why it is the most relevant.</p>
<p>In writing an application, we are going to have application logic that has nothing specifically to do with the Android platform, and we are going to have Android platform specific logic (drawing views, handling Android OS events, interacting with Android APIs etc.)</p>
<p>If we want to write true unit tests, we need to isolate our classes and test them individually.  We should be able to do this for our application logic, without relying on the Android framework.  If we don’t rely on the Android framework, we don’t need to run on a real or emulated device, thus we are not constrained to the Dalvik VM.</p>
<p><strong>If we choose to run our unit test code on a real or emulated device:</strong></p>
<ul>
<li>We will be able to use the Android framework APIs in our testing efforts.  For example, we can create new location objects instead of mocking them up.</li>
<li>We will be completely true to the real execution of our code.  Since we will be using the real VM the code will run on.</li>
<li>Since we are running our tests on a device or emulator, they will run much slower.</li>
<li>We won’t be able to use <a href="http://www.jmock.org/">JMock</a>, EasyMock, or Mockito, we’ll either have to roll our own mocks or use a fledgling Android mocking framework.</li>
</ul>
<p><strong>If we chose to run our unit test code in a JVM on our PC:</strong></p>
<ul>
<li>We will have the full power of the JVM available to our test code, so we can use mocking frameworks like JMock, and BDD frameworks like <a href="http://code.google.com/p/instinct/">Instinct</a>.</li>
<li>We will run our unit tests much faster, since they will be using our PC instead of a device.</li>
<li>We can use standard unit testing practices and not have to inherit from special Android classes, or use special Android test runners.</li>
<li>We will have to wrap any calls to the actual Android framework if we need to use any Android classes or services deeper down in our application.</li>
<li>We have a small risk of having different behavior between running the tests and the real application, because we will be running the code on different VMs.</li>
</ul>
<p>In my next post, I’ll detail which option I chose and why and also give some detailed steps of how to get setup and running.</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.  Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1018/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1018/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1018/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1018/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1018/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1018/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1018/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1018/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1018/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1018/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1018&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/27/the-best-way-to-unit-test-in-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/htcandroid_thumb.png" medium="image">
			<media:title type="html">HTC-Android</media:title>
		</media:content>
	</item>
		<item>
		<title>News Flash: SQL Server Paging Still Sucks!</title>
		<link>http://simpleprogrammer.com/2010/07/22/news-flash-sql-server-paging-still-sucks/</link>
		<comments>http://simpleprogrammer.com/2010/07/22/news-flash-sql-server-paging-still-sucks/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 14:12:13 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=1013</guid>
		<description><![CDATA[Last time I wrote some code to allow paging of stored procedure results in SQL Server it sucked. That was about 3-4 years ago. I just wrote some code to do it again, and well…  It still sucks. Perhaps I’m doing it wrong, but if you are looking for, “how to implement paging in sql [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1013&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Last time I wrote some code to allow paging of stored procedure results in SQL Server it sucked.</p>
<p>That was about 3-4 years ago.</p>
<p>I just wrote some code to do it again, and well…  It still sucks.</p>
<p>Perhaps I’m doing it wrong, but if you are looking for, “how to implement paging in sql server”, or “how to page sql server stored procedure results”, or “paging sql server”, I’ll give you my best answer for it at this point.</p>
<p>(Google search term additions above for helping people find help)</p>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/paging.jpg"><img style="display:inline;border-width:0;" title="paging" src="http://complextosimple.files.wordpress.com/2010/07/paging_thumb.jpg?w=443&#038;h=325" border="0" alt="paging" width="443" height="325" /></a></p>
<h2>How I roll with CTEs</h2>
<p>CTEs or Common Table Expressions, are a pretty nice feature of SQL Server.  They are a bit complex, but basically they let you create a query and treat it as a table to select from.  They also allow recursive calls to themselves, which let you do cool things like query hierarchies of data.</p>
<p><a href="http://www.4guysfromrolla.com/webtech/071906-1.shtml#postadlink">This link</a> offers a pretty good explanation of CTEs.</p>
<p>Anyway, CTEs come in handy combined with the ROW_NUMBER() function to add the ability to page data from an existing stored procedure.</p>
<p>The basic idea here is to modify the original query by wrapping it inside of a CTE that adds a RowNumber column.</p>
<p>Then we can select from that query the rows that are in the range we want.</p>
<h2>Vegetables are good for you</h2>
<p>Let’s look at an example:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:c53ade08-f66e-4cfc-ac56-8d7fd09282ee" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: sql; pad-line-numbers: true;">
SELECT name, goodnessfactor, color
FROM vegetables
WHERE color = 'purple' OR color = 'green'
</pre>
</div>
<p>Now let’s modify this query to be pageable given we have the first and last record we want.  (first record = page number * rows per page, last record = page number + 1 * rows per page)</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:e669a1d3-1171-4f62-8655-96d7f96ad6dd" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: sql;">
WITH pagedVegetables AS (
    SELECT name, goodnessfactor, color,
    ROW_NUMBER() OVER (ORDER BY name) AS rownumber
    FROM vegetables
    WHERE color = 'purple' OR color='green'
)
SELECT *
FROM pagedVegetables
WHERE rownumber &gt; @firstRecord AND
               rownumber &lt;= @lastRecord
</pre>
</div>
<p>Okay, so it is actually not that bad.</p>
<p>A couple of notes to help understand what happened here.</p>
<ul>
<li>ROW_NUMBER() requires an OVER clause which basically will determine how you calculate what the row number is.  In this case, we are going to sort by name.  So the first alphabetical name will be row number 1, and so on.</li>
<li>We’ve wrapped the query in a CTE, and added an additional column so that we have a row number in our new virtual table.  (Think of the CTE as creating a temporary view for us.)</li>
<li>We are selecting everything out of the original query that is in the range of rows we want.  (Depending on how you define your range you may use different equality operators.  If you range is inclusive, you would do &lt;= and &gt;= , and if it is exclusive it would be &lt; and &gt;.  In this case we are both.)</li>
</ul>
<h2>A word on total row count</h2>
<p>If you need the total number of rows, so that you can pass that back to the pager in your code, then you will probably have to select the results from the CTE into a temporary table and return two result sets from it.</p>
<p>I couldn’t really find a way to select the total rows and the data from a single CTE expression.  If you know a way, please let me know.</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.  Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1013/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1013&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/22/news-flash-sql-server-paging-still-sucks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/paging_thumb.jpg" medium="image">
			<media:title type="html">paging</media:title>
		</media:content>
	</item>
		<item>
		<title>The Hardest Thing I Struggle With</title>
		<link>http://simpleprogrammer.com/2010/07/20/the-hardest-thing-i-struggle-with/</link>
		<comments>http://simpleprogrammer.com/2010/07/20/the-hardest-thing-i-struggle-with/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 15:34:33 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Self Improvement]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=1008</guid>
		<description><![CDATA[I ran up against it again as I was trying to figure out the “right” way to build an android application. Some of your coworkers probably don’t struggle with the issue because they really just don’t think about it that much. But, if you are reading this blog, you probably have encountered the problem I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1008&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I ran up against it again as I was trying to figure out the “right” way to build an android application.</p>
<p>Some of your coworkers probably don’t struggle with the issue because they really just don’t think about it that much.</p>
<p>But, if you are reading this blog, you probably have encountered the problem I am about to talk about.  It may be for you, like it is for me, the single greatest thing holding you back.</p>
<p>What am I talking about?</p>
<h2>The struggle with perfection</h2>
<p>We want to build perfect software, we want to build perfect code, but it is just not possible.</p>
<p>Like Tyler Duren’s alter ego, we want to put everything in a nice little box.</p>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/fightclub0015.jpg"><img style="display:inline;border-width:0;" title="fight-club-0015" src="http://complextosimple.files.wordpress.com/2010/07/fightclub0015_thumb.jpg?w=460&#038;h=346" border="0" alt="fight-club-0015" width="460" height="346" /></a></p>
<p>I had mentioned earlier that not all software developers struggle with this problem.  I think it arises when you start to actively seek to improve your development skills.  It is natural to look for the “right” way to do something you want to get better at.</p>
<ul>
<li>There is a right way to swing a baseball bat.</li>
<li>There is a right way to do mathematical calculations.</li>
<li>You can play a piece of music perfectly on a piano.</li>
</ul>
<p>But there isn’t a right way to build software.</p>
<p>There are lots of wrong ways, and then there are many better ways that all have trade-offs against each other.</p>
<p>It is vey hard to come to grips with this reality.  At least for me it is.  I want to know how am I supposed to do it, and I don’t want to hear “any way you like”, or “however seems right to you.”</p>
<h2>Software development is part craft</h2>
<p>There is some science to it.  Don’t get me wrong.</p>
<p>The biggest problem in software development is not people struggling with perfection, but rather developers believing that there are no wrong ways to develop software.</p>
<p>You can learn what is right to some degree, up to a point.  But, after you steer away from the obviously wrong, you end up drifting into the true craft of software development.</p>
<p>When you only have one tool, software development is easy.  You just hack on things with your one tool.  You might not be very effective, or very efficient, and you may make a pretty big mess, but you generally can get things done and you know what to do.</p>
<p>When you have a toolbox full of tools, the world stops being so black and white.  Software development truly becomes an art or craft at this point, as you are forced to make trade-offs and choose architectures and technologies based on experience and intuition combined.</p>
<p>Our minds fight against this concept of unrule.  It is like playing monopoly with generalizations or ideas of how the game should be played, but no explicit rules.  Sometimes life is just easier within preset boundaries that clearly tell us what is right and what is wrong.</p>
<p>It is a strange twist of fate that the act of building something that is absolutely structured and governed by rules is such a rule-less and judgment based pursuit.</p>
<h2>Dealing with perfection in an imperfect world</h2>
<p>So why is striving for perfection bad anyway?</p>
<p>Well, it can be a major roadblock that prevents us from getting things done.</p>
<p>I’ll often find myself at the 90% better than I started solution, and pushing to get to 95%.  That push to 95% can take the same amount of time it took to get to 90%.</p>
<p>Sometimes when we are looking for the perfect architecture, or trying to apply patterns, because we believe they are right, we end up making things more complicated than they need to be, or we miss a better “less perfect” solution, because we have deemed it so.</p>
<p>One thing I have tried to do to curb my insatiable desire for perfection is to <strong>strive to always improve rather than for perfection itself.</strong></p>
<p>It is important for us to recognize when we are at that 90% mark and move on.</p>
<p>Next time we encounter a similar situation, we’ll hit the 91% mark, because we will have more experience and will have built better intuition.</p>
<p>Here are some tips and strategies I have picked up for dealing with the problem of perfection.</p>
<ul>
<li>Try to get rid of the all or nothing mentality.  Don’t do things just good enough to get by, but don’t try to do them perfectly either.  Do an excellent job, and know what that is.</li>
<li>Start working on things in a rough draft form.  Fill in details later.  This especially helpful with web pages or anything that requires design work.</li>
<li>Don’t get stuck on a problem that is mainly just polish.  If the elegant way to do something is causing you 10 hours of debugging, but you can do it in a less elegant way and hide the mess in a nice package, opt for the second.  You can always come back later, when you don’t feel pressured to find a solution.</li>
<li>Get a second opinion.  If you are struggling with a design issue and going back and forth in your head about the best way to do something, ask someone else and it might make one way or another perfectly clear.</li>
<li>Make yourself a research note.  Move on for now, and make a note to come back later or to research a technology or design.</li>
</ul>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.  Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1008/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1008&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/20/the-hardest-thing-i-struggle-with/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/fightclub0015_thumb.jpg" medium="image">
			<media:title type="html">fight-club-0015</media:title>
		</media:content>
	</item>
		<item>
		<title>If You Like Living Dangerously Modify Your Hosts File</title>
		<link>http://simpleprogrammer.com/2010/07/16/if-you-like-living-dangerously-modify-your-hosts-file/</link>
		<comments>http://simpleprogrammer.com/2010/07/16/if-you-like-living-dangerously-modify-your-hosts-file/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 15:13:33 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Infrastructure]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=1002</guid>
		<description><![CDATA[I’ve been seeing it and hearing about it more and more, and every time I do, I cringe. “Hey, how do I test out our web app?” “Oh modify your hosts file so that when you go to wonkywares.com it goes to test.wonkwares.com.” “Umm… ok, that sounds great.” What this is doing What I am [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1002&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I’ve been seeing it and hearing about it more and more, and every time I do, I cringe.</p>
<blockquote><p>“Hey, how do I test out our web app?”</p>
<p>“Oh modify your hosts file so that when you go to <a href="http://wonkywares.com">wonkywares.com</a> it goes to <a href="http://test.wonkwares.com">test.wonkwares.com</a>.”</p>
<p>“Umm… ok, that sounds great.”</p></blockquote>
<h2>What this is doing</h2>
<p>What I am talking about is going to C:\windows\system32\drivers\etc\hosts and adding entries to that file that map ip addresses to host names.</p>
<p>The hosts file is basically a mapping for ip addresses to host names.</p>
<p>So if you put an entry,</p>
<p>207.46.0.87 google.com</p>
<p>in the file, when you type google.com into your browser you will go to bing.com instead. (That is the ip address for bing.com.)</p>
<p>Why would someone do this?</p>
<p>Well if you have some code, automated scripts, or process that uses your real production domain name, you can modify your hosts file so that the production host name gets resolved to the test ip address.</p>
<p>Magically, when you go to <a href="http://wonkywares.com">wonkywares.com</a> you will instead go to <a href="http://test.wonkywares.com">test.wonkywares.com</a>.</p>
<p>Yes, I do realize this may seem cool and clever.</p>
<p>Heck, you might have stopped reading this blog and called an emergency meeting so you can tell everyone on your team how they can modify their hosts file to magically make your production scripts work on test without passing in the url.</p>
<h2>What could possibly go wrong?</h2>
<p>Let me give you a scenario:</p>
<blockquote><p>“Okay Jeff, I’m gonna run the tests scripts on my new workstation to make sure everything is still working right.</p>
<p>“Joebob, how do those fancy dandy test scripts of yours work again?”</p>
<p>“Well Jeff, they drop all the data in the tests database and then create new test data, run the tests, and clean up the data after them again.”</p>
<p>… Joebob runs the test scripts, and watches as they all pass, thinking about how awesome and clever he is.</p>
<p>RING… RING… RING…</p>
<p>“Hello, this is Joebob.”</p>
<p>“Hi Joebob, this is the CEO, Mr. Wonky, I am getting reports that there are no products at all being displayed on our production website.”</p>
<p>“Oh shnikies!  I must have forgotten to modify my hosts file to point wonkywares.com to test.wonkywares.com.  I ran my test scripts against production.”</p>
<p>… Joebob pulls a wonkywares revolver out of his bottom drawer, right next to the single malt scotch he keeps for “planning day.”</p></blockquote>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/percussion_revolver_wanted.jpg"><img style="display:inline;border-width:0;" title="Don't let this happen to you" src="http://complextosimple.files.wordpress.com/2010/07/percussion_revolver_wanted_thumb.jpg?w=497&#038;h=188" border="0" alt="Don't let this happen to you" width="497" height="188" /></a></p>
<p>I am sure you can guess what happens next.</p>
<p>Let me list for you some of the bad things that can happen if you modify your host file to be clever, instead of parameterizing your scripts or code to be able to take a configurable url.</p>
<ul>
<li>You run your test scripts against production because you forgot to configure your hosts file on your new machine.</li>
<li>You think you modified some production data for a data-fix, but you actually only modified test data.</li>
<li>Someone else uses your machine and doesn’t know that you have a modified host file.</li>
<li>You use someone else&#8217;s machine and forget they don’t have a modified hosts file, so you think you are going to the test website, but you are really modifying live production data.</li>
<li>Your IT guys switch the IP addresses of the staging and production server as part of a rotation so they can do maintenance work on what was the production hardware.  You now are pointing to production instead of staging and have no clue.</li>
<li>Someone else tries to run your script or code and is not aware that they have to modify their hosts file first.  They run your scripts against production.</li>
<li>You change your hosts file and expect the change to immediately take effect, but your browser is smart and has cached the ip address of the page you just visited.</li>
</ul>
<p>I am sure you can think of plenty more reasons.</p>
<p>So instead of being “clever” and modifying your hosts file, try making whatever code you are writing take in a parameter for the url to use, or read it from a configuration file.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1002/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1002/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1002/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1002/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1002/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1002/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1002/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1002/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1002/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1002/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=1002&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/16/if-you-like-living-dangerously-modify-your-hosts-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/percussion_revolver_wanted_thumb.jpg" medium="image">
			<media:title type="html">Don't let this happen to you</media:title>
		</media:content>
	</item>
		<item>
		<title>Late to the JQuery Party</title>
		<link>http://simpleprogrammer.com/2010/07/14/late-to-the-jquery-party/</link>
		<comments>http://simpleprogrammer.com/2010/07/14/late-to-the-jquery-party/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 00:24:24 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/2010/07/14/late-to-the-jquery-party/</guid>
		<description><![CDATA[You may be wondering what happened to my usual Monday post. Well, I was having my head rearranged by JQuery.&#160; I have to admit, I am pretty late to the party.&#160; I haven’t really been using JQuery at all the past few years. I missed a lot. Goodbye type safety This part scares me a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=999&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>You may be wondering what happened to my usual Monday post.</p>
<p>Well, I was having my head rearranged by <a href="http://jquery.com/">JQuery</a>.&#160; I have to admit, I am pretty late to the party.&#160; I haven’t really been using JQuery at all the past few years.</p>
<p>I missed a lot.</p>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/jquerylogo_png.png"><img style="display:inline;border-width:0;" title="jquery-logo_png" border="0" alt="jquery-logo_png" src="http://complextosimple.files.wordpress.com/2010/07/jquerylogo_png_thumb.png?w=302&#038;h=302" width="302" height="302" /></a> </p>
<h2></h2>
<h2>Goodbye type safety</h2>
<p>This part scares me a little bit.&#160; I already made a few typos that were pretty hard to detect.</p>
<p>Debugging javascript is not much fun considering that data and methods are both first class citizens.</p>
<p>But, the trade-off just might be worth it.</p>
<p>What we are losing in type safety, we are making up for in productivity and speed.&#160; It is really fast to manipulate a page using JQuery and to hook up events.</p>
<p>I still wish there was some way to have some sort of type safety when using JQuery or Javascript in general, but it really hurts my brain to try and think about how that would be possible.</p>
<p>Perhaps some kind of <a href="http://projects.nikhilk.net/ScriptSharp">Script#</a> / JQuery bastard child?</p>
<p>I know some people are doing Javascript unit testing.&#160; That would certainly make me feel safer not having type safety.</p>
<h2></h2>
<h2>Functional thinking</h2>
<p>I like the functional thinking paradigm.&#160; One of my <a href="http://trycatchfail.com/blog/">co-workers</a> was saying that he likes JQuery because it helps him to write Lamba expressions and LINQ queries.&#160; I tend to agree with him.</p>
<p>I’ve said it before, but I believe the next high abstraction of programming is thinking functionally.</p>
<p>So many pieces of code that I would have used code generation to produce in the past, I can now produce with Lambda expressions.</p>
<h2>It’s not all roses</h2>
<p>I do have some major issues with JQuery and Javascript in general.&#160; One of the big problems is that JQuery is not a language, and it’s not a library.&#160; It is a DSL built on top of a language.</p>
<p><strong>Bleeding all the way through the stack</strong></p>
<p>The downfall of this is that it is a very leaky abstraction.&#160; Abstractions are good because they allow us to think at a higher level.</p>
<p>Leaky abstractions are bad because they force us to context switch from a higher level of thinking down to a lower level.</p>
<p>In order to understand JQuery you must understand Javascript, the DOM and CSS to some extent.</p>
<p>Think for a second about all the “languages” or technologies you have to know to develop an ASP.NET web page using JQuery and MVC.</p>
<ul>
<li>ASP.NET MVC </li>
<li>HTML </li>
<li>C# or Visual Basic </li>
<li>JSON </li>
<li>Javascript </li>
<li>JQuery </li>
<li>CSS </li>
<li>Web Services / Rest </li>
<li>HTTP (You need to be able to debug all the coolness you are flinging back and forth.) </li>
</ul>
<p>When you have it all working, it is pretty slick.&#160; Very slick indeed, but it is not a sustainable model.&#160; It is complex and sprinkled with errors and gotchas.</p>
<p><strong>Debugging / readability</strong></p>
<p>When I am writing JQuery, although I feel like it is magical and cool, I also feel a little bit dirty.</p>
<p>Why?</p>
<p>Well, as clean as you try to write JQuery code, it can be pretty nasty.&#160; There is a lot of magic going on, and it is not very easily understandable.</p>
<p>Perhaps I’ll get better at expressing my intent clearly in JQuery code, but I am pretty sure it has to due in part with the leakiness of the abstraction layer, and that can’t really be cleaned up easily.</p>
<p>Debugging is also hell.&#160; I tried debugging through the validation library to see why my error messages weren’t showing up, and it was no picnic.</p>
<p>Javascript itself is notoriously difficult to debug, because objects can have methods dynamically added to them, and all the other little tricks, make it hard to look at a watch window and get anything useful out of an object.</p>
<h2>View logic</h2>
<p>One of the things I really enjoyed while writing JQuery code was putting the view logic where it really belongs.&#160; It is nice to have a language and a server separating the view logic from the model and presentation.</p>
<p>JQuery really allows you to put all the code that manipulates the view in one place, and on the client side, where it really belongs.</p>
<p>JQuery will make your HTML code so much cleaner and helps to actually plug the leaks in the HTML abstraction layer.&#160; (Although it isn’t perfect, especially when working with ASP.NET Web Forms.)</p>
<h2></h2>
<h2>Better late then never at all</h2>
<p>So I know I’m really late to the JQuery party, but I’m here now, and I am liking it.</p>
<p>Overall, I’d recommend taking a look at JQuery if you are like me and hadn’t really used it until now.&#160; It definitely is a valuable skill to develop and is growing at an <a href="http://trends.builtwith.com/javascript/JQuery">extremely rapid pace</a>.</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.&#160; Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/999/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/999/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/999/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/999/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/999/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/999/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/999/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/999/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/999/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/999/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=999&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/14/late-to-the-jquery-party/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/jquerylogo_png_thumb.png" medium="image">
			<media:title type="html">jquery-logo_png</media:title>
		</media:content>
	</item>
		<item>
		<title>Why Small is Better</title>
		<link>http://simpleprogrammer.com/2010/07/09/why-small-is-better/</link>
		<comments>http://simpleprogrammer.com/2010/07/09/why-small-is-better/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 20:48:35 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Process Improvement]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=993</guid>
		<description><![CDATA[I’ve talked about large backlogs being fatlogs here and here before, but I’d like to apply that general thinking to more than just backlogs, and talk about why I think small is almost always the right choice. We can apply the principles I am about to describe in a variety of contexts. Determining how to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=993&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I’ve talked about large backlogs being fatlogs <a href="http://simpleprogrammer.com/2010/01/26/scrum-backlogs-that-cover-too-much-and-are-not-thinly-sliced-enough-and-have-spreadsheets-attached-and-have-non-specific-things-like-fix-everything-on-this-page/">here</a> and <a href="http://simpleprogrammer.com/2010/03/09/return-of-the-fatlog/">here</a> before, but I’d like to apply that general thinking to more than just backlogs, and talk about why I think small is almost always the right choice.</p>
<p>We can apply the principles I am about to describe in a variety of contexts.</p>
<ul>
<li>Determining how to best slice up a fatlog</li>
<li>Adding tasks to a backlog</li>
<li>Refactoring or writing methods</li>
<li>Creating database tables</li>
<li>Designing a UI</li>
<li>Designing an API</li>
</ul>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/small_car.jpg"><img style="display:inline;border:0;" title="small_car" src="http://complextosimple.files.wordpress.com/2010/07/small_car_thumb.jpg?w=467&#038;h=336" border="0" alt="small_car" width="467" height="336" /></a></p>
<h2>Small = Simple</h2>
<p>Software development, or rather software craftsmanship, is about managing complexity.  It always has been, it always will be.</p>
<p>Small things are typically more simple than large things.  Constraining size forces us to constrain complexity.  This is good.</p>
<p>We can built very large seeming complex things out of simple blocks.</p>
<p>Consider the reason why we break up our code into methods instead of writing it all in the main block.</p>
<p>A simple design is the difference between having a 10 year head start on the mobile phone industry and creating a phone that everyone hates and only ends up using as a basic phone, versus releasing a phone and in a very short time changing the world.</p>
<h2>Small is easy to understand</h2>
<p>This reason is essentially the same as simple, but is worth its own treatise, because it is so important.</p>
<p>Having a unit of work that is easy to understand greatly increases the chances that it will be:</p>
<ul>
<li>Done right</li>
<li>Done at all</li>
<li>Done quickly</li>
<li>Communicated and passed on correctly and easily</li>
</ul>
<p>Small things are easier to understand.  Almost any complex concept can be broken down to small enough pieces that it can be understood.</p>
<p>You will be amazed how much more work gets done when people understand the problem they are trying to solve.  Which brings me to the next topic.</p>
<h2>Problem admiration</h2>
<p>There is nothing… and I mean nothing, that will hamper productivity like problem admiration.  If someone thinks a problem is large and complex they will sit and admire that problem instead of working on it.  It is basic human nature.</p>
<p>Look at your own life.  Look at successful programs for life organization like <a href="http://www.amazon.com/gp/product/0142000280?ie=UTF8&amp;tag=makithecompsi-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0142000280">Getting Things Done</a>.</p>
<ul>
<li>Why aren’t you cleaning your garage?</li>
<li>Why aren’t you finishing up that degree you wanted to get?</li>
<li>Why aren’t you getting in shape?</li>
</ul>
<p>Most likely it is due in some part to problem admiration.</p>
<p>I’ll give you a hint to cleaning your garage.  Break the work up into 10 – 15 small tasks.  Watch how much easier it will be to accomplish that goal.  Give it a shot.</p>
<p>It is hard to admire a small problem.  Take big problems and make them small problems whether you are designing a database, cleaning your garage or slicing up a backlog item.  Small is better.</p>
<h2>Evolution</h2>
<p>Iterative development is about evolving software.  We evolve software because of the feedback loop.  Short feedback loops are good, because they allow us to adjust.</p>
<p>Have you ever had a really bad remote desktop connection?  What happened to your mouse when you moved quickly in one direction?</p>
<p>Makes it hard to control doesn’t it?</p>
<p>The smaller the piece of software you build, the shorter the feedback loop is.  If you want to control the evolution of your software, make the feedback loop short.  If you want to make the feedback loop short, make the slices small.</p>
<h2>Estimation</h2>
<p>Want to know how long it will take me to write code to reverse a string?</p>
<ul>
<li>About 5 minutes.</li>
</ul>
<p>How about how long it will take me to write this blog post?</p>
<ul>
<li>About 30 to 45 minutes.</li>
</ul>
<p>How about how long it will take me to build a dog house?</p>
<ul>
<li>I don’t know.  About a day maybe.</li>
</ul>
<p>How about how long it will take me to write a book?</p>
<ul>
<li>I have no idea, but if you make me guess I would say 5 months give or take 5 months.</li>
</ul>
<p>Excuse the crappiness of my examples, but I hope you can see my point.  The smaller the item is, the easier it is to estimate the time it will take to complete.</p>
<p>If you have been working in software development for any length of time you have probably come to the realization that tasks estimated to take an hour usually take an hour, but tasks that are estimated to take one day rarely actually take one day and sometimes end up taking a whole week.</p>
<p>Think about a more practical example.  Can you show me with your hands how big 1 foot is?  Okay, now look outside and eyeball 500 feet.</p>
<h2>Progress</h2>
<p>There is no better motivator than making progress.</p>
<p>This benefit is purely emotional and artificial, but we are emotional artificially inspired beings.</p>
<p>Everyone loves making progress.  If you break things down small, you can chart your progress and see that you will indeed finish.</p>
<p>Imagine a progress bar with 2 big blocks.  Wouldn’t be very inspiring would it?</p>
<h5>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.  Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h5>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/993/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/993/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/993/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/993/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/993/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=993&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/09/why-small-is-better/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/small_car_thumb.jpg" medium="image">
			<media:title type="html">small_car</media:title>
		</media:content>
	</item>
		<item>
		<title>Book Review: Scrumban</title>
		<link>http://simpleprogrammer.com/2010/07/07/book-review-scrumban/</link>
		<comments>http://simpleprogrammer.com/2010/07/07/book-review-scrumban/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 18:09:07 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Kanban]]></category>
		<category><![CDATA[Process Improvement]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=987</guid>
		<description><![CDATA[I just finished reading “Scrumban” by Corey Ladas. This book is a self-published book, which is really a collection of essays.  Be prepared for that before you read the book, so that you know what to expect. It is not a book that tells you how to exactly do Kanban or Scrumban, but it gives [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=987&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I just finished reading “<a href="http://www.amazon.com/gp/product/0578002140?ie=UTF8&amp;tag=makithecompsi-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0578002140">Scrumban</a>” by Corey Ladas.</p>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/scrumban.jpg"><img style="display:inline;border:0;" title="scrumban" src="http://complextosimple.files.wordpress.com/2010/07/scrumban_thumb.jpg?w=439&#038;h=439" border="0" alt="scrumban" width="439" height="439" /></a></p>
<p>This book is a self-published book, which is really a collection of essays.  Be prepared for that before you read the book, so that you know what to expect.</p>
<p>It is not a book that tells you how to exactly do Kanban or Scrumban, but it gives you ideas about Kanban and lean concepts and how to solve certain problems you might face in implementing a Kanban process.</p>
<p>Some parts of the book might be a bit difficult to understand without a background in lean methodology, but overall most of the common problems and solutions are understandable.  This is definitely a book you could study and contemplate on as you read through the material and look at the charts.</p>
<p><strong>Good:</strong></p>
<ul>
<li>Excellent use of diagrams and pictures of real Kanban boards to illustrate points.</li>
<li>Lots of different solutions for common Kanban problems.</li>
<li>In depth talk about strategy and reason behind different possible solutions.</li>
<li>Several unique perspectives on many of the concepts presented.</li>
</ul>
<p><strong>Bad:</strong></p>
<ul>
<li>Not exactly an easy read.  It is a little rough and some of the concepts rely on knowledge outside of the context of the book.</li>
<li>Some of the assumptions are a little broad and aren’t backup up by much real data.</li>
</ul>
<p><strong>What I learned:</strong></p>
<p>This book really expanded my mind about different ways of implementing Kanban and solutions for some of the common problems.  Some of the solutions surprised me, but made sense.</p>
<p>I learned more about the reasoning behind limiting the WIP to force the system to self-correct.  I had not considered that strictly limiting the WIP would force freed up resources to automatically move to help resolve the clog in the pipe, since they will not be able to take in new work.</p>
<p>Corey presented a great technique on planning by having a just-in-time planning session when the number of items in development reaches a certain threshold.  I had heard of this technique before but had not seen a practical implementation of it.</p>
<p>I would definitely recommend this book to expand your thinking on Kanban software development.  If you are looking for instructions on exactly how to set up your Kanban process, this book probably won’t help you, but if you are looking to understand more about the why of Kanban and how to solve particular problem or pain points, get this book.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/987/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/987/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/987/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/987/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/987/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/987/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/987/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/987/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/987/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/987/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=987&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/07/book-review-scrumban/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/scrumban_thumb.jpg" medium="image">
			<media:title type="html">scrumban</media:title>
		</media:content>
	</item>
		<item>
		<title>The Importance of Always Reading</title>
		<link>http://simpleprogrammer.com/2010/07/02/the-importance-of-always-reading/</link>
		<comments>http://simpleprogrammer.com/2010/07/02/the-importance-of-always-reading/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 22:33:21 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Career]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[Self Improvement]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=982</guid>
		<description><![CDATA[It has been a while since I wrote a book review. There is one coming soon, but I wanted to talk about why it has been so long. Simple answer… I stopped reading. What happened?  Well, I got caught up with switching jobs twice.  Lots of different things going on.  I started a running program, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=982&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>It has been a while since I wrote a book review.</p>
<p>There is one coming soon, but I wanted to talk about why it has been so long.</p>
<p>Simple answer… I stopped reading.</p>
<p>What happened?  Well, I got caught up with switching jobs twice.  Lots of different things going on.  I started a running program, etc… etc… excuse… excuse.</p>
<p>I wasn’t following <a href="http://simpleprogrammer.com/2009/12/08/great-developers-are-librarians/">my own advice</a>.</p>
<p>It is really important to take a moment every once in a while, when you feel like you are off track or not reaching your goals, to reassess your priorities and goals and figure out what you need to get back on track.</p>
<p><a href="http://complextosimple.files.wordpress.com/2010/07/codeincomplete.jpg"><img style="display:inline;border-width:0;" title="CodeIncomplete" src="http://complextosimple.files.wordpress.com/2010/07/codeincomplete_thumb.jpg?w=420&#038;h=545" border="0" alt="CodeIncomplete" width="420" height="545" /></a></p>
<h2>You have to set a schedule</h2>
<p>That is primarily where I failed, and that is how I am succeeding now again.  At one point I was reading through books about 2 a month, but pretty soon a few months passed by without one book read.</p>
<p>When I was successful at reading, it was because I had set up a schedule or reading every day for so much time.</p>
<p>The simple matter of it is, as humans we are not good at <a href="http://simpleprogrammer.com/2010/05/07/drinking-from-the-firehose/">making good choices about priorities</a> without explicitly planning them out.</p>
<p>Now that I have set up a schedule of reading a minimum of 30 minutes each day, I am making progress again, and much faster than I thought.</p>
<h2>Aren’t you being a little strict?</h2>
<p>You may think that just reading blogs is enough to keep up with the information stream, and it may be, but it probably isn’t going to really expand your mind like a book will.</p>
<p>Why I recommend reading technical books (although, I would argue a book on people skills and other self-improvement books should also be mixed in.)</p>
<h4>Breadth of knowledge will increase.</h4>
<p>You will learn more about things and understand more of what is available to you even if you do not employ all the practices and technologies in the books you read.</p>
<h4>Depth of knowledge.</h4>
<p>Sometimes drilling down into an API or deep into a technique can help you truly master a technology or practice way more than hacking out code or consulting APIs to just find what you need to know.</p>
<h4>Motivation.</h4>
<p>This one is a surprise to most people, but what you read is what you are excited about, not the other way around.</p>
<p>Try it if you don’t believe me.  Pick any topic and start reading about it.  You will become passionate about that topic.</p>
<p>If you want to be a better software developer, read about software development.</p>
<h4>Reputable source of authority when influencing decisions.</h4>
<p>You can have the best idea in the world, but if your only authority is yourself, it doesn’t hold much credibility.  It is much more effective to say, “I think we should do it this way.  In <a href="http://www.amazon.com/gp/product/0735619670?ie=UTF8&amp;tag=makithecompsi-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0735619670">Code Complete</a>, Steve McConnell says…”</p>
<h4>Unconscious knowledge absorption.</h4>
<p>I have always been amazed by how much information I picked up from reading than what I was conscious of knowing.</p>
<p>Many of my blog posts will echo things I have read in <a href="http://www.amazon.com/gp/product/0735619670?ie=UTF8&amp;tag=makithecompsi-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0735619670">Code Complete</a>, or <a href="http://www.amazon.com/gp/product/0132350882?ie=UTF8&amp;tag=makithecompsi-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code</a>, or any number of other books I have read, which I do not even consciously recall.</p>
<p>Sometimes when I write code, I intrinsically know it is the right way to do it, because of an unconscious conditioning.  (Make sure you read good books, or you will have the negative aspect of this effect.)</p>
<h2>My little tip</h2>
<p>One great way to get your reading done, and to stay in shape at the same time, is to do it on a treadmill.</p>
<p>I have been doing this for awhile now, and it really makes me feel like I am using my time wisely.</p>
<p>You can just walk on the treadmill, set a little incline to burn some extra calories, and after awhile you’ll forget you are walking.</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.  Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/982/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/982/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/982/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/982/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/982/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/982/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/982/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/982/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/982/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/982/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=982&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/07/02/the-importance-of-always-reading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/07/codeincomplete_thumb.jpg" medium="image">
			<media:title type="html">CodeIncomplete</media:title>
		</media:content>
	</item>
		<item>
		<title>Parsing Columns Like A Ninja</title>
		<link>http://simpleprogrammer.com/2010/06/30/parsing-columns-like-a-ninja/</link>
		<comments>http://simpleprogrammer.com/2010/06/30/parsing-columns-like-a-ninja/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 14:58:17 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=975</guid>
		<description><![CDATA[How many times have you written this code? : public void CreateFromLine(string line) { var columns = line.split(','); this.Name = StripQuotes(columns[0]); this.Description = StripQuotes(columns[1]); this.Category = StripQuotes(columns[2]); this.Stuff = StripQuotes(columns[3]); this.MoreStuff = StripQuotes(columns[4]); if (columns.Count &#62; 5) this.EvenMoreStuff = StripQuotes(columns[5]); } Or some code like it.  It is pretty common to parse a line and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=975&subd=complextosimple&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>How many times have you written this code? :</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:c825b008-c502-408c-a841-f8487ab26076" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: csharp; pad-line-numbers: true;">
public void CreateFromLine(string line)
{
    var columns = line.split(',');

    this.Name = StripQuotes(columns[0]);
    this.Description = StripQuotes(columns[1]);
    this.Category = StripQuotes(columns[2]);
    this.Stuff = StripQuotes(columns[3]);
    this.MoreStuff = StripQuotes(columns[4]);
    if (columns.Count &gt; 5)
        this.EvenMoreStuff = StripQuotes(columns[5]);
}
</pre>
</div>
<p>Or some code like it.  It is pretty common to parse a line and then take each column and store it in your object as data.</p>
<p>One of the annoying problems is that if you have optional columns you have to check to see if they are there before you can parse them.</p>
<p>You’re also repeating your code to strip the quotes off, or whatever other preprocessing you are doing, all over the place.</p>
<p>I know you can use a data driven approach to specify column to property mappings, but I wanted a really low tech, simple solution.</p>
<p>I finally came up with one using one of my favorite C# constructs.</p>
<h2>Action&lt;&gt; is superb for solving these kinds of problems</h2>
<p>See if this code makes you feel any better:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:caa076af-a3dd-444d-a5ca-934a35961566" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: csharp;">
var propertySetters = new List&lt;Action&lt;string&gt;&gt;
{
    value =&gt; this.Name = value,
    value =&gt; this.Description = value,
    value =&gt; this.Category = value,
    value =&gt; this.Stuff = value,
    value =&gt; this.MoreStuff = value,
    value =&gt; this.EvenMoreStuff = value
};

var columns = line.split(',');

foreach (int columnNumber = 0; columnNumber &lt; columns.Count; columnNumber++)
{
    var propertySetter = propertySetters[columnNumber];
    propertySetter(StripQuotes(columns[columnNumber]);
}
</pre>
</div>
<p>It may not seem like much.  It is not really a reduction in code, but we have done a few important things here.</p>
<ul>
<li>Removed the explicit handling of optional columns, since we are now only populating columns that exist.  (Adding a new optional column is as easy as adding one more line to the list.)</li>
<li>Removed the responsibility from the code of explicitly tracking the column numbers.  Column number mapping now is implicit by the order of the columns in the list.</li>
<li>Removed the hidden code duplication of having calls to StripQuotes repeated for each column.</li>
<li>Separated the mapping of properties to columns from the assignment of them.</li>
</ul>
<p>That last point deserves a little more explanation.  Why do we care if we have separated the mapping of properties to columns from the assignment?</p>
<p>The answer is not obvious until you try and use this same code to handle a different set of columns, or columns in a different order.</p>
<p>By separating out the mapping, we can pass the assignment code a different set of mappings, and it will still work.</p>
<p>This allows us to reuse the logic we have in the assignment of the columns to properties instead of rewriting it for other column to property mappings or orderings.</p>
<p><strong>As always, you can subscribe to this </strong><a href="http://feeds.feedburner.com/MakingTheComplexSimple"><strong>RSS feed</strong></a><strong> to follow my posts on Making the Complex Simple.  Feel free to check out </strong><a href="http://elegantcode.com/"><strong>ElegantCode.com</strong></a><strong> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter </strong><a href="http://twitter.com/jsonmez"><strong>here</strong></a><strong>.</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/975/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/975/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/975/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&blog=10597120&post=975&subd=complextosimple&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/06/30/parsing-columns-like-a-ninja/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>
	</item>
	</channel>
</rss>