qingrui li Compiled and annotated based on Charles Calvert's Object Pascal Style Guide
Note: Many people who use Delphi do not pay attention to coding style and often write non-standard code, which makes it difficult for others to understand and prone to errors. I hope this article can encourage the majority of Delphi enthusiasts to unify to the standard style and move forward together.
This article describes the standard style for formatting Delphi code. This sub-style is based on the coding conventions of the Delphi development team. We acknowledge that many popular source code uses a style different from ours, but we strongly recommend the use of the Borland style in the public source code community.
Delphi is a beautiful language, and one of its most important advantages is readability. This standard is designed to enhance readability. When developers follow this standard, all developers will benefit from a consistent and readable style. Efforts to enforce style standards can increase the value of source programs, especially during debugging and maintenance cycles.
InfixCaps naming style
Also called CamelCaps, the name consists of several words that express meaning. The first letter of each word is capitalized and the rest are lowercase.
Such as: MyFile, IntToStr, Items.
Delphi does not use Hungarian notation and recommends meaningful names. For example, use AppName instead of lpszAppName, use Successful instead of bSuccess, and use ClientRect instead of lPRectClientRect.
Source file naming
Use InfixCaps form. If transpiling a C/C++ header file, use the same name as the original header file. If multiple header files need to be merged into one unit, use the name of the main header file, such as: WinBase.h, Windows.h are merged into Windows.pas.
naming convention
Except for reserved words and directive words, which are all lowercase, other identifiers use the InfixCaps style.
Note: There seems to be a recent trend toward using simple lowercase or abbreviations for local variables and formal parameters.
The exception is that identifiers in transpiled header files retain their original style.
Type names all start with T (the first letter of Type)
Function, procedure, and method names use verbs or verb phrases, and other identifiers use nouns or noun phrases.
Class data members start with F (the first letter of Field)
Enumeration type members are preceded by a lowercase type abbreviation, usually two letters, for example:
TBitBtnKind = (bkCustom, bkOK, bkCancel, bkHelp, bkYes, bkNo, bkClose, bkAbort, bkRetry, bkIgnore, bkAll);
Functions that return Boolean values begin with Is, for example: IsVisible, IsResizable
Use of white space
Insert blank lines at appropriate locations to separate different parts of the code, such as between class declarations, function implementations, etc.
Where to use spaces: To the right of punctuation, on either side of binary operators
Where spaces should not be inserted:
Correct example:
function TMyClass.MyFunc(var Value: Integer);MyPointer := @MyRecord;MyClass := TMyClass(MyPointer);MyInteger := MyIntegerArray[5];
Error example:
function TMyClass.MyFunc( var Value: Integer ) ;MyPointer := @ MyRecord;MyClass := TMyClass ( MyPointer ) ;MyInteger := MyIntegerArray [ 5 ] ;
indentation
Two-space indentation should be used and no tab characters should be used.
The code between begin...end should be indented, and begin...end itself should not be indented.
Indent two spaces when continuing lines
Correct example:
function CreateWindowEx(dwExStyle: DWord; lpClassName: PChar; lpWindowName: PChar; dwStyle: DWORD; if ((X = Y) or (Y = X) or (Z = P) or (F = J) thenbegin S := J;end;
while (LongExpression1 or LongExpression2) do begin // DoSomething // DoSomethingElse;end;if (LongExpression1) or (LongExpression2) or (LongExpression3) then
kind
Class declarations are organized in the following order
data field
method
property
The access hierarchy is organized in the following order, with the exception of code automatically generated by the IDE.
Constructors and destructors are declared before the method declaration. Since TObject.Destroy is a virtual function and TObject.Free calls Destroy, do not use other names for the destructor. The constructor can use a name other than Create, but it is generally best to use Create.
example:
TMyClass = class(TObject) private protected public published end;
Data should only be declared in the private section and start with F (the first letter of Field).
type TMyClass = class(TObject) private FMyData: Integer; function GetData: Integer; procedure SetData(Value: Integer); public published property MyData: Integer read GetData write SetData; end;
Interfaces follow the same rules