12 Delphi code templates to save keystrokes

Sometimes, typing is just no fun. As a programmer there are certain things that you need to type over and over (and over and over) again, with only the smallest of differences.

For example, how often have you declared a property that looks like this:

property SomeProp: Integer read GetSomeProp write SetSomeProp;

Next, you press Ctrl+C to auto-complete the declaration and then only do you get to writing the beef of what you’re trying to accomplish. OK, so it isn’t painful, but it is monotonous.

Suppose you could have the declaration above created by only supplying the property name and type? Like this:

Code template for properties

That’s the cool thing about code templates, which had its debut in Delphi 2006 Delphi 2005 (I think). Delphi ships with a number of ready-made templates, most of which are very nicely done. We can add a few more to make for less repetitive property declarations. But that’s not the best bit.

Delphi 2009 has support for generics, which allow (among many other things) the quick creation of type-safe enumerators, lists and hash tables. If you don’t have Delphi 2009 yet, that’s no reason to write these from scratch every time and it certainly is no reason to just throw your valuable data into lists designed for pointers.

Enter my code templates for type-safe object lists, enumerators and TBucketLists. Suppose I need an enumerator for TClientAccountList which contains TClientAccount objects, I only need to place my cursor among the private field declarations of TClientAccountList and type:

enumarray

Then press Tab. There are three placeholders, namely enumerator class name (TAccountEnumerator), list item class name (TClientAccount) and item list class name (TClientAccountList):

    type
      TAccountEnumerator = record
      private
        FIndex: Integer;
        FOwner: TClientAccount;
      public
        function GetCurrent: TClientAccountList; inline;
        function MoveNext: Boolean; inline;
        property Current: TClientAccountList read GetCurrent;

        constructor Create(AOwner: TClientAccount);
      end;

You also get a public GetEnumerator method and the implementations of these declarations, which you unfortunately will need to move to the implementation section of the unit yourself – I couldn’t find a way of doing this from within the template.

If you think the enumerator above looks a little strange, check out Hallvard Vassbotn’s excellent write-up on enumerators.

The code template for object lists obviously includes enumerators and work in a similar way.

So before I give you the downloads, a couple of instructions.

For Delphi 2007 and 2009, the XML files should be stored to My Documents\RAD Studio\code_templates and for Delphi 2006 you place it in \Program Files\Borland\BDS\4.0\Objrepos\code_templates\delphi.

Some of my templates expand automatically. If you find that annoying, (or would like another one to do that) change the following line near the top:

<template name=”namehere” invoke=”auto”>

to

<template name=”namehere” invoke=”manual”>

or vice versa.

Also, because there are many different permutations of property declarations, I used a naming convention (imagine!):

prop[i][f/c][f/c/nothing]

So they all start with prop, if your property is an array property, you add an i (for index) and then either f (for field) or c (for code) for each of the read and write specifiers.

If that only server to confuse you, some examples may help:

  1. propf is a read-only property that reads a private field.
  2. propicc is an array property with a get-function and a set-procedure.
  3. propfc is a property that reads from a field but writes to a procedure.
  4. and so on.

Enough talking. Here is a zip file with all twelve code templates. Feel free to use them any which way you please.

About these ads

4 Comments »

  1. Bruce McGee said

    It’s good to see more posts about live templates. IMHO, they’re a seriously under appreciated feature.

    Small correction; They were introduced in Delphi 2006.

    http://delphi.wikia.com/wiki/Delphi_Live_Templates

    • Agreed. They’ve saved me a lot of time.

      Corrected the wrong guess, thanks for the heads-up.

  2. [...] editor has learnt some interesting new tricks, including code templates. You really want that. And Syncedit. And regions. And XML [...]

  3. michael said

    If this was only the code I was looking, Thanks I might uses it for future code……

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: