Tuesday, March 25, 2014

Factors of Refactoring

Even if you've never written a line of program or script code in your life, you know what refactoring is.  You may not have known it, but you knew it.  Confused yet?  Let me help.

Refactoring is basically "refinement" or "optimization" or "streamlining".

Let's try this analogy on:

You build a book case from scraps of wood and some hand tools in your garage.  After the first shelf, you decide you want to build another one.  But this time you modify the design to change some of the parts and how they're joined together.  Maybe you change it so there are fewer parts overall.  The first one was assembled from eight (8) pieces of wood, and sixty-four (64) screws.  The second one from six (6) pieces, and thirty-two (32) screws.  After a few more, you realize you can rearrange the cut patterns on a sheet of raw plywood to get more parts from each sheet with less wasted scraps.  Eventually, you have a sturdier product, requiring less material and lower cost to build each one.

Or how about this:

You cook meals and find that you tend to make mostly Italian recipes.  After several dinners you realize that by reorganizing the pantry and spice rack you can more easily get the ingredients to the stove top in less time and with less confusion.  As time goes on, you move things around: pot racks, islands, roller carts, etc.  After a month of "trial and error" you now have the most streamlined kitchen setup imaginable.  Everything from the cookware, cooking area and ingredients to the paths you traverse moving from one station to another is now as good as it can be.  Eventually, you realize you've reduced the time and effort required to cook most meals by 50% and lowered your costs as well.

Or how about this:

You wrote a script, and it does a lot of separate, but sequentially-dependent tasks in order to arrive at a desired result.  The first draft contains 12 distinct custom function blocks, and repeated "for" or "while" loops on the same of data.  The second draft contains 10 distinct custom function blocks and the repeated iteration blocks are now calls to one of the custom functions.  By the third draft, the total number of code lines has been reduced from 1034 to 811 and down to 643.

Going back to look for ways to accomplish the same task in fewer steps.  With better quality results.  Improved reliability, predictability, dependability.  All those other -ilities.  Reusing things instead of making new ones all the time.  Getting more out of same effort than you did before.

This is refactoring.

Case in Point:  Program Files Example 101

Phase 1 - two files, one does option "A" and the other option "B"
Phase 2 - files are combined and use input parameter (i.e. "switches") to drive the code workflow.
Phase 3 - instead of distinct blocks of code, sections are combined into calls to one function with input params.
Phase 4 - instead of forked code paths in each function, the function now passes the input params through to drive external interfaces via concatenated references (e.g. you call one SQL view or another by passing in the param as the view name itself)
Phase 5 - SQL view is replaced with a stored procedure or db function that builds expression with input param directly.
Phase 6 - your work day now goes from 6 hours of keyboard banging and eating at your desk, to 4 hours of enjoyable keystrokes and going out for lunch.

For those of you who already knew this and are staring at this page like you're witnessing a cat eating a pig, whole, I apologize for your agony.


Post a Comment