Based on the VCL code and some of my experiences over the past two years, we can summarize a set of commonly used naming rules in Delphi.
- Type)
- Including class, record, enumeration, procedure/function variables, and Other types written in the Type Definition
- Start with a capital T, such as tlist (class), tpoint (record), talign (enumeration), tpolicyevent (Procedure variable)
- Constant (const)
- We recommend that you start with C for custom simple type constants (such as integer and pointer ).
- There are also many constants that need to be inferred based on the meaning of the word, such as maxdatetime (starting with Max, let people guess from the meaning of a constant)
- String constants start with S (because the processing method of string constants and simple type constants is different during compilation, For details, refer to "Delphi source code analysis" 2.2.4 constant)
- Private member variable of the class, global variable under implementation
- Start with F
- Do not show public member variables in the class. All attributes to be made public are represented by property, procedure, and function.
- Global variables under the interface
- It is better not to show global variables in the interface. You can replace them with the global variable value returned by the function in the interface.
- If yes, start with G.
- Local variable
- Function (procedure or function) real parameters
In addition to this, I have some of my usual Delphi programming habits and share them with you. You are welcome to shoot bricks.
- Start with a project abbreviation
- Since namespace in C ++ or package in Java does not exist in Delphi, the name conflict problem is serious. To avoid this problem, you can add the abbreviation of the project name before the global type and constant of a project. For example, for a project called myproject, the exclusive class can be called tmponeclass, and the constant can be called cmponenumber.
- Replace global variables with functions
- When we need to access the clipboard, we will use the clipboard object. I wonder if you have noticed that this is actually a function that returns the tclipboard object, and we also use the singleton mode in the design mode.
- Since no parameter function can be called in Delphi without parentheses, a function can often be used as a variable. How to Use it can be used in a different way? Singleton is just one example.
- Use the Pascal naming method
- These naming methods have evolved from the Pascal naming method.
- The basic idea of Pascal's naming method is that if a name contains multiple words, the first letter of each word must be capitalized, for example, thisanexample.
- It is not recommended to use the underline naming method (such as this_is_an_example), because the variable name auto-completion tool of Delphi does not automatically filter the underline, and manual input is too tired...
- In Delphi, variable names are not case sensitive (for example, a and a are actually the same variable, which is the biggest difference with C languages ), therefore, do not try to distinguish variables with uppercase or lowercase letters. It is best to start all variables with uppercase letters.