refactoring - the power of good variable, method and function names within code

Deep in some rainforest, an explorer found a long-lost tribe of people who could only count up to the number 2.

Imagine only being able to count up to 2!

To represent numbers, you’d have to say: “One”, “Two” or “Many”.

Most other cultures, of course, have names for bigger numbers like “One Thousand”. By doing this, it enables us (yes, I’m from one of those fancy ‘big-number-naming societies’) to not only ‘discuss’ them, but also to ‘comprehend’ and ‘use’ them.

They are, in effect, linguistic tools. They help us to design useful things like pyramids and Pez Dispensers. Enough of what you know, already!

Anyway - the reason why I’m ranting about ‘the power of names’ is because I design websites and the simple process of renaming something, often, opens up possibilities for me.

Example;

I, once, built a ‘classified advert listings’ website. For the sake of the example - lets get silly and say people used it to advertise their soiled underwear. I had one classified listing of pants and one listing of socks and another for bras. Because each type of garment was quite different, I had different specifications for each list.
E.g. People searching the list of socks wanted to order it by shoe-size, but that specification is not relevant to bras. To make things easier to code, I gave each type of item a reference number value for a variable called “item_type”;

So,

if ($item_type==1)
{
//we are talking pants here
}
elseif ($item_type==2)
{
//we are talking socks here
}
elseif ($item_type==3)
{
//we are talking bras here
}
else {
//Commando Error: No underwear found in Construct - expected, at least socks you sweaty monkey, you! }

This variable name, then, became a lynch-pin of the website. For everything I needed to do, I could write ONE function that would process ALL types of items.

All I needed to do was to pass it the “$item_type” variable and good stuff would happen:

function CreateNewItem( of $item_type 2) - One sock is created - that’s always handy ;o)
function ShowListOfItems( of $item_type 1 )- A multitude of Pants before your eyes.
function DeleteItem( of $item_type 3) - A Bra is destroyed.

However, my name was restricting my imagination!

For a long time I managed to use just one variable name for all functions that I needed to do. Functions that created items and functions that displayed them in lists were both passed the same variable. This convention came back and bit me when I tried to mix multiple types of items into one list. To cope with this I was adding extra values to the variable that really should not have been used.
e.g $item_type=5 //means a combination of pants and bras

So,
function ShowListOfItems( of $item_type 5 )- A list for those who absolutely hate socks, but cannot get enough of the pants-and-bras thang

But,
function CreateNewItem( of $item_type 5)
would throw a:
Logical Error: Cannot Create a “pantbra”

The only way to really get out of this situation was to add another variable name:

$item_type //means the type of item
$list_type //means the type of list

Then, I related the two variables when necessary. E.g
list_type 52 is related to pants, socks and vests. (Yes vests! The site was diversifying, fast.)

So, the moral of this, extremely silly, story is:

  1. You can ’survive’ with badly named variables. But, renaming can really open up the possibilities. So, “ABCR” - Always Be Considering Renaming!
  2. If things don’t seem to be going well in your scripts. Don’t be afraid to rename things!
  3. Renaming is not as tricky as you think - its fast. With a good ‘find and replace’ it can take just a few minutes and save you HOURS!

Resources

The practice of renaming stuff in scripts falls under a design process called: REFACTORING

Learn about it here:
Book: Refactoring: Improving the Design of Existing Code (Hardcover) by Martin Fowler

I hope you were not put off by my crazy silly jokey style. Criticize me in the comments part of this blog if you feel like grilling me!
Go on - I dare you! You soiled-panty fancying sweaty-sock munchin’ muppet, you!

(My lack of comments on this blog disturbs me. So, I’ve taken to abusing my visitors in order induce a response! Please add a comment - I beg you!)
——-
Hunter Brian: “Out in the forrest I found food”
Elder Steve: “Oh really, Brian. What type of food?”
Hunter Brian: “Well, Steve, I found fruit. Berries, to be exact.”
Elder Steve: “How many berries, Brian? The tribe is hungry. Was it 1, 2 or many?”
Hunter Brian: “Many”
Elder Steve: “Jolly good - lets go and eat ‘em.”

Leave a Reply