<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Refactoring Exercise</title>
    <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Refactoring Exercise</title>
      <description>&lt;p&gt;So I&amp;#8217;ve written a somewhat ugly method with the intent of having people clean it up. Want to give it a try? Post your results and then I&amp;#8217;ll show what I ended up doing in a few days (or after the activity dies down).&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s in Java, but feel free to post in other languages. I&amp;#8217;d be interested in seeing something in whatever language Michael Feathers happens to be deeply studying right now!-)&lt;/p&gt;


	&lt;p&gt;Oh, and if you feel like going &amp;#8220;overboard&amp;#8221; and using a few design patterns just because you can, I&amp;#8217;d like to see that as well.&lt;/p&gt;


	&lt;p&gt;Here it is:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_java "&gt;  public void take(int v) {
    if (currentMode == Mode.accumulating) {
      int digits = (int)Math.pow(10, (int)Math.log10(v) + 1);
      int x = stack.pop();
      x *= digits;
      x += v;
      stack.push(x);
    }

    if (currentMode == Mode.replacing) {
      stack.pop();
      stack.push(v);
      currentMode = Mode.accumulating;
    }

    if (currentMode == Mode.inserting) {
      int top = stack.peek();
      stack.push(top);
      stack.push(v);
      currentMode = Mode.accumulating;
    }
  }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 04 Jun 2009 21:23:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ce8dfe6e-cd31-4b42-acee-f98327c0bd2a</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>refactoring</category>
      <category>exercise</category>
    </item>
    <item>
      <title>"Refactoring Exercise" by mlb jerseys sale</title>
      <description>&lt;p&gt;&lt;a href="http://www.superstonejerseys.com" rel="nofollow"&gt;http://www.superstonejerseys.com&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 22 Jul 2010 04:42:53 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:59a40f37-11ed-4235-a788-b68e902c15c4</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-17106</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Chris</title>
      <description>&lt;p&gt;@Brett&lt;/p&gt;


	&lt;p&gt;I think perhaps we are talking at cross-purposes. I am not objecting to the name &amp;#8220;enter&amp;#8221; in itself, nor to the subtle distinction between &amp;#8220;pressing enter&amp;#8221; and &amp;#8220;completing an input&amp;#8221;. I am objecting to the use of &lt;em&gt;any&lt;/em&gt; interface-level concepts in the domain model.&lt;/p&gt;


	&lt;p&gt;In a RPN calculator, the domain interface requires three basic operations: putting in a number, putting in an operation, and reading out the current value.&lt;/p&gt;


	&lt;p&gt;Internally, of course, it maintains some sort of stack, but the state of that stack is evident only through operations that implicitly change the values at the top of the stack and through reading the current top value.&lt;/p&gt;


	&lt;p&gt;Similarly, there is some logic associated with knowing when a sequence of digits typed by the user is a new number ready to put onto the stack, but this matters only to the UI code. We can see that things like typing an individual digit or pushing an enter button are not in themselves meaningful concepts in the domain, because they make a difference only when they result in pushing a new number onto the stack.&lt;/p&gt;


	&lt;p&gt;This structure would have been immediately obvious had there been separation between the domain model (probably a class with only three public methods plus constructor) and the UI code (with whatever state machine might be appropriate to handle the individual key presses). But because in Uncle Bob&amp;#8217;s version there is just the one huge Register class trying to do everything with many tiny methods, there isn&amp;#8217;t even this basic separation of concerns and the levels of abstraction are all mixed up.&lt;/p&gt;</description>
      <pubDate>Wed, 01 Jul 2009 17:30:10 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:0f8cd5fb-76cc-4102-8f0c-9be663e6636d</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3643</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Andrei Vajna</title>
      <description>&lt;p&gt;I took a quick look to the current solutions, and it seems mine is veeery close to Peter Bona&amp;#8217;s.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
abstract class Mode {
  public abstract Mode void take(Stack stack, int v);

  public static final Mode accumulating = new Mode() {
    private int calculatePowerOfTen(int v) {
      return (int)Math.pow(10, (int)Math.log10(v) + 1);
    }

    Mode void take(Stack stack, int v) {        
      int top = stack.pop();  
      stack.push(top * calculatePowerOfTen(v) + v);
      // i chose to use the temp because it's more explanatory this way
      // and because pop() has side effects and i don't want to include it
      // in an expression
      // but i could have easily written
      // stack.push(stack.pop() * calculatePowerOfTen(v) + v);
      // it would be the same regarding functionality
      // but i feel it's more readable this way
      return this;
    }
  };

  public static final Mode replacing = new Mode() {
    Mode void take(Stack stack, int v) {
      stack.pop();
      stack.push(v);
      return Mode.accumulating;
    }
  };

  public static final Mode inserting = new Mode() {
    Mode void take(Stack stack, int v) {
      stack.push(stack.peek());
      stack.push(v);
      return Mode.accumulating;
    }
  };
}
&lt;/code&gt; 
&lt;/pre&gt;

	&lt;p&gt;The originial take method becomes:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
public void take(int v) {
  currentMode = currentMode.take(stack, v);
}
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Mode is an implementation of the State pattern. I preffered your initial use of constans and also, I used annonymous classes for conciseness. You could as well have used proper classes, subclassed from Mode base class.&lt;/p&gt;


	&lt;p&gt;The difference from Peter&amp;#8217;s solution are little readability refactorings, like eliminating on of the temps in the accumulating mode with a method.&lt;/p&gt;


	&lt;p&gt;Two points I&amp;#8217;d like to make to your problem: you didn&amp;#8217;t specify what the purpose of the refactoring is, so I could well say that the already implemented solution is fine as it is. And second, &amp;#8220;using a few design patterns just because you can&amp;#8221; is bad! :)&lt;/p&gt;</description>
      <pubDate>Tue, 23 Jun 2009 14:02:25 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:a9ee3d27-d9db-4ef4-a0fd-cf494b354805</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3619</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Luca Minudel</title>
      <description>&lt;p&gt;public void take(int v) {&lt;/p&gt;
	&lt;pre&gt;&lt;code&gt;currentMode.take(stack, v);
  currentMode = new CurrentModeAccumulating();&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;}&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;...&lt;/p&gt;


	&lt;p&gt;class CurrentModeAccumulating : ICurrentMode {...}&lt;/p&gt;


	&lt;p&gt;class CurrentModeReplacing : ICurrentMode {...}&lt;/p&gt;


	&lt;p&gt;class CurrentModeInserting : ICurrentMode {...}&lt;/p&gt;</description>
      <pubDate>Sun, 21 Jun 2009 12:19:41 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:2e28fab4-1026-4df0-b611-01c8f1d56eea</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3608</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Brett L. Schuchert</title>
      <description>&lt;p&gt;&lt;b&gt;Chris&lt;/b&gt; wrote:
