Guidelines for Writing Delphi Programs

Over the years, I have used components written by several companies. There are a number of problems that seem to be repeated - this lists a few the are easy to avoid.

Existing Style Guides | Use columns | if..then..else | Long Strings | Underscores | *with* Statement | Naming Conventions


Existing Style Guides

There are several design guidelines available online - such as Though both of these are pretty good (and well worth reading), I suggest the following modifications.


Use columns

The Borland guidelines suggest using only a single space on each side of an assignment statement and do not allow any spaces on either side of a period that is used to separate properties and methods from the object identifier.

I disagree - spaces can be used to improve readability. Consider these

In general, Borland follows their guidelines, but occasionally, even they line up assignment statements.

I suggest that any time spaces can be added to make code easier to read ... do it.

On the other hand, when only a single space is used before the colon, then searching for an assignment statement becomes much easier - it is nearly impossible with a variable number of spaces.


if..then..else

The Borland guidelines suggest using separate lines for begin and end - I think that this is a waste of space.


Long Strings

The Borland guidelines specifically state not to start a line with an operator. I disagree - I frequently find it easier to read code that starts with operators. In long string concatenations like these, I frequently debug by commenting out one of the strings. In the first case, if the ORDER BY string is removed, I have to remember to also remove the plus sign from the previous string ... and to replace it when the string is reactivated. Another nice touch is placing the semicolon on a separate line ... for much the same reason.

I also do this with long if statements that require multiple lines - except in this case I start the lines with and or or and line up the parameters.


Underscores

I don't know why the guidelines continuously remind the developer NOT to use underscores in variable names. Specifically, I find that their use makes for much more readable code.


*with* Statement

The with statement can be used to implicitly associate properties and methods with an object. The Delphi help indicates that this construct will also make the program run faster.

However, the with statement makes development and debug much more difficult.


Naming Conventions

Generally, I don't believe in strong naming conventions, but I do believe in readable code.


General Naming Conventions

Delphi is not case-sensitive.

The following definitions are followed but not enforced

"A good guideline is that method names have verbs in them. If you find that you create a lot of methods that do not have verbs in their names, consider whether those methods ought to be properties." from the Delphi Help

Most property methods start with either Set or Get; events start with On.


User Interface Components

User Interface Components

Menus

Other form components that are part of a larger group should follow a similar format, including


Custom DialogBoxes and Sub-Forms

Custom DialogBoxes and Sub-Forms

Forms are actually a bit more complicated - they have both a form name and a unit name (which is also used to name the *.dfm & *.pas files) ... and these two names must be different. In most cases, this is no big deal since an application has only one form However, when an application contains lots of forms, .... (I am still working on this - 08-30-06)


Other References

Also see my NamingConventions page.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Delphi / Style_Guides.html