Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to update old code from TList to Generics Collections TList<> ?


This question is not answered.


Permlink Replies: 1 - Last Post: Jun 25, 2015 11:01 AM Last Post By: Peter Below
Incus J

Posts: 5
Registered: 7/16/09
How to update old code from TList to Generics Collections TList<> ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2015 2:33 AM
I'm reviewing a class I coded years ago that uses TList to form a tree of sub-objects (a hierarchy of instances of the same class), and contemplating the wisdom of updating its internal workings to use generic collections TList<TSpecificType> instead of ordinary TList.

I think the benefits might include better run time performance (by avoiding dynamic x:=MyList[i] as TSpecificType; casts), and maybe better code-completion information in the IDE and debugger visualizers (it would be great to be able to access individual objects stored in a TList<> while debugging).

However the first thing that jumps out is its current use of the Assign method to duplicate a TList e.g. MyList2.Assign(MyList1);

As far as I can tell a generics TList<> has no Assign method. Is there an equivalent?

The tree of TList objects can be huge (many hundreds of thousands of objects), so performance is critical. It would be very useful to know in what areas a generics TList<> might perform better/worse than an ordinary TList, or other potential 'gotchas'.

Can anyone point me to any info or articles that might help? Am I making a mistake converting from TList to TList<TSpecificType> ?

Elsewhere I'm also considering replacing TStringlist.Objects[i] with TDictionary<string,TObject> which I guess is similar.
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: How to update old code from TList to Generics Collections TList<> ?
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2015 11:01 AM   in response to: Incus J in response to: Incus J
Incus J wrote:

I'm reviewing a class I coded years ago that uses TList to form a
tree of sub-objects (a hierarchy of instances of the same class), and
contemplating the wisdom of updating its internal workings to use
generic collections TList<TSpecificType> instead of ordinary TList.

I think the benefits might include better run time performance (by
avoiding dynamic x:=MyList[i] as TSpecificType; casts), and maybe
better code-completion information in the IDE and debugger
visualizers (it would be great to be able to access individual
objects stored in a TList<> while debugging).

That is definitely a pro argument. But you can use typecasts in the
evaluate/modify dialog of the IDE to see details of "anonymous" objects
held in a TList.

However the first thing that jumps out is its current use of the
Assign method to duplicate a TList e.g. MyList2.Assign(MyList1);

As far as I can tell a generics TList<> has no Assign method. Is
there an equivalent?

Clear combined with AddRange should do that.


The tree of TList objects can be huge (many hundreds of thousands of
objects), so performance is critical. It would be very useful to
know in what areas a generics TList<> might perform better/worse than
an ordinary TList, or other potential 'gotchas'.

I'm afraid you will have to run tests to see whether any existing
difference would be significant in your context.

Can anyone point me to any info or articles that might help? Am I
making a mistake converting from TList to TList<TSpecificType> ?

Elsewhere I'm also considering replacing TStringlist.Objects[i] with
TDictionary<string,TObject> which I guess is similar.

The performance difference depends on the number of items you need to
add, and whether you do a lot more searching than adding. Addition is
more costly for a dictionary, both in terms of processing and memory
usage. Searching can be faster in a dictionary, but if your list is
pretty static after the addition phase and can be sorted it may in fact
be slower. Binary search in a sorted list is pretty fast, you probably
need to exceed several tens of thousands of items before you start to
see a significant difference in access speed. The big plus of a
dictionary is, of course, that search time is nearly O(1), independent
of the number of items.

Again only testing can tell whether the difference would be significant
for your application.

I'm a conservative type myself. I use the new things available for new
applications, but keep the old code unchanged if it works and is
maintainable (has a good enough structure). Change just for the sake of
change is bad, since you alway incrue a significant cost in terms of
work you need to invest and in terms of added risk (that your modified
code may not work quite as expected and the problems are not revealed
during testing). I usually only modify existing code if a change in
requirements needs a change anyway. In that case I may refactor code to
use more modern approaches.


--
Peter Below (TeamB)

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02