&lt;blockquote&gt;
On the contrary, it is describing not what the function does but when it should be called in terms of specific user actions.
&lt;/blockquote&gt;&lt;/p&gt;


	&lt;p&gt;I think you are a bit off the mark. Ultimately, the underling facade supporting some user interface needs to have the ability to represent logical operations from the perspective of the user.&lt;/p&gt;


	&lt;p&gt;Sure, this is not always a 1-to-1 mapping (and in fact it should&lt;i&gt; &lt;b&gt;not&lt;/b&gt;&lt;/i&gt; be so most of the time). However, there is a difference between &amp;#8220;entering the digits of the number&amp;#8221; and &amp;#8220;starting the next number&amp;#8221;.&lt;/p&gt;


So some questions I might ask:
	&lt;ul&gt;
	&lt;li&gt;What are the logical interactions?&lt;/li&gt;
		&lt;li&gt;How many steps do I want to make explicit in the design of my facade layer?&lt;/li&gt;
		&lt;li&gt;How will one design impact multiple potential user interfaces?&lt;/li&gt;
		&lt;li&gt;Can that one facade server all, or is it easy enough to adapt to make it a reasonable abstraction?&lt;/li&gt;
		&lt;li&gt;Does the facade as designed preclude some kind of interaction?&lt;/li&gt;
		&lt;li&gt; ...&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Here is an example I came across on a project in 1997&amp;#8230;&lt;/p&gt;


	&lt;p&gt;The project allowed flight rebookings on Palm 7&amp;#8217;s, the web, WAP enabled phones and smart two-way pagers. It worked well, it was ready for use and it was in production (well it was in a beta sense).&lt;/p&gt;


	&lt;p&gt;Then along came a new 2-way pager that used SMTP as its communication between the pager and the base. The average response time was 1 minute. The minimum was 30 seconds and the max was 3+ minutes.&lt;/p&gt;


We had designed several logical steps to change a flight (end to end):
	&lt;ul&gt;
	&lt;li&gt;Go to a URL/bookmark&lt;/li&gt;
		&lt;li&gt;Log in&lt;/li&gt;
		&lt;li&gt;Review itineraries&lt;/li&gt;
		&lt;li&gt;Select itinerary to change&lt;/li&gt;
		&lt;li&gt;Indicate desired change (earlier/later)&lt;/li&gt;
		&lt;li&gt;Select one from a list of options&lt;/li&gt;
		&lt;li&gt;Accept change fees&lt;/li&gt;
		&lt;li&gt;Log out&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Ignoring logout, that&amp;#8217;s 7 steps. On this new device, that would, on average, require 7 minutes.&lt;/p&gt;


	&lt;p&gt;We had a conflicting requirement to make a system that was faster than making a phone call. 7 minutes was off the mark by several minutes.&lt;/p&gt;


	&lt;p&gt;So we had to adapt the underlying interaction. We left the underlying service as is but we developed an application for the two-way pager. This application maintained a cache of itineraries and credentials.&lt;/p&gt;


The new interaction was:
	&lt;ul&gt;
	&lt;li&gt;Start application (no time, no hit)&lt;/li&gt;
		&lt;li&gt;Select itinerary and indicate earlier or later booking (hit one)&lt;/li&gt;
		&lt;li&gt;Select from option&lt;/li&gt;
		&lt;li&gt;Accept change fees&lt;/li&gt;
	&lt;/ul&gt;


