<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ketan&#039;s Musings &#187; programming</title>
	<atom:link href="http://ketan.padegaonkar.name/tag/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://ketan.padegaonkar.name</link>
	<description>Where he blogs about his eclipse musings</description>
	<lastBuildDate>Fri, 23 Jul 2010 16:03:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Tell, Don&#8217;t Ask &#8211; Part 2</title>
		<link>http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-2.html</link>
		<comments>http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-2.html#comments</comments>
		<pubDate>Fri, 04 Sep 2009 02:46:42 +0000</pubDate>
		<dc:creator>Ketan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[clean-code]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ketan.padegaonkar.name/?p=404</guid>
		<description><![CDATA[Objects exposing behavior, not state Controlling complexity of your codebase by limiting what state your objects expose The more objects that can see and change states on other objects, the more complex your system. Objects returning a boolean mean that someone calling that method will use an if branch, returning an integer would mean someone [...]]]></description>
			<content:encoded><![CDATA[<h2>Objects exposing behavior, not state</h2>
<h3>Controlling complexity of your codebase by limiting what state your objects expose</h3>
<p>The more objects that can see and change states on other objects, the more complex your system. Objects returning a boolean mean that someone calling that method will use an if branch, returning an integer would mean someone using if/else or switch/case. Returning objects would mean introspeting that returned object to invoke something else on it. This increases coupling between classes, makes code hard to read and test.</p>
<h3>My class has 3 friends, I talk to my friends&#8217; friends. My friends are difficult to mock, therefore mocking sucks&#8230;</h3>
<p>&#8230; well, yeah!</p>
<p>Testing procedural code is hard. Testing such code generally involves setting up &#8220;data&#8221; and asserting on state of objects. <a href="http://www.c2.com/cgi/wiki?TellDontAsk">Tell Don&#8217;t Ask</a> code on the other hand is easier to test since you&#8217;re not testing state. Also notice how DI makes things simpler to test.</p>
<pre>
void testOwnerCanFeedDog(){
    Dog dog = new Dog();
    // have to create a mouth since owner calls dog.getMouth() to feed it
    Mouth mouth = new Mouth();
    dog.setMouth(mouth);
    PetOwner owner = new PetOwner();
    owner.setDog(dog);
    owner.feedDog(food);

    // verify that the dog gets the food (well the mouth, actually)
    assertEquals(food, mouth.getFood());
}
</pre>
<pre>
void testOwnerCanFeedDog(){
    Dog dog = mock(Dog.class);
    PetOwner owner = new PetOwner(dog);
    owner.feed(food);

    // verify that the dog gets the food
    verify(dog).feed(food);
}
</pre>
<p>Without <a href="http://martinfowler.com/articles/injection.html">Dependency Injection</a>, testing is quite difficult; without <a href="http://www.c2.com/cgi/wiki?TellDontAsk">Tell Don&#8217;t Ask</a>, testing is almost always impossible. Put together, things are separated, testing is simplified.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-2.html&amp;title=Tell%2C+Don%27t+Ask+-+Part+2" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-2.html&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-2.html&amp;title=Tell%2C+Don%27t+Ask+-+Part+2" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=RT+%40ketanpkr+Tell%2C+Don%27t+Ask+-+Part+2+-+http://bit.ly/90HIZs&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-2.html&amp;t=Tell%2C+Don%27t+Ask+-+Part+2" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-2.html&amp;title=Tell%2C+Don%27t+Ask+-+Part+2&amp;description=Objects%20exposing%20behavior%2C%20not%20state%0D%0A%0D%0AControlling%20complexity%20of%20your%20codebase%20by%20limiting%20what%20state%20your%20objects%20expose%0D%0A%0D%0AThe%20more%20objects%20that%20can%20see%20and%20change%20states%20on%20other%20objects%2C%20the%20more%20complex%20your%20system.%20Objects%20returning%20a%20boolean%20mean%20that%20someone%20calling%20that%20method%20will%20use%20an%20" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-2.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Tell, Don&#8217;t Ask &#8211; Part 1</title>
		<link>http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-1.html</link>
		<comments>http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-1.html#comments</comments>
		<pubDate>Fri, 04 Sep 2009 02:40:41 +0000</pubDate>
		<dc:creator>Ketan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[clean-code]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://ketan.padegaonkar.name/?p=371</guid>
		<description><![CDATA[I spend more time reading code than writing it. I therefore like code that is readable. Rarely do I like to read code that is verbose and does too much orchestration in order to do something that is orthognal to what I&#8217;m looking for. Code is easier to read and maintain when objects are written [...]]]></description>
			<content:encoded><![CDATA[<p>I spend more time reading code than writing it. I therefore like code that is readable. Rarely do I like to read code that is verbose and does too much orchestration in order to do something that is orthognal to what I&#8217;m looking for.</p>
<p>Code is easier to read and maintain when objects are written in a <a href="http://www.c2.com/cgi/wiki?TellDontAsk">Tell Don&#8217;t Ask</a>.</p>
<p>&#8220;Tell, Don&#8217;t Ask&#8221; is a style of programming where anObject tells anotherObject to doSomething(), rather than asking anotherObject to getSomeValue() and then makeADecision().</p>
<p>Code that does violates this this is more procedural than it is object oriented. In the procedural world code is written to fetch some data (or state) and then make a decision or perform some action. Procedural programming &#8220;pulls data&#8221; into the logic to get things done.</p>
<p>In object oriented programming, we do the opposite &#8212; have objects do something for you instead of you doing it yourself. Don&#8217;t overdo this too much, someone still has to do the real work though <img src='http://ketan.padegaonkar.name/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Identifying places where you may tell instead of ask:</p>
<pre>
class PetOwner{
    void feedDog(Food food){
        if(getDog().isHungry()){
            dog.getMouth().putFood(food);
        }
    }
}
</pre>
<p>This can instead be written as:</p>
<pre>
class PetOwner{
    void feedDog(Food food){
        dog.feed(food);
    }
}

class Dog{
    void feed(Food food){
        if (iAmHungry()){
            // consume food
        }
    }
}
</pre>
<p>Notice how the PetOwner does not know (or care to know) about the fact that the dog has a mouth.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-1.html&amp;title=Tell%2C+Don%27t+Ask+-+Part+1" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-1.html&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-1.html&amp;title=Tell%2C+Don%27t+Ask+-+Part+1" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=RT+%40ketanpkr+Tell%2C+Don%27t+Ask+-+Part+1+-+http://bit.ly/ddcIlO&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-1.html&amp;t=Tell%2C+Don%27t+Ask+-+Part+1" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-1.html&amp;title=Tell%2C+Don%27t+Ask+-+Part+1&amp;description=I%20spend%20more%20time%20reading%20code%20than%20writing%20it.%20I%20therefore%20like%20code%20that%20is%20readable.%20Rarely%20do%20I%20like%20to%20read%20code%20that%20is%20verbose%20and%20does%20too%20much%20orchestration%20in%20order%20to%20do%20something%20that%20is%20orthognal%20to%20what%20I%27m%20looking%20for.%0D%0A%0D%0ACode%20is%20easier%20to%20read%20and%20maintain%20when%20objects%20are%20written%20in" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://ketan.padegaonkar.name/2009/09/04/tell-dont-ask-part-1.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
