One of the big dilemmas programmers face every day is "Should I write this code as quickly as possible or should I write it to be as robust as possible?". Deadline pressures make some pick the quick option too often. :-( That's why I was pleasantly surprised to find
CodeSmith. CodeSmith is a template-based code generator that can output code that can then be used in any ASCII based language (e.g. Delphi).
During development, there are plenty of times when you need to create a collection of objects. For example, you have a TCustomer object and need a TCustomerList object to store a list of those objects. You have a lot of options on how to represent this. Here are some of those options:
- Place the TCustomer objects in a TList. You need to typecast the code, and you can put any object in the list. Plus, you have to worry about object lifetimes.
- Use a TObjectList instead of a TList. This allows you to not worry about object lifetimes, but the rest of the problems using a TList still exist.
- Use a TStrings object (e.g. TStringList), and add your object in to the Objects property
- Create a custom list class, where you don't have to worry about type safety or object lifetime
Unfortunately, the last option is rarely chosen due to time constraints. It doesn't take all that long to do, but if you have to stop what you're doing to create the custom class, it doesn't get done more times than not.
To solve that, I created a CodeSmith template to generate strongly typed collections. Download my template, install CodeSmith, play with it, and let me know what you think. I can think of some nice improvements, like adding comments (along with an option to include those comments or not). After I get some feedback on this, I plan to upload it to the CodeSmith File Share forum.