Tech Note: Stinky’s Refactoring Binge

Science • Views: 2,074

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];

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2023-04-04 11:11 am PDT
LGF User's Guide RSS Feeds

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Cash.app
Recent PagesClick to refresh
Texas County at Center of Border Fight Is Overwhelmed by Migrant Deaths EAGLE PASS, Tex. - The undertaker lighted a cigarette and held it between his latex-gloved fingers as he stood over the bloated body bag lying in the bed of his battered pickup truck. The woman had been fished out ...
Cheechako
Yesterday
Views: 102 • Comments: 0 • Rating: 0