Skip to main content

Intro to Refactoring with LEGOs

Practicing Into to Refactoring with LEGOs.

Refactoring is when a developer changes the structure of the code without changing the behavior.  To do this with little or no risk a craftsman will have a set of well maintained tests that prove the code still behaves exactly as before.

A simple way to visualize refactoring is to think of 12 eggs.  Most people will imagine a carton of eggs.  But it is possible to think of 2 cartons of 6 eggs; or even 6 packages of two hardboiled eggs.  Refactoring get's its name from the factorization of numbers.  Twelve has multiple factors (6 x 2), (3 x 4), (2 x 6), (1 x 12).  No matter which factorization the resulting collection is twelve eggs.  In TDD we have test to make sure we don't break the eggs.

Note:  Many IDE's claim to do refactoring, and many times they work just as expected; however not all IDE refactorings are reliable.  I suggest you test your IDE's refactoring before you trust it.

To introduce beginner developers to the basics of refactoring - start with a well understood class with many blocks of code.  First refactoring to practice is Extract Method.


But first make a program of blocks in this order:  white, red, blue, blue (again), red, yellow, green, red, yellow at the bottom.


Extract Method:  using the extract method refactoring - pull out the red block of code; replacing it with a call (the 4x2 red plate) to the extracted block.



Repeat the Extract Method operation on each red section of the program.



Now, a step of refactoring (or TDD) is to remove duplication.  All three of those red blocks of code are theoretically the very same, so we really only need one.  Set two aside and place the one remaining at the bottom of the program stack.


Good job!

Now let's use Extract Method operation on the two blue blocks of code.




And remove the duplication.  Remembering the one blue block remaining is place on the bottom of the program stack.


Practice, practice, practice.... the art of a craftsman.

Let's practice again with the yellow block;  perform the Extract Method operation on yellow blocks of code.




Well done.

Now the program looks a bit wonky... unbalanced by the large green block - let's extract it also.



There, good practice... and continue with the white block....



There all the in-lined class code has been extracted to methods and method calls.  That is well factored.  But let's do a bit of house keeping.  Clean up and reorder the methods and calls into a similar order.


When separated the blocks of code and the calling plates look like this:


Before and after refactoring images.














Required LEGOs

(1) Green 4x2 Plate;  (1) Green 4x2 Block
(1) White 4x2 Plate;  (1) White 4x2 Block
(2) Yellow 4x2 Plate;  (2) Yellow 4x2 Block
(2) Blue 4x2 Plate;  (2) Blue 4x2 Block
(3) Red 4x2 Plate;  (3) Red 4x2 Block



Reference:

Adapted from Bryan Beecham's (@BillyGarnet TDD with LEGOs presentations and PDF.


Post a Comment

Most Popular on Agile Complexification Inverter

David's notes on "Drive"

- "The Surprising Truth about what Motivates Us" by Dan Pink.

Amazon book order
What I notice first and really like is the subtle implication in the shadow of the "i" in Drive is a person taking one step in a running motion.  This brings to mind the old saying - "there is no I in TEAM".  There is however a ME in TEAM, and there is an I in DRIVE.  And when one talks about motivating a team or an individual - it all starts with - what's in it for me.

Introduction

Pink starts with an early experiment with monkeys on problem solving.  Seems the monkeys were much better problem solver's than the scientist thought they should be.  This 1949 experiment is explained as the early understanding of motivation.  At the time there were two main drivers of motivation:  biological & external influences.  Harry F. Harlow defines the third drive in a novel theory:  "The performance of the task provided intrinsic reward" (p 3).  This is Dan Pink's M…

Software Development terms applied to Home Construction

Let's Invert the typically wrong headed view of Software Development project management as a construction project.  We can map it the other way just to see if it works... to have some fun, to explore the meaning of phrases we toss around quite frequently.


Normally Project Management terms come from a construction domain.  We are going to apply the lexicon of modern software to the construction of a home.  We will follow the construction project and meet some of the people doing the work.

This is a very small (8 homes from $600,000 skyward) program in my 30-40 year old neighborhood.

About 6 months ago I saw the programs landing page go up.  It gives casual observers and some of the stakeholders a general idea of the intent of the program.  And most importantly who to contact for additional information if you happen to be interested in their products.

The Refuge program has 8 product projects and has them running independently.  Yet much of their DevOps infrastructure has already b…

Where is Shakespeare When We Need Him?

We are desperately searching for a term for people that connotes the best of human kind.  The creative, sensing, combinatorial synergistic, empathic solutioning persons that have yet to been labeled with a role name that works.

Some of the old terms:
Staff, Workforce, Human Resource, My Team, Army, Company

Shakespeare created 1700 words in his time.  He mutated verbs to nouns, and vice-a-versa, transformed verbs into adjectives, and formed words from whole cloth never before heard.  This skill is rare, but there is a poet that can create the term we need in the twenty-first century.

What should this term define?

21st Century Human Resource; the generalizing specialist.

Yes, but what more?  What less?

Suggest your poetry in the comments, let us see if we cannot do 1/1700 as well as The Bard.

By-the-way; who create the phrase "coin a word"?




a little feedback please...

some feedback please...
How do you like the new look and feel of our site?
  ___)  nah (I like the old one better - bring it back)
  _X_)  yeah (much cleaner and easy to navigate)



powered by Typeform
See Also:

Innovation in the Automobile Industry

In the 1900s the automobile industry was the most important and innovation industry in the USA.  But one could question if this was good for our society in the long run.  And one could question if they actually innovated.

In the early 1900s there were few automobiles, very little infrastructure created to support the industry.  For example the road system was still designed for horse drawn wagons and the wagon wheel (remember a steal rim and wooden compression spoke wheel).  The future US Highways, or the 1950s Interstate Highway System at the cost of $425 billion were decades and many innovations away. There was no gas service station, there were however horse stables, farriers, and blacksmiths in each town along the roads.  There was no real "road map", there was no road naming system, like was created in 1926 - the United States Numbered Highway System.

The industry employees millions of people, and was a large factor in the economy of the USA.  It created or was created b…