<?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>Learning by Experience &#187; refactoring</title> <atom:link href="http://www.inze.be/andries/category/refactoring/feed/" rel="self" type="application/rss+xml" /><link>http://www.inze.be/andries</link> <description>Java, Project Management, Life and anything else.</description> <lastBuildDate>Mon, 09 Jan 2012 21:38:00 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Extract Method: an in-depth motivation</title><link>http://www.inze.be/andries/2007/10/18/extract-method-an-in-depth-motivation/</link> <comments>http://www.inze.be/andries/2007/10/18/extract-method-an-in-depth-motivation/#comments</comments> <pubDate>Thu, 18 Oct 2007 21:11:02 +0000</pubDate> <dc:creator>Andries Inzé</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[refactoring]]></category> <guid
isPermaLink="false">http://www.inze.be/andries/?p=16</guid> <description><![CDATA[Extract method is one of the easiest refactorings within the GoF patterns. Developers tend to look at large methods and use Extract Method only then. Well, there is a lot more to this simple refactoring then just splitting up large methods. Let&#8217;s digg in! Separation of responsibility Methods should have 1 concern (responsibility, purpose, &#8230; [...]]]></description> <content:encoded><![CDATA[<p>Extract method is one of the easiest refactorings within the <a
href="http://en.wikipedia.org/wiki/Design_Patterns" title="GoF" target="_blank">GoF </a>patterns. Developers tend to look at large methods and use <em>Extract Method</em> only then. Well, there is a lot more to this simple refactoring then just splitting up large methods. Let&#8217;s digg in!</p><h3>Separation of responsibility</h3><p>Methods should have 1 concern (responsibility, purpose, &#8230; ). Let&#8217;s look into the matter with an example. Consider following code:</p><pre>public void createCustomer(Map requestParameters) {
	Customer customer = new Customer();
	customer.setName = requestParameters.get("name");</pre><pre>	//Check if a customer was already registered with that name
	if (customerService.getCustomerByName(customer.getName()) != null) {
		System.out.println("Customer already exists");
		return;
	}
	customer.setShoppingCart(new ShoppingCart());</pre><pre>	customerService.save(customer);</pre><pre>}</pre><p>The above code is something I scraped together, it&#8217;s not real code. But it illustrates something I see regular. The method name is <em>save</em>. So I expect that the code only does some saving. Instead it creates a new customer, checks if it&#8217;s valid (no double names) and then saves it. So the method is actually performing 3 things.</p><p>This method should be refactored into 4 methods. The first method, which I would call <em>bindValidateAndSave</em> is the <em>algorithm </em>method. It tells what to do, not how it&#8217;s done. Its responsibility is to provide the algorithm. The 3 other methods would be called <em>bindCustomer </em>(bind and add new shoppingCart), <em>validateCustomer</em> and <em>saveCustomer</em>.</p><p>With the last method (<em>saveCustomer</em>) I get the <em>WHY?</em> question a lot, since the extracted method is actually only 1 line of code long. Although it might not improve readability for programmers, it&#8217;s a paradigm shift in how the method is addressed. In our example, if the <em>saveNewCustomer</em> calls customerService.save() it&#8217;s responsible that the save method is actually called right. Instead if we let it delegate to a newly extracted method (<em>saveCustomer) </em>it isn&#8217;t responsible for the explicit saving. Instead now it defines the algorithm. In this case bind, validate, save. The implementation is just the save method.</p><p>Working in this fashion takes some getting used to, but in the end I truly believe this produces stabler and more maintainable code.</p><h3>Self-describing methods</h3><p>The second insight I have about the <em>Extract Method</em> refactoring, is removing comments from my code. A lot of fresh developers believe writing comments is the key to great software. As Martin Fowler stated in his excellent book Refactoring, comments are really a bad smell that something is wrong. Either your code is too complex or too long winded. Extracting code just to give it a nice method name helps readability. Take for instance the following example:</p><pre>public void eat(Food food)  {
	//Other eat actions...</pre><pre>	//Need to blow on the food cause it might be hot
	while (food.getTemp() &gt; 50) {
		mouth.blow();
	}
}</pre><p>Although the example is completely stupid, it would be slightly less stupid if written like this:</p><pre>public void eat(Food food)  {
	//Other eat actions...
	blowWhileHot(food);
	mouth.eat(food);</pre><pre>}
private void blowWhileHot(Food food) {
	while (food.getTemp() &gt; 50) {
		mouth.blow();
	}
}</pre><p>Et voila, our code just became A LOT more readable.</p><p>I agree, both examples are intertwined  in most cases, but there is a difference between the two.</p><p>Both insights are developed by reading <a
href="http://www.refactoring.com" title="Refactoring" target="_blank">Refactoring</a>, a book that definitely made it into my top 10 list!</p><p>Got any comments, let&#8217;s hear them!</p><p>Andries</p> ]]></content:encoded> <wfw:commentRss>http://www.inze.be/andries/2007/10/18/extract-method-an-in-depth-motivation/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
