« Ma'am, I am tonight | Main| Travel advice »

Ooh, ooh, before I forget...

Category domino
Sorry, got a bit carried away there. It's just that I've become something of a refactoring addict lately. Maybe it's because I don't have the luxury of constantly refactoring my code at work, so stuff I play with at home (or, in this case, at a Ramada outside Memphis) gets refactored over and over again because I get a kind of buzz off of paring code down to its absolute essentials. I think maybe it's the programmatic equivalent of having a garage sale every few days... get rid of everything I don't need, find cool stuff in the attic that I forgot I even had, etc.

So I got an idea while driving today (15 hours of driving through the Great American Emptiness provides plenty of good musing time): judicious use of Static might trim this down even more. I don't really use Static... either I've never had a need for it, or just haven't discovered its true potential yet, but mulling over the nature of how the members of our little abstract singleton are initialized, it occurred to me that this might be a perfect place to use it.

Consider: a Boolean is automatically initialized to False. Static causes a variable's value to be saved between calls to the same procedure. So... instead of using Private Boolean class members to keep track of whether other members have already been initialized, how's about moving them inside the various Subs that initialize those members, but making them Static? That way, the first time each initialization sub is called, the corresponding Boolean is False, so initialization will take place, but the next time it will have become True, so initialization will be skipped. In other words, the same thing it's doing now... except that even the Booleans are not stored in memory until each is needed. I know, Booleans only take up 2 bytes, but if this class is expanded (or extended) to keep track of a large number of objects - which it could be, since it only grows as large in memory as the code calling it requires it to - this approach might help it to scale even better.

I'll discuss this with Chad and see what he thinks. Actually, Chad, if you're reading this, let me know what you think. Maybe I'm dwelling on this one topic too long, but it's already saved me a bunch of time, so I can't help wanting to refine it even more.

Comments

Gravatar Image1 - That's right, I'd forgotten. Bummer. I played around with an alternate approach - moving the initialization subs out of class scope so that the Booleans don't even exist until needed... I set each to take the object to be initialized as an argument, so that the first time each sub is called, the argument object would be passed as Nothing but intialized during the first call, left alone on subsequent calls. Works well, but that makes the class dependent on the external subroutines and vice versa. I suppose that would be fine, but something about the approach doesn't sit well with me... perhaps it's because I prefer classes to be as self-contained as possible. Oh well, back to the drawing board.

Gravatar Image2 - I like the idea but base on what I read and tried the Static option is not allowed in classes.

Gravatar Image3 - These are the pains that we have to go through to come up with something great. I like the ideas keep them coming, maybe Lotus will listen and add some of these features.

Contact Me

Hire Me

Elsewhere

What the Quote?

"It's kind of like a Purple Nurple, but with less pain"

Steven Rodgers

"I'm pretty sure that the Great Leader is some sort of twisted ass freak."

Brent Bowers

"Their server's been down five thousand minutes? Ya can't even find cell phone plans that high."

Steven Rodgers

"Duck is the new monkey."

Greg Rotz

"I've always wanted to say, 'Paris is burning", and have it be true."

Jennifer Holland

Apparel

Lotus Rocks

I write the code that makes the young girls cry

Current Terror Alert Level

Assorted Linkage

ClustrMap