Delphi code style conventions

zhaozj2021-02-11  205

Delphi code style conventions

QinGrui LI According to Charles Calvert's Object Pascal Style Guide Note

Note: Many people who use Delphi don't pay attention to the code style, often write some irregular code, so that others are ugly to understand and easy to make mistakes. I hope this article can encourage the majority of Delphi enthusiasts to unify the standard style and move together.

This article describes the standard style of formatting Delphi code. The secondary style is based on the Delphi development group coding convention. We admit that a lot of popular source code is different from our style, but we strongly recommend using the Borland style in the public source community.

Delphi is a beautiful language, an important one in the advantages is readability. This standard is an enhanced readability design. When developers follow this standard, all developers will benefit from unified readable styles. The effort to enhance the style of style can improve the value of the source program, especially in debugging and maintenance cycles.

Infixcaps naming style

Also called Camelcaps, the name consists of several expressions, the first letters of each word, the rest of the lowercase.

Such as: MyFile, ITTOSTR, ITEMS.

Delphi does not use Hungary representation, and the recommended name is clear. To use Appname instead of lpszappname, use Successful rather than BSuccess, with ClientRect instead of LPRECTCLIEntRect.

Source file naming

Use the infixcaps form. If you translate the C / C header file, use the same name as the original file. If you need to merge multiple headers into a unit, use the name of the main header file, such as WinBase.h, Windows.h merged into Windows.PAS.

Naming agreement

In addition to reserving words and instructions, other identifiers use INFIXCAPS style.

Note: Recently, it seems to use simple lowercase or abbreviated words to use simple lowercase or abbreviation words.

The exception is the identifier of the translation head file to retain the original style.

Type names are headed (Type's first letters)

Functions, process, method names use verbs or verb phrases, other identifiers use nouns or noun phrases.

Class data members take f head (Field's first letters)

Enumeration Type members crown to lowercase type abbreviation, general two letters, examples:

Tbitbtnkind = (BKCUSTOM, BKOK, BKCANCEL, BKHELP,

Bkyes, BKNO, BKCLOSE, BKABORT, BKRETRY,

Bkignore, bkall);

Return to the Boolean value of the Boolean, for example: IsVisible, IsResizable

Blank use

Insert a space line, separated by different parts of the code, such as class declarations, and function implementation rooms.

Use the space of the space: the right side of the punctuation, the binary operator

Whenever possible:

The method name and the subsequent left appendage. The front and rear one-membered opener and the left left brackets of the operance and the right bracket

Correct example:

Function TMYCLASS.MYFUNC (VARUE: Integer);

MyPointer: = @MyRecord;

Myclass: = TMYCLASS (MyPointer);

Myinteger: = MyintegeraRray [5];

Wrong example:

Function TMYCLASS.MYFUNC (VARUE: Integer);

MyPointer: = @ MyRecord;

Myclass: = TMYCLASS (MyPointer);

Myinteger: = MyintegeraRray [5];

indentation

It should be used to indent the indentation of two spaces, do not use TAB characters. Begin ... End code to indent, begin ... End itself does not indent

Continued two spaces during continuation

Correct example:

Function CreateWindowEx (DWEXStyle: DWORD;

LPClassName: pchar; lpwindowname: pchar;

DWStyle: DWORD; X, Y, NWIDTH, NHEIGHT: INTEGER;

HWNDPARENT: HWND; HMENU: HMENU; Hinstance: hinst;

LPPARAM: POINTER: hWnd; stdcall;

IF ((x = y) or (y = x) OR

(Z = p) or (f = j) THEN

Begin

S: = j;

END;

While (LONGEXPRESSION1 or LONGEXPIPRESSION2) DO

Begin

// DOSMETHING

// DOSMETHINGELSE;

END;

IF (LONGEXPIESSION1) OR

(LONGEXPRESSION2) OR

(Longexpression3) THEN

class

Class declaration

Data domain method properties

Access hierarchical organized order organization, but IDE automatically generated code is exception

Private Declarations Protected Declarations Public Declarations Published Declarations

Constructor and destructor declaration in front of the method declared. Since Tobject.destroy is a virtual function and TOBJECT.FREE calls Destroy, the destructor does not use other names. The constructor can use the name except CREATE, but it is generally best to unify CREATE.

example:

TMYCLASS = Class (TOBJECT)

Private

protected

public

Published

END;

Data should only be declared in private parties and heads with F (Field's first letters).

Type

TMYCLASS = Class (TOBJECT)

Private

FMYDATA: Integer;

Function GetData: Integer;

Procedure setData (Value: integer);

public

Published

Property MyData: Integer Readdata Write SetData;

END;

Interface follows the same rules

转载请注明原文地址:https://www.9cbs.com/read-5103.html

New Post(0)