Procedures and Functions 1. Naming and Format (1) Procedure names should start with an uppercase letter, and the uppercase and lowercase letters should be staggered to increase readability. The following is an incorrect way of writing: PRocedure thisisapoorlyformattedroutinename; change it to this: procedure ThisIsMuchMoreReadableRoutineName; (2) The process name should be meaningful. A routine that performs an action is best prefixed to its name with the verb that represents the action. For example: procedure FormatHardDrive; The routine name that sets the input parameter value should be prefixed with Set, for example: procedure SetUserName; The routine name that gets the value should be prefixed with Get, for example: function GetUserName: string; 2. Formal parameters ( 1) Format: Whenever possible, formal parameters of the same type should be grouped together. For example: procedure ProcedureName (Param1, Param2, Param3: Integer; Param4: string); (2) Naming: The names of all formal parameters should express their purpose. If appropriate, formal parameter names should be prefixed with the letter A. For example: procedure ProcedureName (AUserName: string; AUserAge: integer); When the parameter name has the same name as a class attribute or field, the prefix A is necessary. (3) Parameter order: The order of formal parameters mainly depends on the register calling rules. The most commonly used parameter should be the first parameter, arranged from left to right in order of frequency of use. Input parameters precede output parameters. Parameters with a large range should be placed before parameters with a small range. For example: procedure ProcedureName (APlanet, AContinent, ACountry, AState, ACity). Some are exceptions. For example: During event processing, the Sender parameter of type TObject is often the first parameter to be passed. (4) Constant parameters: To prevent parameters of record, array, short string or interface type from being modified by the routine, the formal parameters should be marked as Const. In this way, the compiler will generate code in the most efficient way, ensuring that the passed parameters are immutable. If other types of parameters are not expected to be modified by the routine, they can also be marked with C onst. Although this has no impact on efficiency, it gives the caller of the routine more information. (5) Naming conflict: When two units contain routines with 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 situation, you can add the desired unit name before the method name, for example: SysUtils.FindClose (SR);Windows.FindClose(Handle);3. Variables (1) Naming and format of variables: The name of a variable should be able to express its purpose. Loop control variables are often single letters such as I, J, or K. You can also use a more meaningful name, such as UserIndex; Boolean variable names must clearly indicate the meaning of the True and False values. (2) Local variables: Local variables are used inside routines and follow the naming rules of other variables. If necessary, variables should be initialized immediately at the entry of the routine. Local AnsiString type variables are automatically initialized to empty strings; local interface and dispinterface type variables are automatically initialized to nil; local Variant and OleVariant type variables are automatically initialized to Unassigned. (3) Global variables: The use of global variables is generally discouraged. However, sometimes it is needed. Even so, global variables should be restricted to the environments where they are needed. Global variables may be global only in the implementation part of the unit; global data, if it will be used by many units, should be moved to a common unit and used by all objects; global data can be directly initialized to a value when declared. (Note that all global variables are automatically zero-initialized, so do not initialize global variables to null values such as 0, nil, or Unassigned. Zero-initialized global variables take up no space in the . EXE file. Zero-initialized data is saved in In the virtual data segment, the virtual data segment only allocates memory when the application is started. Non-zero initialized global data takes up space in the . EXE file) 4. Type(1) Case rules: Type identifiers are reserved words and should be all lowercase. Win32 API types are often all-capitalized and follow rules for specific type names such as Windows.pas or other API units. For other variable names, the first letter should be capitalized and other letters should be in alternating case. For example: varMyString: string; // Reserved word WindowsHandle: HWND; // Win32 API type I: Integer; // Type identifier introduced in System unit (2) Floating point type: The use of Real type is discouraged because it is just Reserved for compatibility with older Pascal code. Normally, Double should be used for floating point numbers. Double can be optimized by the processor and is a standard data format defined by IEEE. Extend can be used when a larger range than Double provides is required. Extend is an Intel-specific type and is not supported by Java. When the physical number of bytes of the floating point variable is important (perhaps using a different language to write the DLL), Single should be used. (3) Enumeration type: The enumeration type name must represent the purpose of the enumeration. The T character must be prefixed before the name to indicate that this is a data type. The prefix of the identifier list of the enumeration type should contain 2 to 3 lowercase characters to associate with each other. For example: TSongType = (stRock, stClassical, stCountry, stAlternative, stHeavyMetal, stRB); The name of the variable instance of the enumeration type is the same as the type, but without the prefix T. You can also give the variable a more special name, such as: FavoriteSongTpe1, FavoriteSongTpe2, etc. wait. (4)Variant and OleVariant: It is generally not recommended to use Variant and OleVariant. However, these two types are necessary for programming when the data type is known only at runtime (often in COM and database applications). When doing COM programming such as automating ActiveX controls, you should use OleVariant; for non-COM programming, you should use Variant. This is because Variant can effectively save Delphi's native strings, while OleVariant converts all strings to OLE strings (ie, Wide Char strings) without reference counting. 5. Constructed type (1) Array type: The array type name should express the purpose of the array. Type names must be prefixed with the letter T. If you want to declare a pointer to an array type, you must prefix it with the letter P and declare it before the type declaration. For example: typePCycleArray = ^TCycleArray; TCycleArray=array [1..100] of integer; In fact, the variable instance of the array type has the same name as the type, but without the T prefix. (2) Record type: The record type name should express the purpose of the record. Type names must be prefixed with the letter T. If you want to declare a pointer to a record type, you must prefix it with the letter P and declare it before the type declaration. For example: typePStudent = ^ TStudent;TStudent = recordStudentName: string;StudentAge: Double;6. Classes (1) Naming and Format The name of a class should express the purpose of the class. The letter T is added before the class name to indicate that it is a type. For example: typeTStudent= class (TObject); The instance name of the class is the same as the class name, but without the prefix T. varStudent: TStudent; Note that regarding the naming of components, please refer to Section 6.6 "Components". (2) Field naming and format: The naming of fields follows the same rules as variables, except that the prefix F is added to indicate that this is a field. Visibility: All fields must be private. If you want to access a field outside the scope of a class, you can do so with the help of class attributes. (3) Method naming and format: The naming of methods follows the same rules as procedures and functions. Static methods: Static methods should be used when you do not want a method to be overridden by derived classes. Virtual methods vs. dynamic methods: When you want a method to be overridden by a derived class, you should use virtual methods. If a class method is to be used directly or indirectly by multiple derived classes, dynamic methods should be used. For example, if a class contains a frequently overridden method and has 100 derived classes, the method should be defined as dynamic, which can reduce memory overhead. Abstract methods: If a class is to create instances, then do not use abstract methods. Abstract methods can only be used in base classes that never create instances. Property access methods: All property access methods should be defined in the private or protected part of the class. Property access methods follow the same rules as procedures and functions. The method used for reading should be prefixed with Get, and the method used for writing should be prefixed with Set, and have a parameter called Value whose type is the same as the type of the attribute. For example: TStudent = class (TObject)privateFName: string;protectedfunction GetName: string;procedure SetName (Value: string);publicproperty Name: string read GetName write SetName;end;(4) Property properties serve as accessors to private fields and follow the same The fields have the same naming rules, but without the F prefix. Property names should be nouns, not verbs. Properties are data and methods are actions. Array property names should be plural, while general properties should be singular. (5) Although the use of access methods is not required, it is recommended that you use write access methods to access properties representing private fields.