2004. november 10., szerda

What Custom Compiler messages can do for you


Problem/Question/Abstract:

One of the problems of working in a large project is that you need sometimes to mark certain places of you’re source code, remaining you of things you need to do.
We can mark those places in the code, letting Delphi remind us of them.

Answer:

One of the problems of working in a large project is that you need sometimes to mark certain places of you’re source code, remaining you of things you need to do. There are a number of experts for Delphi that provide a to-do list, even one (nice one) that comes with Delphi.

One think that always bugs me about having a separated list of To-Do items is that you have to go and look for it. You can just as well forget all about it, and it loses it’s purpose. Wouldn’t it be nice if we could tell the compiler to generate a message each time a code with To-Do items is compiled?

Another thing is that a To-Do List is good for project level reminders, not a unit of even object level. You need a reference from the To-Do Item to the unit or object, some way of getting from the item to the code.

I find that using custom compiler messages, I can get another way to remind me of what not to forget. It allows me to overcome the two inconveniencies above, letting me insert reminders directly into the code and letting the compiler remind me of them.

The syntax for this command is

{$MESSAGE HINT|WARN|ERROR|FATAL 'text string' }

and some examples of it (taken from the Delphi help files) are:

{$MESSAGE 'Boo!'}emits a hint
{$MESSAGE Hint 'Feed the cats'}emits a hint
{$MESSAGE Warn 'Looks like rain.'}emits a warning
{$MESSAGE Error 'Not implemented'}emits an error, continues compiling
{$MESSAGE Fatal 'Bang.  Yer dead.'}emits an error, terminates compiler

One use of this directive that I find very useful is for TBD items – To Be Done. I use it to replace a To-Do list in this case. There are two advantages to this approach:

You get a compiler message for each item, reminding you to deal with it.
You can browse those messages with the Alt-F8, Alt-F7 keys.

A Code with this directive can look like (just an example from a code I am writing right now):

destructor TumbSelectionTempTable.Destroy;
begin
  // Clear the temp tables.
{$MESSAGE Warn ' - remember to free all allocated objects'}
  ClearAllOuterWorldFold;
  if FSubjectsTempTableCreated then
    DropTempTable(FTableName);

  FOuterWorldsFolded.Free;
  FQuery.Free;
  inherited;
end;

Nincsenek megjegyzések:

Megjegyzés küldése