Delphi coding standard - process and function

zhaozj2021-02-16  54

Process and functions

Named and format

(1) The process name should start with uppercase letters, and write interleaves to increase readability.

Here is an incorrect way:

Procedure thisisapoorlyformattedroutinename;

Change to this is right:

Procedure thisismuchmorereadableroutinename;

(2) The process name should make sense. The routine for performing an action is preferably prefixed in front of the name before the name is prefixed.

E.g:

Procedure formatharddrive;

Setting the autograph name of the input parameter value should be prefixed as a set.

E.g:

Procedure setusername;

Getting value routines should be prefixed as GET,

E.g:

Function GetUsername: String;

2.

(1) Format: As long as it is possible, the same type of shape should be classified together.

E.g:

Procedure ProcedureName (Param1, Param2, Param3: Integer; Param4: String);

(2) Name: All the names of all ginseng should express its use. If appropriate, the name of the parameter is preferably prefixed in letter A.

E.g:

Procedure procedurename (ausername: string; meanserage: integer);

The prefix A is necessary when the parameter name is with the characteristics or field of the class.

(3) Parameter sequence: The order in which the meticulin is mainly considering the register call rule. The most common parameters should be used as the first parameter, follow the left to right rows according to the frequency of use. The input parameter is before the output parameter. The large parameters should be placed before the range of small parameters.

E.g:

Procedure ProcedureName (Aplanet, Acontinent, Acountry, Astate, ACITY).

Some of the exceptions.

E.g:

During the event handling, the Snder parameter of the TOBJECT type is often the first parameter to be passed.

(4) Constant parameters: To make the record, array, short strings, or interface types of parameters cannot be modified by routines, the shape should be used in const. In this way, the compiler will generate a code in the most effective way to ensure that the passing parameters are not variable.

If other types of parameters are designed to be modified by the routine, C O N S is also marked. Although this has no effect on efficiency, this is more information to the caller's caller.

(5) Name the conflict: When the two units contain the same name, if the routine is called, the routine in the unit that appears later in the Uses clause is actually called. To avoid this, you can add the desired cell name before the method.

E.g:

SYSUTILS.FINDCLOSE (SR);

Windows.FindClose (Handle);

3. variable

(1) Named and format of the variable: The name of the variable should be able to express its use.

The loop control variable is often a single letter, such as I, J or K. More meaningful names can also be used, such as UserIndex;

The Boolean variable name must be clearly indicating the meaning of true and false values.

(2) Local variable: local variables are used inside the routine and follow the naming rules of other variables. If needed, the variable should be initialized immediately at the entrance of the routine.

The local Ansistring type variable is automatically initialized to an empty string;

The partial interface and the variable of the DISPINTERFACE type are automatically initialized to NIL;

The variables of local Variant and Olevariant type are automatically initialized to Unassigned.

(3) Global variables: generally do not encourage global variables. However, sometimes it needs to be used. Even so, global variables should be limited to the desired environment. Global variables may only be in the implementation part of the unit;

Global data If you will be used by many units, you should be used to use all objects in a common unit;

Global data can be directly initialized to a value when declaring.

(Note that all global variables are automatically initialized, so do not initialize global variables to null values ​​such as 0, nil, or unassigned. Zero initialization global variables are not space in the EXE file. Zero initialized data is saved In the virtual data segment, the virtual data segment is only assigned memory only when the application starts. The global data of non-zero started is in. EXE file is spaced.)

4. Types of

(1) Calculation rules: Type identifiers are reserved words, should be written. Win32 API types are often all capitalized and follow rules such as Windows.PAS or other API units for specific type names. For other variable names, the first letter should be capitalized, and the other letters are cases interlaced.

E.g:

VAR

MyString: String; // Reserved word

WindowsHandle: hwnd; // Win32 API type

I: integer; / / Type ID introduced in the S Y S Y T E M unit

