Tech Note: Stinky’s Refactoring Binge

Charles Johnsonfollow me on twitter
Science • Sat Sep 27, 2008 at 5:34 pm PDT • Views: 310

Stinky Beaumont is project-managing a major code refactoring process this weekend, that we’ve been putting off because it’s painful. We’re talking duplicated code. Global variables. Inconsistent variable naming. Remnants from the days before we started coding smart, instead of just getting it working. It’s not all like this, but the Ajax-based comments code in particular still had a funny smell in some neglected corners. In other words, it worked but it wasn’t sleek.

We’re replacing duplicated code with object methods, or functions where the procedural code still needs more untangling. Replacing global variables with constants where possible, object properties where not.

It’s happening bit by bit, piece by piece, step by step. While loop by function call. Much of the ‘new comments’ logic has now been refactored in this way, and the front page and daily archives pages now use a common display interface.

And with that, I’m opening registration for a very limited time.

UPDATE at 9/27/08 5:45:20 pm:

Here’s a tip that comes in handy for eliminating global variables in PHP.

Constants are very useful for this purpose. They’re automatically available in the global scope. But they’re limited to numbers or strings. No arrays. Bummer.

But you can define an array, then serialize it and define the resulting serialized string as a constant. Then when you need to use it, even within a function or object method, you can simply unserialize the array and you’re good to go. For example, to define the serialized array:

$myItems = array('item1', 'item2', 'item3');
define('MY_ITEMS', serialize($myItems));

Then, to access the “array constant:”

$array = unserialize(MY_ITEMS);

UPDATE at 9/28/08 9:54:46 am:

LGF reader “scrad” pointed out that you can access any variable in the global scope (including arrays and objects) by using PHP's $GLOBALS array. The above example could also be coded this way (eliminating the overhead of the serialize/unserialize functions):

$myItems = array('item1', 'item2', 'item3');

To access the second item in the array:

$item2 = $GLOBALS['myItems'][1];

Advertisement

709 comments

^ back to top ^

Name:

Pass:

Register Forgot Your Password? Re-send Confirmation (To log in, cookies must be enabled in your browser!)

Turn off ads by subscribing!
For about 33 cents a day, our subscription option turns off all advertisements at LGF!
Read more...


► LGF Headlines

  • Loading...

► Tweeted Articles

  • Loading...

► Tweeted Pages

  • Loading...

► Top 10 Comments

  • Loading...

► Bottom Comments

  • Loading...

► Recent Comments

  • Loading...

► Tools/Info

► Tag Cloud

► Contact

You must have Javascript enabled to use the contact form.
Your email:

Subject:

Message:


Messages may be published in our weblog, unless you request otherwise.
Tech Note:
Using the Contact Form

More Partners

Compare Electricity Prices in your area. Texas Electricity is deregulated; you have the right to choose Texas Electric Rates from among many Texas Electric Companies.

Overdrawn at the bank of karma.

TwitterFacebook
LGF Pages
Recent Pages

researchok
'I Was Looking Forward to a Quiet Old Age': Instead, Etta Shiber, Helped Smuggle Stranded Allied Soldiers To Freedom
3 hours, 56 minutes ago
Views: 54 • Comments: 0
Tweets: 1 • Rating: 0

Daniel Ballard
Late Afternoon Light-Kalanchoe
11 hours, 37 minutes ago
Views: 98 • Comments: 0
Tweets: 0 • Rating: 4

MikeySDCA
Colin Powell Endorsed Same-Sex Marriage Once It Was Safe, More Evidence He's Hardly a Great Leader.
11 hours, 41 minutes ago
Views: 127 • Comments: 1
Tweets: 0 • Rating: 1

Eclectic Infidel
City College of San Francisco Budget Update
12 hours, 29 minutes ago
Views: 115 • Comments: 0
Tweets: 0 • Rating: 0

Michael McBacon
Kansas governor signs 'Shariah bill' to ban Islamic law
17 hours ago
Views: 225 • Comments: 6
Tweets: 0 • Rating: 5

Aigle
National Geographic Traveler Veers Off Track
1 day, 17 hours ago
Views: 447 • Comments: 7
Tweets: 0 • Rating: -5

MichaelJ
Apple TV Slated to Debut in December?
1 day, 18 hours ago
Views: 224 • Comments: 0
Tweets: 0 • Rating: 1

Ascher
Israeli Who Saved Turk on Everest: You Never Abandon a Friend - Israel News, Ynetnews
1 day, 19 hours ago
Views: 296 • Comments: 1
Tweets: 0 • Rating: 3

Haywood Jabloeme
The Harrassment of Patterico & Its Roots in Left-Wing Activism
1 day, 19 hours ago
Views: 517 • Comments: 2
Tweets: 0 • Rating: 4

Curt
Brian Banks: (Video) Falsely accused of rape speaks out
1 day, 22 hours ago
Views: 271 • Comments: 2
Tweets: 0 • Rating: 5

 Frank says:

Whoever we are, whereever we're from, we should have noticed by now our behaviour is dumb, and if our chances are expected to improve, it's gonna take a lot more than trying to remove, the other race, or the other whatever, from the face of the planet altogether -- Dumb All Over, You Are What You Is