Separate The What From The How

I use three (code) refactoring rules.

  1. OnceAndOnlyOnce - All code must appear in only one place.
  2. SeparateTheWhatFromTheHow - A method should comprise one how, or two or more whats
  3. WabiSabi - There is elegance in non-perfect, but working, code

Don't confuse with TheWhatButNotTheWhy


What's a "what"? How do you define "how"?

A "what" is a delegation to another method with a MeaningfulName. A "how" is a method that does one thing.

Example - before

 moveRoom: aRectangle
   couch
      x: (aRectangle origin x + couch offset x)
      y: (aRectangle origin y + couch offset y).
   chair
      x: (aRectangle origin x + chair offset x)
      y: (aRectangle origin y + chair offset y).
   bookshelf
      x: (aRectangle origin x + bookshelf offset x)
      y: (aRectangle origin y + bookshelf offset y).
after
 moveRoom: aRectangle
   | origin |
   origin := aRectangle origin.
   self moveCouch: origin.
   self moveChair: origin. 
   self moveBookshelf: origin.

SeparateTheWhatFromTheHow is an approximation. If a method doesn't read "what-what-what..." or "how", but there is no obvious fix, leave it alone.

I am having trouble following this psuedocode. What is Origin? Why is everything a rectangle? What is moveX (where X = Chair, etc.) Is it a method or attribute?

This isn't pseduocode, it's code, smalltalk code. I'm no smalltalker, but as best I know, "| origin |" declares a variable local to the method, and the "moveX" things are selectors (like keyword arguments) for methods. The example isn't complete because it doesn't show how the "couch x: (aRectangle blah blah blah" stuff has been moved into other methods of whatever class moveRoom: belongs to.


HaveThisPattern: Alternate approach to the "what vs how" question is an old ModularProgramming maxim:

"Functions should be managers or workers: A function should either do the work (being a worker) or delegate work to worker functions (making it a manager). Manager functions should not contain lots of code or algorithem, as their primary task is to determine what needs to be done, and then delegate it to others."


Isn't this similar to the old separation of mechanism and policy?


SeparateTheWhatFromTheHow is similar to ComposedMethod. See also ShortMethods and IntentionNotAlgorithm.


SuccessStory CategoryRefactoring
EditText of this page (last edited November 25, 2003)
FindPage by browsing or searching

This page mirrored in WikiPagesAboutRefactoring as of July 17, 2004