Version 2.1, updated 2025-10-08
This comprehensive style guide establishes conventions for modern Delphi projects across formatting, naming, structure, and coding practices.
Indentation & Line Length:
- Use 2 spaces per logical block (avoid tabs)
- Maximum 120 characters per line
begin..endstatements on separate lines
Comments:
//for single-line comments{}for multi-line comments(* *)for temporarily disabled code///for XML documentation
| Element | Convention | Example |
|---|---|---|
| Variables (local) | L prefix + PascalCase | LCustomerName |
| Variables (field) | F prefix + PascalCase | FConnectionString |
| Parameters | A prefix + PascalCase | AValue |
| Loop counters | lowercase, no prefix | i, j (exception to rules) |
| Constants | c prefix; sc for strings | cMaxRetries, scErrorMsg |
| Types/Classes | T prefix + PascalCase | TCustomer |
| Interfaces | I prefix + PascalCase | ILogger |
| Exceptions | E prefix + PascalCase | EFileNotFound |
| Methods | Verb + PascalCase | SaveDocument, IsValidEmail |
Namespace Hierarchy with Dot Notation:
- Forms:
Main.Form→ fileMain.Form.pas→ classTFormMain - Data Modules:
Main.DM→ fileMain.DM.pas→ classTDMMain - Nested:
Customer.Details.Form→TFormCustomerDetails
Always use FreeAndNil() instead of .Free:
try
LObject := TObject.Create;
finally
FreeAndNil(LObject);
end;- Collections: Use
TArray<T>for fixed sizes,TList<T>for dynamic lists,TObjectList<T>for owned objects - Inline variables (Delphi 10.3+) preferred in loops
- Sealed classes for utility functions
- Generics for type-safe collections
- Anonymous methods for callbacks
Use XML documentation comments with ///:
/// <summary>
/// Calculates the sum of two numbers
/// </summary>This guide emphasizes consistency, maintainability, and modern Delphi practices.