Coding standards mainly provide the development team with a programming guideline so that project developers have a consistent format to follow when programming. In this way, the code written by each programmer in the development team can be understood by others, thereby improving the maintainability of the code, making a set of software written by multiple people as if it was written by one person, making the code easier to understand. This requires everyone to use a consistent coding style. So, the reason why it is cliché to introduce these standards is because when new developers join the project development team, some may not be familiar with Delphi's coding standards. These standards will be introduced here in the following categories: 1. General source code format rules 2. Procedures and functions 3. File, form and data module naming 4. Package and component naming General source code format rules Indentation is for each level There are two spaces between them. Don't place tab characters in source code. This is because the width of the tab character varies with different settings and code management utilities (printing, documentation, version control, etc.). Margins Margins are set to 80 characters. Source code generally does not exceed the margin by writing a word, but this rule is more flexible. Whenever possible, statements longer than one line should be wrapped with commas or operators. After a line break, it should be indented by two characters. Brackets have no space between the opening bracket and the next character. Likewise, there is no space between the closing bracket and the previous character. The following example demonstrates correct and incorrect whitespace. CallPRocedure(Parameters); // Wrong! CallProcedure (Parameters); // Correct! Reserved words and keywords Object The reserved words and keywords of Pascal language are always completely lowercase. The begin...endbegin statement must be on a separate line. For example, the first line below is wrong, but the second line is correct: for i:=0 to 10 do beginStatement end// Wrong, begin is on the same line as for for i:=0 to 10 do //Correct! begin in A special case of this rule in beginStatementend on another line is when begin is part of an else statement. For example: if Condition thenbeginStatement endelse beginStatement; the endend statement is always on a separate line. When begin is not part of an else statement, the corresponding end statement is indented by the same amount as the begin statement. Statement (1) The most likely situation of the if_then_else statement should be placed in the then clause, and the unlikely situation should be placed in the else clause. To avoid many if statements, use case statements instead. If there are more than 5 levels, don't use if statements. Please use a clearer method instead. Don't use extra parentheses in if statements. In source code, parentheses are used only when really needed. For example: if (I=42) then // Wrong, parentheses are redundant if (I=42) or (J=42) then // Correct, parentheses must be used If there are multiple conditions to test in the if statement, you should Arrange from right to left in order of computational complexity. This allows the code to take full advantage of the compiler's short-circuit estimation logic. If Condition1 is faster than Condition2 and Condition2 is faster than Condition3, the if statement should be constructed like this: if Condition1 and Condition2 and Condition3 then(2) case_else statement The constants for each case in the case statement should be arranged in numerical or alphabetical order. The action statement for each situation should be short and usually no more than 4 to 5 lines of code. If the action is too complex, the code should be placed in a separate procedure or function. The else clause of a case statement is only used for default cases or error detection. (3) The while statement recommends not to use the exit process to exit the while loop. If necessary, a loop condition should be used to exit the loop. All code that initializes the while loop should be located before the while entry and should not be separated by irrelevant statements. Any auxiliary work for the business should be carried out immediately after the cycle. (4) for statement If the number of loops is determined, the for statement should be used instead of the while statement. (5) repeat statement The repeat statement is similar to a while loop and follows the same rules. (6) with statement The with statement should be used with caution. Avoid overuse of with statements, especially when using multiple objects or records within a with statement. For example: with Record1, Record2 do these situations can easily confuse programmers and make debugging difficult. Structured Exception HandlingException handling is mainly used to correct errors and protect resources. This means that wherever resources are allocated, try...finally must be used to ensure that the resources are released. However, exceptions are made if resources are allocated/released in the initial/final part of the unit or in the constructor/destructor of the object. (1) Usage of try...finally Whenever possible, each resource allocation should match the try...finally structure. For example: //The following code may cause errors SomeClass1: = TSomeClass.Create;SomeClass2: = TSomeClass.Create;try{do some code}finallySomeClass.Free;SomeClass.Free;end;//A safe solution for the above resource allocation is: SomeClass1: = TSomeClass Create;trySomeClass2: = TSomeClass Create;try{do some code}finallySomeClass2.Free;end;finallySomeClass1.Free;end;(2) Usage of try...except If you want to perform some tasks when an exception occurs, you can use try...except. Usually, there is no need to use try...except to simply display an error message, because the application object can do this automatically based on the context. If you want to activate default exception handling in the clause, you can trigger the exception again. (3) The usage of try...except...else is discouraged from using try...except with an else clause, because this will block all exceptions, including exceptions that you are not prepared to handle.