(2) Floating point: Does not encourage the use of the REAL type because it is just reserved with old Pascal code. Typically, Double should be used for floating point numbers. Double can be optimized by processor, which is the standard data format defined by IEEE. You can use extend when needed than the range provided by Double. Extend is the type of Intel, Java does not support. When the physical byte number of floating point variables is important (possibly writing DLLs in other languages), you should use Single.

(3) Enumeration: Enumeration Type Name must represent the purpose of enumeration. The name is plus T character as a prefix before the name, indicating that this is a data type. The prefix of the enumeration type identifier list should contain 2 to 3 lowercase characters to associate each other.

E.g:

TsongType = (Strock, StClassical, StCountry, Stalternative, Stheavymetal, STRB);

The name of the modulus instance of the enumeration type is the same as the type, but there is no prefix t, or a variable is a more special name, such as: FavoritesongTPE1, FavoritesongTPE2, etc.

(4) VARIANT and OLVARIANT:

Variant and olevariant are generally not recommended. However, when the data type is only known in the run (often in the program of the COM and database applications), these two types are necessary to program. When COM programming such as an automated ActiveX control, Olevariant should be used; and Variant should be used for non-COM programming. This is because Variant can effectively save the native string of Delphi, while Olevariant converts all strings to an OLE string and does not reference counting functions.

5. Texture type

(1) Array type: The array type name should express the use of the array. Type names must add letter T to prefix. If you want to declare a pointer to the array type, you must add letter P to prefix, and the declaration is before the type declaration.

E.g:

Type

PcycleArray = ^ TcycleArray;

TcycleArray = array [1..100] of integer;

In fact, the variable instance of the array type is the same as the type name, but there is no T prefix. (2) Recording type: The record type name should express the use of records. Type names must add letter T to prefix. If you want to declare a meter pointing to the type of record, you must add the letter P as a prefix, and its declaration before the type declaration.

E.g:

Type

Pstudent = ^ TSTUDENT;

TSTUDENT = Record

StudentName: String;

STUBLE: Double;

6. class

(1) Name and format

The name of the class should express the use of class. The class name is previously adding letter T, indicating it is a type.

E.g:

Type

TSTUDENT = Class (TOBJECT);

The instance name of the class is the same as the class name, but there is no prefix T.

VAR

STUDENT: TSTUDENT;

Note about the naming of components, see 6.6 "Components".

(2) field

Naming and format: Field naming follows the same rules as variables, just add prefix f, indicating that this is a field.

Visibility: All fields must be private. If you want to access fields outside the scope of the class, you can be implemented by means of the properties of the class.

(3) method

Name and format: Names of methods follows the same rules as the process and functions.

Static method: When you don't want a method to be derived, the static method should be used.

Virtual Method and Dynamic Method: When you want a method to be overwritten, you should use a virtual method. If the class method is used directly or indirectly by multiple derived classes, the dynamic method should be used.

For example, a certain class contains a frequently covered method and has 100 faults, and the method should be defined as dynamic, which reduces the overhead of memory.

Abstract method: Do not use abstract methods if a class is to create an instance. The abstract method can only be used in the base class that never creates an instance.

Property Access Method: All attribute access methods should be defined in the private or protective part of the class. The attribute access method follows the same rules as the process and functions. The method for reading should be added to the G e t prefix, and the method used for writing should add a SET prefix, and there is a parameter called Value, which is the same as the attribute type.

E.g:

TSTUDENT = Class (TOBJECT)

Private

FNAME: STRING;

protected

Function GetName: String;

Procedure setName (Value: string);

public

Property Name: String Readname Write SetName

END;

(4) attribute

As a private field, the property follows the same naming rules as the field, but there is no F prefix. The property name should be noun, not the verb. The attribute is data, and the method is the action. The array attribute name should be a plural, and the general attribute should be a single number.

(5) Use of access method

Although not necessarily, but it is recommended that you use a write access method to access representative private field properties.

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

New Post(0)