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.

See Also:

James Grenning's ZOMBIES approach to TDD which is related to counting Zero, One, Many.

Five Underplayed Premises of TDD - GeePaw


Post a Comment

Most Popular on Agile Complexification Inverter

Exercise:: Definition of Ready & Done

Assuming you are on a Scrum/Agile software development team, then one of the first 'working agreements' you have created with your team is a 'Definition of Done' - right?



Oh - you don't have a definition of what aspects a user story that is done will exhibit. Well then, you need to create a list of attributes of a done story. One way to do this would be to Google 'definition of done' ... here let me do that for you: http://tinyurl.com/3br9o6n. Then you could just use someone else's definition - there DONE!

But that would be cheating -- right? It is not the artifact - the list of done criteria, that is important for your team - it is the act of doing it for themselves, it is that shared understanding of having a debate over some of the gray areas that create a true working agreement. If some of the team believes that a story being done means that there can be no bugs found in the code - but some believe that there can be some minor issues - well, …

Elements of an Effective Scrum Task Board

What are the individual elements that make a Scrum task board effective for the team and the leadership of the team?  There are a few basic elements that are quite obvious when you have seen a few good Scrum boards... but there are some other elements that appear to elude even the most servant of leaders of Scrum teams.









In general I'm referring to a physical Scrum board.  Although software applications will replicated may of the elements of a good Scrum board there will be affordances that are not easily replicated.  And software applications offer features not easily implemented in the physical domain also.





Scrum Info Radiator Checklist (PDF) Basic Elements
Board Framework - columns and rows laid out in bold colors (blue tape works well)
Attributes:  space for the total number of stickies that will need to belong in each cell of the matrix;  lines that are not easy eroded, but are also easy to replace;  see Orientation.

Columns (or Rows) - labeled
    Stories
    To Do
    Work In P…

Webinar: Collaboration at Scale: Defining Done, Ready, and NO.

I was invited to participate in a Scrum Alliance Webinar.  Maybe you would like to listen to us in a discussion of techniques to collaborate at scale (remotely and with many people).  The topic is one that I've got some experience in discussions - yet I never seem to get to done...
Collaboration at Scale: Defining Done and Ready and NO for Distributed Teams
With Joel Bancroft-Connors, Agile Organizational Coach; David A. Koontz, Agile Transition Guide; and Luke Hohmann, CEO and Founder of Conteneo, Inc.


14 February 2018 11 a.m. ET (USA).




The Scrum Guide is pretty clear on the criticality of the definition of Done: "When a Product Backlog item or an Increment is described as "Done," everyone must understand what "Done" means. However, the Scrum Guide ALSO says that the definition of Done can "vary significantly per Scrum Team." This leads us to examine when and how the definition of Done should vary, how distributed teams should cr…

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…

A T-Shaped 21st Century Knowledge Worker

Knowledge workers in the 21st Century must have many areas of deep knowledge, while also be capable of collaboration across multiple other domains with dissimilar T-shaped individuals.  This description of a person is a metaphor.  Compare it to the shape of the "I" in the classic saying there is no "I" in Team.


I first read about Scott Ambler's term "Generalizing Specialist" - but it's so hard to remember the proper order of the words... get it backwards and it has an inverted meaning... T-Shaped is easier to remember. 
A generalizing specialist is someone who:
Has one or more technical specialties (e.g. Java programming, Project Management, Database Administration, ...). Has at least a general knowledge of software development. Has at least a general knowledge of the business domain in which they work. Actively seeks to gain new skills in both their existing specialties as well as in other areas, including both technical and domain areas.  General…