General Rules: Identifiers, Keywords, Indentation

From RAD Studio
Jump to: navigation, search

Go Up to Delphi’s Object Pascal Style Guide

Three general rules always apply to Object Pascal code, but before we get into the details of the various specific cases, there is a general recommendation: when picking a name, be descriptive; self-document code by using good naming and structure. Prefer plain words to cryptic acronyms and shortened versions.

Without exceeding in length, a slightly longer and more descriptive name is generally better than a condensed version. As part of a longer name, it is better to use ObjectPointer than ObjPtr, String over Str; while Int and Bool are readable enough, you might want to avoid spelling them out as Integer and Boolean.

Camel Casing

While there are specific rules in each case, all identifiers in ObjectPascal should follow the general rule of using Pascal Casing (aka, InfixCaps), a variation of Camel Casing. See Camel Case for more information. Do not use underscores to separate words.

Following this rule, a symbol referring to “my name” is written as MyName.

Header Translations Exceptions. The major exception to this rule is in the case of header translations, which should always follow the conventions used in the header. For instance, write WM_LBUTTONDOWN, not Wm_LButtonDown. It is OK to use underscores to separate words only in header translations - that is, translations converting from another language using different naming conventions. Similarly, do not use all caps for const declarations, except where required in header translations. When writing a translation from another language with other conventions, retain those conventions in your Delphi translation, as the Windows API header translations do.

No Hungarian. The original document had this ironic comment (a more realistic reason is that adding the variable type as a prefix is redundant information, which might cause issues when variable type changes and variable name no longer matches it):

Delphi is created in California, so we discourage using Hungarian notation, except where required in header translations. Correct: FMyString, Incorrect: lpstrMyString.

Note: Internal Embarcadero code should have identifiers written using US-ASCII characters only and no Unicode symbols (this rule also applies to comments and other language elements).

Lowercase Keywords

Language keywords, reserved words, and compiler directives should all be in lowercase.

This can be confusing at times given types such as Integer are just identifiers, and appear with a first capital letter, while strings are declared with the reserved word string, which should be all lowercase.

Besides string, another keyword occasionally written incorrectly is nil. At the same time Self is an identifier defined in the context of the methods of a class or record, not a reserved word, and should be written accordingly.

//Correct
begin
uses
Self
// Incorrect
Begin
USES

Two Spaces Indentation

Basic indentation is two spaces or multiple of 2 spaces (4, 6, 8, and so on). Do not use tabs. The Delphi IDE, as with most editors, has a setting to use spaces instead of tabs. In Delphi, this is on by default. Leave this on.

Next

Source Code Files, Units, and Their Structure