Saturday, March 12, 2005

Object ID's replace elbow grease

All of us know these god-damn blocks of the same instruction, over and over again for a different form object. May it be the adjustment of visibility, the setting of a background color in VB or whatever, columns like the one below can be seen everyday:


The objects in the example above are called ObjA, ObjB and ObjC-not the kind of names that one should see in real-world programs, but o.k. nevertheless!
In most languages, the user has little choice. He can opnly refer to controls by their name-and names cannot be used in an array. Howeverm the Palm OS allows the programmer to access controls by their resource ID-which is a 16bit integer from 1000 to 9999. Does that make the bell ring? If the controls are lined up in an intelligent way, one can simply use a for loop to iterate through them.
Lets say that the ID's of the our objects are 1000,1001,1002. Then, one can easily take the following code to do the task mentioned above:

int i;

The benefit may not be visible with three objects, but when you have a block of 30 adjacent ones-trust me, you will feel the difference! Changing object ID's usually is no problem-unless there are hard-coded references, but these are getting rare nowadays.
What do you think?


Blogger epotter said...

I like this idea. I've used similar techniques. I did it slightly differently. I but the ID's in an array and referenced them from there. This allows for the IDs to be non sequential and I believe it improves maintainablility. I also like to put the long line of code in an inline function or a macro.

inline MyFrmHideObject (UInt16 ID)

void Foo()
const UInt16 numIDs = 4;

UInt16 IDs[numIDs] = {1000, 1001, 1005, 1008};

for (i = 0; i < numIDs; i++)


4:15 PM  

Post a Comment

<< Home