This was considered too long (three minutes), so we made it even simpler:
	&lt;ul&gt;
	&lt;li&gt;Select itinerary and provide a window (2 &amp;#8211; 4 hours earlier) &lt;/li&gt;
		&lt;li&gt;Auto select change fees if &amp;lt; $250&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So this made it fast enough, but it took away too much from the experience and was rejected. We actually had to bring the manufacturer on site to tell them there really were no other options.&lt;/p&gt;


	&lt;p&gt;Why do I bring all of this up at all?&lt;/p&gt;


	&lt;p&gt;When you are building a system, a part of its design is user interaction. Part of that is designing a system that accommodates all of the points of interaction that make the best possible user experience, removing was is not necessary (or relegating it to an alternative).&lt;/p&gt;


	&lt;p&gt;In this case, enter() is the name given for the logical idea of completing input on one number.&lt;/p&gt;


	&lt;p&gt;Can this be removed? Yes. Can a calculator designed without this interaction work with many different user interfaces? Yes (I&amp;#8217;ve done it). Does it map well to the domain? YES.&lt;/p&gt;


	&lt;p&gt;The HP calculators have been around since the late 60&amp;#8217;s (see &lt;a href="http://www.hpmuseum.org/" rel="nofollow"&gt;http://www.hpmuseum.org/&lt;/a&gt;


	&lt;p&gt;So to take it away in fact could make the underlying solution less close to the domain and in fact result in a worse solution.&lt;/p&gt;


	&lt;p&gt;One person&amp;#8217;s intention is another person&amp;#8217;s &amp;#8220;specific user actions&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;Discussing the &amp;#8220;enter&amp;#8221; function is taking a phenomenological perspective, removing it and discussing &amp;#8220;indication completion&amp;#8221; is moving towards the ontological.&lt;/p&gt;


	&lt;p&gt;Context dictates one approach as &amp;#8220;better&amp;#8221; than another &amp;#8211; at least for now.&lt;/p&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 16 Jun 2009 22:25:01 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ec97e996-f61b-4d68-bc70-2d3f46721d49</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3577</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Chris</title>
      <description>&lt;p&gt;@Philip Schwarz&lt;/p&gt;


	&lt;p&gt;The thing is, calling a function &amp;#8220;enter&amp;#8221; like that &lt;em&gt;isn&amp;#8217;t&lt;/em&gt; intention revealing. On the contrary, it is describing not what the function does but when it should be called in terms of specific user actions. There is a place for event handlers like that, but it&amp;#8217;s in your user interface modules. Why is any code in an internal class like this even aware of what the nature of the user interface is, never mind referring to specific user actions?&lt;/p&gt;


	&lt;p&gt;Again, there is a not-seeing-wood-for-trees problem here. If we were to expand the scope of the problem to include the user interactions - which is far beyond what the original function covered and not a mere refactoring, of course - then we should be creating an internal class to model the stack behaviour with interface methods representing domain concepts such as pushing a number onto the stack, applying an operator and showing the current top of the stack, and we should also have separate code to handle UI quirks like parsing single keystrokes and evaluating when they become triggers for certain meaningful actions in the problem domain.&lt;/p&gt;</description>
      <pubDate>Sun, 14 Jun 2009 17:46:43 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:41c7d302-bdce-4984-abf4-525800142bc7</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3566</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Chris&lt;/p&gt;


	&lt;p&gt;You said:&lt;/p&gt;


&lt;blockquote&gt;
I could immediately guess what isEmpty would mean from the calling code, and while it is only a single line long, it does represent a useful abstraction.

On the other hand, despite knowing the general problem area we&#8217;re discussing, &lt;b&gt;I still don&#8217;t see why the names take, dup, enter and accumulate are particularly helpful here. take seems rather arbitrary: neither it nor the name Register provide enough context for it to be a meaningful concept without further information&lt;/b&gt;. 
&lt;/blockquote&gt;

	&lt;p&gt;OK..so while you find &amp;#8216;isEmpty&amp;#8217; to be intention-revealing, the same is not true for Uncle Bob&amp;#8217;s one-line methods.&lt;/p&gt;


	&lt;p&gt;If you look back 11 posts from Uncle Bob&amp;#8217;s, you&amp;#8217;ll see Brett&amp;#8217;s firts follow-up post, in which he says:&lt;/p&gt;


	&lt;p&gt;&lt;i&gt;By the way, this is the beginnings of a method to handle input for an RPN calculator. Input is more complex than I first imagined:&lt;/i&gt;
&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;i&gt;Initially digits are accumulating.&lt;/i&gt;&lt;/li&gt;
		&lt;li&gt;&lt;i&gt;After pressing enter, the next digit will replace the top of the stack (the x register) because pressing enter duplicated the current top.&lt;/i&gt;&lt;/li&gt;
		&lt;li&gt;&lt;i&gt;After executing an operator, e.g., + &amp;#8211; / * !, typing a digit duplicates the top of the stack (while another operator uses what&#8217;s there).&lt;/i&gt;&lt;/li&gt;
	&lt;/ul&gt;


&lt;br&gt;
Based on that, the following is possible:
	&lt;ol&gt;
	&lt;li&gt;The accumulate method is called that way because it tells the Calculator to go into a state in which it accumulates digits.&lt;/li&gt;
		&lt;li&gt;The take method is called that way because it takes the user&amp;#8217;s input to the calculator.&lt;/li&gt;
		&lt;li&gt;The Register class is called that way because the Calculator has a number of register, e.g. x.&lt;/li&gt;
		&lt;li&gt;The dup method maps to the problem domain (or system metaphor) concept of duplicating the top of a stack&lt;/li&gt;
		&lt;li&gt;The enter method maps to the problem domain concept of pressing the calculator&amp;#8217;s enter button.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;So Uncle Bob has created methods that map to problem-domain abstractions and are therefore intention-revealing to anyone who knows enough about the problem domain. To make the code readable, he has hidden code that tells us HOW the problem solution is implemented, behind code that models WHAT happens in the problem domain.&lt;/p&gt;


	&lt;p&gt;You seem (to me) to be saying that Uncle Bob&amp;#8217;s one-line methods make the code harder to read because they are not intention-revealing (to you because you don&amp;#8217;t know enough about the problem domain), so the code would be better off (more readable) if instead of telling you about WHAT happens in the problem domain, it just told you HOW the solution to the problem is implemented e.g. using stack operations.&lt;/p&gt;


	&lt;p&gt;I disagree. Uncle Bob has chunked implementation details and hid them behind intention-revealing method names, so that people who know enough about the problem domain (e.g. anyone intending to maintain the code) can read his code more quickly and accurately.&lt;/p&gt;</description>
      <pubDate>Sun, 14 Jun 2009 17:10:09 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:cba02e13-ee0e-4200-a1fc-52fdb4027ce7</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3565</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Chris</title>
      <description>&lt;p&gt;@Philip Schwarz&lt;/p&gt;


	&lt;p&gt;I think the code you cited is an excellent example of taking a good rule of thumb to such an absurd extreme that it breaks.&lt;/p&gt;


	&lt;p&gt;I am amused by the fact that the one function in the proposed rewrite that does do something actually does two things; the name even describes (with no useful abstraction whatsoever) what those two things are. If iteration and alternation violate the Sacred Principle of Oneness enough to justify separating everything inside any pair of {} then surely sequencing does as well? I&amp;#8217;m mildly surprised that the person who wrote that originally hasn&amp;#8217;t yet died of old age waiting for an infinite recursion to finish! :-/&lt;/p&gt;


	&lt;p&gt;The tragic thing is that while messing around with dogmatic changes to achieve small functions for no particular benefit, the author has missed the wood for the trees. The first question I would ask about the original function is why this code is messing around so much with the details of another object. Tell, don&amp;#8217;t ask:&lt;/p&gt;


&lt;pre&gt;
public void pay()
{
  for (Employee e : employees)
  {
    e.pay();
  }
}
&lt;/pre&gt;

	&lt;p&gt;and on the Employee class:&lt;/p&gt;


&lt;pre&gt;
public void pay()
{
  if (isPayDay())
  {
    deliverPay(payDue());
  }
}
&lt;/pre&gt;

	&lt;p&gt;Then we might consider further improving the function names, but cleaning those up requires knowledge of the context that hasn&amp;#8217;t been provided in this example, so there&amp;#8217;s not much we can do beyond renaming the calculation function to something more appropriate given that its purpose is defined by the value it returns.&lt;/p&gt;


	&lt;p&gt;Of course, this whole example is very artificial, as it&amp;#8217;s unlikely that in a real system an Employee object would know how to make a payment without depending on other parts of the system anyway&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Sun, 14 Jun 2009 00:56:15 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:20662a32-8880-4c96-996b-8572a525de8f</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3564</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Chris</title>
      <description>&lt;p&gt;@Philip Schwarz&lt;/p&gt;


	&lt;p&gt;We seem to be overlapping here because of some funny delays in posting, so I don&amp;#8217;t know if you&amp;#8217;d seen my earlier posts that already addressed some of your points when you wrote your latest contribution. I&amp;#8217;ll just briefly comment on a couple of your other points for now.&lt;/p&gt;


	&lt;p&gt;On the subject of Composed Method:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Divide your program into methods that perform one identifiable task. Keep all of the operations in a method at the same level of abstraction. This will naturally result in programs with many small methods, each a few lines long.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;The first two principles I completely agree with. This is basic functional decomposition advice that has been around since the programming stone age (though alas many professional developers seem to have forgotten it shortly afterwards in their quest to objectify this and higher level language that).&lt;/p&gt;


	&lt;p&gt;But I can&amp;#8217;t agree with the conclusion. I&amp;#8217;ve worked on code in several fields, from mathematical modelling to instrument control, where it would be perfectly possible to have a function dozens, perhaps even hundreds, of lines long that &lt;em&gt;did&lt;/em&gt; represent a single task with the implementation at the same level of abstraction throughout. Usually the control flow would be quite linear, but subdividing the logic further would just be an artificial break.&lt;/p&gt;


	&lt;p&gt;In any case, as I mentioned before, my objection is not to small functions as such, just to small functions that don&amp;#8217;t really gain us any useful abstraction. To wit, I see a marked contrast between your comments on the Intention Revealing Message pattern with the isEmpty example and Uncle Bob&amp;#8217;s take function that you quoted. In particular, I could immediately guess what isEmpty would mean from the calling code, and while it is only a single line long, it does represent a useful abstraction.&lt;/p&gt;


	&lt;p&gt;On the other hand, despite knowing the general problem area we&amp;#8217;re discussing, I still don&amp;#8217;t see why the names take, dup, enter and accumulate are particularly helpful here. take seems rather arbitrary: neither it nor the name Register provide enough context for it to be a meaningful concept without further information. dup betrays the underlying implementation, while enter randomly obscures the implementation despite operating at the same level of abstraction. The worst is accumulate, which doesn&amp;#8217;t accumulate anything at all, despite having a common meaning along those lines when applied to a sequence in computer science literature; it actually changes the mode (and apparently in a way that only code related to the Register class is permitted to do, though the analogous functions for setting other modes are public). In fact, looking over the entirety of Uncle Bob&amp;#8217;s code again, it is littered with one-liners that are never actually used, yet betray implementation details or at best rename something without providing any additional abstraction.&lt;/p&gt;


	&lt;p&gt;In short, reading Uncle Bob&amp;#8217;s take function, I would have had to look up every single function it called anyway, to drop to a useful level of abstraction and work out what the underlying logic was, completely defeating the point of factoring out the individual routines. However, to work out what other code interacted with the take function, I would also have to analyse all code with access to the same instance of Register because the numerous small, public functions effectively expose the implementation anyway. Whatever happened to &amp;#8220;Tell, don&amp;#8217;t ask&amp;#8221;?&lt;/p&gt;


	&lt;p&gt;As for the small methods vs. increased interactions issue, I don&amp;#8217;t have much to add there. A few consultants can repeat the mantra as often as they like, but the evidence simply doesn&amp;#8217;t support their conclusion. Increasing &lt;em&gt;complexity&lt;/em&gt; is definitely a causal factor in making code harder to understand; this much is well supported by research. Moreover, it seems likely that there is a positive correlation between function length and function complexity, but it doesn&amp;#8217;t follow that small functions automatically help readability. It &lt;em&gt;does&lt;/em&gt; follow, however, that if the functions get so small that the complexity starts to increase because of all the added interactions, this harms readability.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jun 2009 22:59:14 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:6f00ca02-5286-4a9c-a47f-3b2cc274ab0a</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3563</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Chris&lt;/p&gt;


	&lt;p&gt;I typed in that last method incorrectly&amp;#8230;here is the correct version: &lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;public void calculateAndDeliverPay(Employee e)
{
  Money pay = e.calculatePay();
  e.deliverPay(pay);
}&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Sat, 13 Jun 2009 22:26:09 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:9d198835-053d-47da-bb69-e13fde1a6bfb</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3562</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Chris&lt;/p&gt;


	&lt;p&gt;OK, so you have a copy of clean code&amp;#8230;good&amp;#8230;based on your opinion of one-line methods, I suspect that Heuristic G30 (&lt;b&gt;Functions Should do One Thing&lt;/b&gt;) will give you a heart attack:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;public void pay()
{
  for (Employee e : employees)
  {
    if (e.isPayDay())
    {
      Money pay = e.calculatePay();
      e.deliverPay(pay);
    }
  }
}&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;This bit of code does three things&amp;#8230;This code would be better written as:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;public void pay()
{
  for (Employee e : employees)
  {
    payIfNecessary(e);
  }
}&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;public void payIfNecessary(Employee e)
{
  if (e.isPayDay())
  {
    calculateAndDeliverPay(e);
  }
}&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;public void pay()
{
  Money pay = e.calculatePay();
  e.deliverPay(pay);
}&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;What do you think?&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jun 2009 22:13:28 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d026a668-f578-4aeb-996f-91770472ec8f</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3561</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Chris&lt;/p&gt;


You said:
&lt;blockquote&gt;
This is in marked contrast to, for example, Code Complete by Steve McConnell, in which the author describes &#8220;a mountain of research&#8221; on the subject and cites six distinct sources that do not support the benefits of very small functions.
&lt;/blockquote&gt;

	&lt;p&gt;Thanks for that, I will dig my copy out.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jun 2009 22:03:27 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:572a4bf4-1431-446a-a2ab-a17ddde6d857</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3560</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Chris wrote:&lt;/p&gt;


&lt;blockquote&gt;
Why do we use functions? We use them to define an abstraction, to hide implementation details that are irrelevant to higher-level code. What are you abstracting by writing a single-line function like the ones in Uncle Bob&#8217;s example? Probably nothing. The problem doesn&#8217;t get any simpler by doing this.
&lt;/blockquote&gt;

&lt;blockquote&gt;
On the contrary, although each individual method becomes trivially simple to follow, a reader now has to laboriously scan through the whole verbose mess to find enough context to understand what is actually happening. This is not an improvement, it makes things much more complicated.
&lt;/blockquote&gt;

	&lt;p&gt;Object Mentor have their own school of thought about clean code. They don&amp;#8217;t claim their particular school to be &lt;i&gt;right&lt;/i&gt;. They do claim that if you follow their teachings, you will learn to write code that is clean and professional. They say that there are other schools of thought, and other masters, that have just as much claim to professionalism as they do. While they present their opinions as &amp;#8220;their&amp;#8221; absolutes (the way &lt;i&gt;they&lt;/i&gt; practice their &lt;i&gt;art&lt;/i&gt;, they acknowledge that many of their recommendations are controversial, and that not everyone will agree with them.&lt;/p&gt;


	&lt;p&gt;In &lt;a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882" rel="nofollow"&gt;Clean Code&lt;/a&gt;, Uncle Bob kicks off his recommendations for functions with three key principles: &lt;b&gt;they should be small, they should only do one thing, and all their statements should be at the same level of abstraction&lt;/b&gt;.&lt;/p&gt;


	&lt;p&gt;About function size, he says:&lt;/p&gt;


&lt;blockquote&gt;
the first rule of functions is that they should be small, and that the second rule is that &lt;i&gt;they should be smaller than that&lt;/i&gt;. 
&lt;/blockquote&gt;

	&lt;p&gt;His experience has taught him that functions should be very small. Describing a program written by Kent Beck, he says:&lt;/p&gt;


&lt;blockquote&gt;
every function in this program was just two, or three, or four lines long. Each was transparently obvious. Each told a story, And each led you to the next in a compelling order. That&amp;#8217;s how short your functions should be!
&lt;/blockquote&gt;

	&lt;p&gt;Going back to the three principles (&lt;b&gt;Small, Do One Thing, Same Level of Abstraction&lt;/b&gt;), you will recognise these as being the three components of Kent Beck&amp;#8217;s &lt;b&gt;&lt;a href="http://c2.com/cgi/wiki?ComposedMethod" rel="nofollow"&gt;Composed Method pattern&lt;/a&gt;&lt;/b&gt;:&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;Q&lt;/b&gt;: How do you divide a program into methods?&lt;br&gt;
&lt;b&gt;A&lt;/b&gt;: Divide your program into methods that perform one identifiable task. Keep all of the operations in a method at the same level of abstraction. This will naturally result in programs with many small methods, each a few lines long.&lt;/p&gt;


	&lt;p&gt;When describing the pattern, Kent Beck says:&lt;/p&gt;


&lt;blockquote&gt;
&lt;b&gt;The opportunity to communicate through intention revealing message names is the most compelling reason to keep methods small.&lt;/b&gt; People can read your programs much more quickly and accurately if they can understand them in detail, then chunk those details into higher level structures. Dividing a program into methods gives you an opportunity to guide that chunking. It is a way for you to subtly communicate the structure of your system.
&lt;/blockquote&gt;

	&lt;p&gt;And here is Kent Beck&amp;#8217;s &lt;b&gt;Intention Revealing Message&lt;/b&gt; pattern:&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;Q&lt;/b&gt;: How do you communicate your intent when the implementation is simple?&lt;br&gt;
&lt;b&gt;A&lt;/b&gt;: Send a message to &amp;#8220;self&amp;#8221;. Name the message so that it communicates what is to be done rather than how it is to be done. Code a simple method for the message.&lt;/p&gt;


	&lt;p&gt;Here is my translation into Java of one of his Smalltalk examples:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;Collection isEmpty()
{ 
    return this.size() == 0 
}&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;When describing the pattern, Kent Beck says:&lt;/p&gt;


&lt;blockquote&gt;
What is going on? Communication. Most importantly, &lt;b&gt;one line methods are there to communicate&lt;/b&gt;...Intention revealing messages are the most extreme case of writing for readers instead of the computer&amp;#8230;&lt;b&gt;methods that separate intention (what you want done) from implementation (how it is done) communicate better to a person.&lt;/b&gt;
&lt;/blockquote&gt;

	&lt;p&gt;&lt;b&gt;Brett&amp;#8217;s original &amp;#8216;take&amp;#8217; method is NOT a composed method.&lt;/b&gt;&lt;/p&gt;


	&lt;p&gt;It doesn&amp;#8217;t do just one thing (try describing it!): it is checking and modifying some state, doing some maths, manipulating a stack, and doing different things in different states.&lt;/p&gt;


	&lt;p&gt;Its statement are not at the same level of abstraction: on one line it is doing a low-level computation of the number of digits in a value, in others it using that number to compute a higher level function, in others it is manipulating a stack.&lt;/p&gt;


	&lt;p&gt;The method is not short: while it is not long, it is not a few lines.&lt;/p&gt;


	&lt;p&gt;Also, the method does not delegate to intention-revealing methods. It just exposes you in one go to all the gory details of how it does what it does.&lt;/p&gt;


	&lt;p&gt;If instead you take Uncle Bob&amp;#8217;s &amp;#8216;take&amp;#8217; method for the &amp;#8216;inserting&amp;#8217; mode:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;public void take(Register r, int v) {
  r.dup();
  r.enter(v);
  r.accumulate();
}&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;It is much more composed than the original method, and its calls to r.dup(), r.enter(...), and r.accumulate() (examples of the single-line methods you object to) are very similar in their effect to invocations of intention-revealing methods: by calling them, the &amp;#8216;take&amp;#8217; method tells you what it is doing, rather than how it is doing it (e.g. by manipulating a low-level stack or setting a class variable).&lt;/p&gt;


	&lt;p&gt;Regarding your concern about &lt;b&gt;&amp;#8216;laboriously scanning through the whole verbose mess to find enough context to understand what is actually happening.&amp;#8217;&lt;/b&gt;, this is acknowledged by the main proponents of small methods as a price worth paying.&lt;/p&gt;


	&lt;p&gt;In &lt;a href="http://books.google.com/books?id=1MsETFPD3I0C&amp;#38;dq=refactoring&amp;#38;printsec=frontcover&amp;#38;source=bn&amp;#38;hl=en&amp;#38;ei=kWM0StqdEZbKjAf3yOX9CQ&amp;#38;sa=X&amp;#38;oi=book_result&amp;#38;ct=result&amp;#38;resnum=5" rel="nofollow"&gt;Refactoring&lt;/a&gt;, Fowler writes:&lt;/p&gt;


&lt;blockquote&gt;
The object programs that live best and longest are those with short methods. &lt;b&gt;Programmers new to objects often feel that no computation ever takes place, that object programs are endless sequences of delegation.&lt;/b&gt; When you have lived with such a program for a few years, however, you learn just how valuable all those little methods are. All of the payoffs of indirection &amp;#8211; explanation, sharing, and choosing &amp;#8211; are supported by little methods
&lt;/blockquote&gt;

&lt;blockquote&gt;
&lt;b&gt;There is still an overhead to the reader of the code because you have to switch context to see what the subprocedure does&amp;#8230;.the real key to making it easy to understand small methods is good naming. If you have a good name for a method you don&amp;#8217;t need to look at the body.&lt;/b&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
The net effect is that &lt;b&gt;you should be much more aggressive about decomposing methods.&lt;/b&gt; A heuristic we follow is that whenever we feel the need to comment something, we write a method instead. &lt;b&gt;Such a method contains the code that was commented but is named after the intention of the code rather than how it does it. We may do this on a group of lines or on as little as a single line of code. We do this even if the method call is longer than the code it replaces, provided the method name explains the purpose of the code. The key here is not method length but the semantic distance between what the method does and how it does it.
&lt;/b&gt;&lt;/blockquote&gt;

	&lt;p&gt;In &lt;a href="http://www.amazon.com/Smalltalk-Best-Practice-Patterns-Kent/dp/013476904X" rel="nofollow"&gt;Smalltalk Best-Practice Patterns, Kent Beck writes:&lt;/a&gt;&lt;/p&gt;


&lt;blockquote&gt;
&lt;b&gt;Following the flow of control in programs with many small methods can be difficult. Novice Smalltalk programmers often complain that they can&amp;#8217;t figure out where any &amp;#8220;real&amp;#8221; work is getting done&lt;/b&gt;. As you gain experience, you will need to understand the flow of control through several objects less often. &lt;b&gt;Well chosen message names let you correctly assume the maning of invoked code.&lt;/b&gt;
&lt;/blockquote&gt;

	&lt;p&gt;And in &lt;a href="http://www.amazon.com/Implementation-Patterns-Addison-Wesley-Signature-Kent/dp/0321413091" rel="nofollow"&gt;Implementation Patterns&lt;/a&gt;, Kent Beck says:&lt;/p&gt;


&lt;blockquote&gt;
Code readers need to solve several problems that provide opposing influences on the size of methods. When reading for overall structure, seeing lots of code at once is valuable. ... The same big method that helped me orient myself becomes a hindrance when I turn to trying to understand the code in detail&amp;#8230;.Simultaneously supporting browsing and digesting is the challenge of the code author who is dividing logic into methods. &lt;b&gt;I find that my code reads best when I break it into relatively small methods (at least by C standards).&lt;/b&gt;
&lt;/blockquote&gt;</description>
      <pubDate>Sat, 13 Jun 2009 21:58:59 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:9eef09b3-72e2-4682-b114-fcf0f7250ac8</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3559</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Chris</title>
      <description>&lt;p&gt;Apologies if this post is a duplicate; I seem to be having some trouble replying here today.&lt;/p&gt;


	&lt;p&gt;@Philip Schwarz&lt;/p&gt;


	&lt;p&gt;Regarding the &amp;#8220;small functions = good&amp;#8221; claim I criticised, the first source that comes to mind is &lt;i&gt;Clean Code&lt;/i&gt; by Uncle Bob himself, which has this to say on the subject:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;The first rule of functions is that they should be small. The second rule of functions is that &lt;em&gt;they should be smaller than that&lt;/em&gt;. This is not an assertion that I can justify. I can&amp;#8217;t provide any references to research that shows that very small functions are better. [...] What this experience has taught me, through long trial and error, is that functions should be very small.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Of course, there are numerous other sources, from web sites to in-house style guides, that claim that functions should be no longer than one screen/50 lines/some other arbitrary limit. Googling &amp;#8220;functions should be short&amp;#8221; appears to turn up plenty of examples.&lt;/p&gt;


	&lt;p&gt;This is in marked contrast to, for example, &lt;i&gt;Code Complete&lt;/i&gt; by Steve McConnell, in which the author describes &amp;#8220;a mountain of research&amp;#8221; on the subject and cites six distinct sources that do &lt;em&gt;not&lt;/em&gt; support the benefits of very small functions.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jun 2009 19:00:48 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:550f1c97-46fe-47e4-8507-2ff25a231b72</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3558</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Chris</title>
      <description>&lt;p&gt;Apologies if the first part of this post is a duplicate; I seem to be having some trouble replying here today.&lt;/p&gt;


	&lt;p&gt;@Philip Schwarz&lt;/p&gt;


	&lt;p&gt;If you&amp;#8217;d like to debate further, I&amp;#8217;ll be happy to defend my argument, but please allow me to clarify a couple of things immediately just to avoid talking at cross-purposes.&lt;/p&gt;


	&lt;p&gt;Firstly, I am not against using small functions in general. However, I think the value of a function, at least from a readability perspective, is dictated by the amount of abstraction it enables rather than by the number of lines of code it contains. A corollary to this is that functions of literally only one or two lines, which don&amp;#8217;t have space to raise the level of abstraction very much, are often a warning sign to me, like seeing a class full of get/set methods for individual private data items and not much else. Sometimes these mechanics are justified, but often they are clues that the responsibilities and abstractions are broken.&lt;/p&gt;


	&lt;p&gt;Secondly, regarding my observation about locating the start-up code, of course you can fire up an IDE and use it to help you navigate code. My point was that in the original code I didn&amp;#8217;t have to, because I&amp;#8217;d comprehended the mechanics entirely just from reading the code, probably in less time than it would take to load that IDE. I find it hard to reconcile this with the claim that the longer code that requires IDE support to navigate with reasonable efficiency is more readable.&lt;/p&gt;


	&lt;p&gt;I notice that you&amp;#8217;ve just replied again, asking about the &amp;#8220;small functions = good&amp;#8221; claim I criticised. The first source that comes to mind is &lt;i&gt;Clean Code&lt;/i&gt; by Uncle Bob himself, which has this to say on the subject:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;The first rule of functions is that they should be small. The second rule of functions is that &lt;em&gt;they should be smaller than that&lt;/em&gt;. This is not an assertion that I can justify. I can&amp;#8217;t provide any references to research that shows that very small functions are better. [...] What this experience has taught me, through long trial and error, is that functions should be very small.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Of course, there are numerous other sources, from web sites to in-house style guides, that claim that functions should be no longer than one screen/50 lines/some other arbitrary limit. Googling &amp;#8220;functions should be short&amp;#8221; appears to turn up plenty of examples.&lt;/p&gt;


	&lt;p&gt;This is in marked contrast to, for example, &lt;i&gt;Code Complete&lt;/i&gt; by Steve McConnell, in which the author describes &amp;#8220;a mountain of research&amp;#8221; on the subject and cites six distinct sources that do &lt;em&gt;not&lt;/em&gt; support the benefits of very small functions.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jun 2009 13:56:46 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:41db9bb0-e62f-40f4-a49a-0319a3075133</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3557</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Chris</title>
      <description>&lt;p&gt;@Philip Schwarz&lt;/p&gt;


	&lt;p&gt;If you&amp;#8217;d like to debate further, I&amp;#8217;ll be happy to defend my argument, but please allow me to clarify a couple of things immediately just to avoid talking at cross-purposes.&lt;/p&gt;


	&lt;p&gt;Firstly, I am not against using small functions in general. However, I think the value of a function, at least from a readability perspective, is dictated by the amount of abstraction it enables rather than by the number of lines of code it contains. A corollary to this is that functions of literally only one or two lines, which don&amp;#8217;t have space to raise the level of abstraction very much, are often a warning sign to me, like seeing a class full of direct get/set methods for individual private data items and not much else. Sometimes these mechanics are justified, but often they are clues that the responsibilities and abstractions are broken.&lt;/p&gt;


	&lt;p&gt;Secondly, regarding my observation about locating the start-up code, of course you can fire up an IDE and use it to help you navigate code. My point was that in the original code I didn&amp;#8217;t have to, because I&amp;#8217;d comprehended the mechanics entirely just from reading the code, probably in less time than it would take to load that IDE. I find it hard to reconcile this with the claim that the longer code that requires IDE support to navigate with reasonable efficiency is more readable.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jun 2009 13:36:30 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:80025bc0-1678-4a47-9e65-b9e0630401dd</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3556</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Chris wrote:&lt;/p&gt;


&lt;blockquote&gt;Sorry, but I just can&#8217;t agree with this. Breaking things down into numerous very short methods like that actively harms readability, and &lt;b&gt;it&#8217;s long past time people stopped dogmatically repeating &amp;#8220;small functions = good&amp;#8221; as if it&#8217;s some self-evident truth&lt;/b&gt;&lt;/blockquote&gt;

	&lt;p&gt;Could you please elaborate further?&lt;/p&gt;


	&lt;p&gt;Do you have a particular time in mind when people dogmatically kept repeating that equation as if it was self-evidently true&amp;#8230;was it this decade? was it the previous one?&lt;/p&gt;


	&lt;p&gt;And who was it that did this?, do you have particular personalities/methodologies in mind?&lt;/p&gt;


	&lt;p&gt;I am genuinely interested, and would be grateful if you could tell me more about this, or just give me some pointers.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jun 2009 13:21:45 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:681fbcab-0fdf-4ac1-8739-eac2a4e80033</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3555</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Chris&lt;/p&gt;


	&lt;p&gt;Thank you for taking the time to explain why you think that rather than improving readability, breaking code down into many very short methods actually makes it harder to read. I was hoping someone would care enough to respond, and find the energy to do it.&lt;/p&gt;


	&lt;p&gt;You made several points, which I will tackle in separate posts.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ll start with a minor one.&lt;/p&gt;


	&lt;p&gt;You said: &lt;i&gt;it took me a couple of minutes just to be sure of the entry point in Uncle Bob&#8217;s example that corresponded to the original function.&lt;/i&gt;&lt;/p&gt;


	&lt;p&gt;I pasted Uncle Bob&amp;#8217;s code, a single class called Register, in an empty Java project in the Eclipse IDE, and opened up the class with the default Project Explorer, which showed the method signatures of the class, and immediately revealed only three public methods: &lt;b&gt;insert()&lt;/b&gt;, &lt;b&gt;replace()&lt;/b&gt;, and &lt;b&gt;take(int)&lt;/b&gt;, the latter clearly corresponding to the original function.&lt;/p&gt;</description>
      <pubDate>Sat, 13 Jun 2009 12:01:50 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b2523fe9-c252-4b3e-ad33-b90612d8cbd8</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3554</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Chris</title>
      <description>Philip Schwarz wrote:
	&lt;blockquote&gt;
		&lt;p&gt;In my view, while Uncle Bob&#8217;s solution is much longer, it is much more readable.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Sorry, but I just can&amp;#8217;t agree with this. Breaking things down into numerous very short methods like that actively &lt;em&gt;harms&lt;/em&gt; readability, and it&amp;#8217;s long past time people stopped dogmatically repeating &amp;#8220;small functions = good&amp;#8221; as if it&amp;#8217;s some self-evident truth.&lt;/p&gt;


	&lt;p&gt;Why do we use functions? We use them to define an abstraction, to hide implementation details that are irrelevant to higher-level code. What are you abstracting by writing a single-line function like the ones in Uncle Bob&amp;#8217;s example? Probably nothing. The problem doesn&amp;#8217;t get any simpler by doing this.&lt;/p&gt;


	&lt;p&gt;On the contrary, although each &lt;em&gt;individual&lt;/em&gt; method becomes trivially simple to follow, a reader now has to laboriously scan through the whole verbose mess to find enough context to understand what is actually happening. This is not an improvement, it makes things much more complicated.&lt;/p&gt;


	&lt;p&gt;As a result, it took me a couple of minutes just to be sure of the entry point in Uncle Bob&amp;#8217;s example that corresponded to the original function. This was not helped by having several methods on different but intimately related types sharing the same name, compounded by virtuality in one case. I&amp;#8217;d read and understood the mechanics of the &lt;em&gt;entire&lt;/em&gt; original code snippet in far less time.&lt;/p&gt;


	&lt;p&gt;As for the cyclomatic complexity, of course we want to keep the logic straightforward, but let&amp;#8217;s be serious: we&amp;#8217;re talking about a single real decision here, a simple dispatch based on what appears to be a choice of three possible modes. The consequences of that decision affect a single piece of shared state (the stack) and are defined by a single extra input value (the parameter to the take function). Distributing such simple logic  across 14 different functions over more than 80 lines of code is madness.&lt;/p&gt;


	&lt;p&gt;Moreover, the decision is based on a simple value, not a type, so obscuring it by using an enumeration/virtuality trick where a simple switch or if-chain would do is unhelpful. Use an enumeration for the possible states, sure. Pull out a function that switches on the current state and dispatches to one function per state accordingly, by all means. But the rest is just trying to be clever, and I don&amp;#8217;t see any advantage at all, never mind anything to justify the extra verbosity and clutter introduced.&lt;/p&gt;


	&lt;p&gt;In a nutshell, this problem isn&amp;#8217;t even close to the size and complexity where patterns like State and Visitor pull their weight, nor is there any evidence that it is likely to become so any time soon. If dogma is appropriate to a problem like this, I suggest that You Ain&amp;#8217;t Gonna Need It would have been a better choice.&lt;/p&gt;</description>
      <pubDate>Fri, 12 Jun 2009 17:32:56 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:110dd009-ad80-4067-ba80-80e9a0347004</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3547</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;What a coincidence that Uncle Bob&amp;#8217;s solution should appear straight after the Haskell one. I say this partly because these are the two solutions that stand out the most (in my view), but more importantly, because while they are so different, they both stand out (again, in my view) as the most readable.&lt;/p&gt;


	&lt;p&gt;My first reaction when I saw Uncle Bob&amp;#8217;s solution, straight after the extreme succinctness of Haskell, was something along the lines of: what a huge contrast&amp;#8230;how very long Bob&amp;#8217;s solution is compared to the Haskell one&amp;#8230;how can it take so much code to implement the same functionality?...&lt;/p&gt;


	&lt;p&gt;But then I started going back to Bob&amp;#8217;s code, again and again, and once I took into account the huge expressiveness gap between the functional and OO paradigms, I have to say that I think that Bob&amp;#8217;s solution is just as remarkable as the Haskell one in terms of its extreme readability.&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;Which of the solutions you have seen would you prefer to maintain?&lt;/b&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;This is a master at work, producing extremely readable code.&lt;/b&gt;&lt;/p&gt;


	&lt;p&gt;Brett&amp;#8217;s original snippet, the one he asked us to refactor, was a single method with 15 lines of code, and a &lt;b&gt;Cyclomatic Complexity (CC)&lt;/b&gt; of 4.&lt;/p&gt;


	&lt;p&gt;Bob has replaced this with 14 methods: nine one-liners, three two-liners, one three-liner, and one six-liner (the magnitude method). While the magnitude method has a CC of 3, all other methods have a CC of 1.&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;The result is some seriously readable code&lt;/b&gt;. No conditionals. Most of the time you are looking at a one-line method! A few times, it is a 2 or 3 line method. Also, while there is still some relatively high CC and high line-count in the system, it has been relegated to a single method (magnitude), which thanks to a very clear name and responsibility, won&amp;#8217;t necessarily need to be read to understand all the rest of the code.&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;James Ladd said&lt;/b&gt;:&lt;/p&gt;


&lt;blockquote&gt;
I&#8217;d like to play devils advocate and say that the method [Brett&amp;#8217;s original] doesn&#8217;t need refactoring, since it is only 13 statements and not hard to understand.

Why should you refactor this method? Note that some of the current refactorings come out to more than 13 statements and don&#8217;t necessarily add anymore clarity.
&lt;/blockquote&gt;

	&lt;p&gt;In my view, while Uncle Bob&amp;#8217;s solution is much longer, it is much more readable.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ll conclude with a quote from Uncle Bob&amp;#8217;s Clean Code:&lt;/p&gt;


&lt;blockquote&gt;
&lt;b&gt;the ratio of time spent reading vs. writing is well over 10:1.&lt;/b&gt; We are constantly reading old code as part of the effort to write new code.

	&lt;p&gt;Because this ratio is so high, we want the reading of code to be easy, even if it makes the writing harder. Of course there&amp;#8217;s no way to write code without reading it, so making it easy to read actually makes it easier to write.&lt;/p&gt;


There is no escape from this logic. You cannot write code if you cannot read the surrounding code. The code you are trying to write today will be hard or easy to write depending on how hard or easy the surrounding code is to read. &lt;b&gt;So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read&lt;/b&gt;.
&lt;/blockquote&gt;</description>
      <pubDate>Wed, 10 Jun 2009 18:13:37 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c36a2505-254c-42e5-b4d6-7be432299ddf</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3531</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Brett L. Schuchert</title>
      <description>&lt;p&gt;&lt;b&gt;Philip Schwarz&lt;/b&gt; wrote:
&lt;blockquote&gt;
But what does violating command/query separation buy you? Unless the violation pays for itself, how about avoiding it by simply pushing the pop() (pun not intended) out of determineNewTop, and explicitly passing the popped value into the method?:
&lt;/blockquote&gt;&lt;/p&gt;


	&lt;p&gt;I like it. In fact, I made that change and you&amp;#8217;ll see it in the next exercise.&lt;/p&gt;


	&lt;p&gt;Thanks!&lt;/p&gt;</description>
      <pubDate>Tue, 09 Jun 2009 21:42:54 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c423b105-bd51-466d-b300-0a8e08c6e51a</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3525</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Vincent Burns</title>
      <description>&lt;p&gt;Erlang version&lt;/p&gt;


	&lt;p&gt;obligary use of processes and message passing&lt;/p&gt;


&lt;pre&gt;
start() -&amp;gt;
    F = fun(Mode, [Top | Stack], F) -&amp;gt;
        receive
        {take, V} -&amp;gt;
             case Mode of
             accumulating -&amp;gt;
                 Digits = round(math:pow(10, round(math:log10(V) + 1))),
                 F(Mode, [Top * Digits + V | Stack], F);
             replacing -&amp;gt;
                 F(accumulating, [V | Stack], F);
             inserting -&amp;gt;
                 F(accumulating, [V, Top, Top | Stack], F)
             end
        end
    end,
    spawn(F).
&lt;/pre&gt;

	&lt;p&gt;and a version without the spawn and messages&lt;/p&gt;


&lt;pre&gt;
take({accumulating, [Top | Stack]}, V) -&amp;gt;
    Digits = round(math:pow(10, round(math:log10(V) + 1))),
    {accumulating, [Top * Digits + V | Stack]};
take({replacing, [_ | Stack]}, V) -&amp;gt;
    {accumulating, [V | Stack]};
take({inserting, [Top | Stack]}, V) -&amp;gt;
    {accumulating, [V, Top, Top | Stack]};
&lt;/pre&gt;</description>
      <pubDate>Tue, 09 Jun 2009 19:11:35 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:2ea87201-828c-470d-82b8-838255e8ded6</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3522</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Brett L. Schuchert&lt;/p&gt;


	&lt;p&gt;&lt;i&gt;You said: the determineNewTop method both changes the stack (with a pop) and returns a value. Violates command/query separation, but it is also a private method, so I&#8217;m not as concerned about that violation.&lt;/i&gt;&lt;/p&gt;


	&lt;p&gt;But what does violating command/query separation buy you? Unless the violation pays for itself, how about avoiding it by simply pushing the pop() (pun not intended) out of determineNewTop, and explicitly passing the popped value into the method?:&lt;/p&gt;


&lt;pre&gt;
  public void take(int value) {
    if (currentMode == Mode.accumulating) 
      value = determineNewTop(stack.pop(), value);

    if (currentMode == Mode.replacing) 
      stack.pop();

    stack.push(value);
    currentMode = Mode.accumulating;
  }

  private int determineNewTop(int top, int value) {
    int newTopValue = top;

    String digits = Integer.toString(value);
    while(digits.length() &amp;gt; 0) {
      newTopValue *= 10;
      newTopValue += Integer.parseInt(digits.substring(0,1));
      digits = digits.substring(1);
    }

    return newTopValue;
  }
&lt;/pre&gt;

	&lt;p&gt;And maybe, since you tolerated modifying input parameter &amp;#8216;value&amp;#8217; in the &amp;#8216;take&amp;#8217; method, you may want to do the same thing in &amp;#8216;determineNewTop&amp;#8217;: drop &amp;#8216;newTopValue&amp;#8217; and just modify input parameter &amp;#8216;top&amp;#8217;.&lt;/p&gt;</description>
      <pubDate>Tue, 09 Jun 2009 18:21:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:495f6908-195b-46bf-afd1-50761782283e</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3521</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Philip Schwarz</title>
      <description>&lt;p&gt;@Mike Bria&lt;/p&gt;


	&lt;p&gt;Neat&amp;#8230;I like the fact that you got the actOn methods to shield the reader from the implementation details of stack manipulation&amp;#8230;.but wait&amp;#8230;.in Accumulater you didn&amp;#8217;t go the whole way&amp;#8230; shouldn&amp;#8217;t its actOn method be calling something like &lt;b&gt;adjustTopBy(value)&lt;/b&gt;, rather than &lt;b&gt;stack.push(topAdjustedBy(value))&lt;/b&gt;?&lt;/p&gt;</description>
      <pubDate>Tue, 09 Jun 2009 16:47:49 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b8732d86-394f-4cfa-865a-f03e16055ee8</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3520</link>
    </item>
    <item>
      <title>"Refactoring Exercise" by Phil</title>
      <description>&lt;p&gt;I don&amp;#8217;t know about refactoring this short method, but if your goal is an rpn calculator program, you can see mine on my blog of programming etudes at &lt;a href="http://programmingpraxis.wordpress.com/2009/02/19/rpn-calculator/.&lt;/p" rel="nofollow"&gt;http://programmingpraxis.wordpress.com/2009/02/19/rpn-calculator/.&lt;/a&gt;&lt;/p&gt;&gt;</description>
      <pubDate>Tue, 09 Jun 2009 14:10:55 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:7fdca299-c558-4970-ae83-6cd2d8195d3d</guid>
      <link>http://blog.objectmentor.com/articles/2009/06/04/refactoring-exercise#comment-3519</link>
    </item>
  </channel>
</rss>
