The coding standard is mainly to provide a criteria for programming for the development group, so that the project developers have consistent formats when programming. In this way, the code written in the development group can be understood by others, thereby improving the maintenanceability of the code, making a set of many writings like one person, making the code more easily understood. This requires everyone to use the consistent code style.
Then, the reason why you often introduce these standards because some may not be familiar with Delphi's coding standard when new developers join the project development group.
These standards will be described here to introduce these criteria:
1 General source code format rules
2 procedures and functions
3 file, form and data module named
4 packs, components named general source code format rules
indentation
The indentation is two spaces in each stage. Do not place tabs in the source code. This is because the width of the tablet is different from different settings and code management utilities (printing, documentation and version control, etc.).
Margin
The margin is set to 80 characters. Sourcecodes generally do not exceed margins by writing a word, but this rule is more flexible. As long as it is possible, the statement that exceeds a row should be wrapped in a comma or operator. After the wrap, the two characters should be indensed.
brackets
There is no space between the left bracket and the next character. Similarly, there is no space in the right brackets and the previous characters.
The following example demonstrates correct and incorrect spaces.
CallProcedure (parameters); // is wrong!
CallProcedure (parameters); / / correct!
Reserved words and keywords
The reserved word and keywords for the Object Pascal language are always completely lowercase.
Begin ... End
The Begin statement must be occupied separately.
For example, the first line below is wrong, and the second line is correct:
For i: = 0 to 10 do begin
Statement
End // fault, Begin and FOR in the same line
For i: = 0 to 10 do // correct! Begin is in another line
Begin
Statement
end
A special case of this rule is that when Begin is part of the ELSE statement.
E.g:
IF condition the
Begin
Statement
end
Else Begin
STATEMENT;
end
End statement is always a line. When BeGin is not part of the ELSE statement, the corresponding End statement is the same as the reachable amount of the Begin statement.
Statement
(1) ife_then_lse statement
The most likely that the situation should be put in the THEN clause, and it is unlikely to place in the else clause.
To avoid many I F statements, you can use the CASE statement instead.
If more than 5, don't use the I F statement. Please use a clearer way.
Do not use excess parentheses in the I F statement. In the source code, parentheses only use when needed.
E.g:
IF (i = 42) THEN / / wrong, parentheses is redundant
IF (i = 42) or (j = 42) THEN / / correct, must be used in parentheses
If there are multiple conditions to be tested in the I F statement, you should follow the right to left. This allows the code to make full use of the short-circuit estimation logic of the compiler. If condition1 is fast than Condition2, the construct2 is fast than Condition3, then the IF statement should construct this: if Conditior1 and constructing2 and condition3 Then
(2) Case_ELSE statement
Constants for each situation in the case statement should be arranged in the order of numbers or letters.
The action statement of each situation should be short and usually no more than 4 to 5 lines of code. If the action is too complicated, the code should be placed separately in a process or function. The ELSE clause of the CASE statement is only used for default or error detection.
(3) While statement
It is recommended not to use the EXIT process to exit the While loop. If necessary, you should use a loop condition to exit the loop. All code to initialize the W H i L E loop should be located before the W H i L E entry and not unrelated statements. Any business auxiliary work should be carried out immediately after cycling. (4) for statement
If the number of cycles is determined, use the for statement to replace the While statement.
(5) REPEAT statement
The REPEAT statement is similar to the While loop and follows the same rules.
(6) WITH statement
WITH statement should be careful. To avoid excessive use of the WITH statement, especially in the with statement, multiple objects or records.
E.g:
With record1, record2 do
These situations are easy to confuse programmers and lead to difficulties in debugging.
Structured abnormal processing
Abnormal processing is mainly used to correct errors and protection resources. This means that anywhere to allocate resources must be used to ensure that resources are released. However, if it is an exception to the resource in the initial / end portion of the unit or the object of the object.
(1) TRY ... FINALLY usage
In a possible case, each resource allocation should match the Try ... Finally structure.
E.g:
// The following code may cause an error
SomeClass1: = TsomeClass.create;
SomeClass2: = TsomeClass.create;
Try
{do some code}
Finally
Someclass.free;
Someclass.free;
e n d;
// A security scheme for the above resource allocation is:
SomeClass1: = TsomeClass Create;
Try
Someclass2: = TsomeClass Create;
Try
{do some code}
Finally
Someclass2.free;
END;
Finally
Someclass1.free;
END;
(2) Try ... Except's usage
If you want to perform some tasks when an exception occurs, you can use Try ... Except. Usually, it is not necessary to use Try ... Except for simply displaying an error message because the Application object can automatically do this according to the context. If you want to activate the default abnormality processing in the clause, you can trigger an exception again.
(3) Try ... Except ... Else
Try ... Except with Else clauses is not encouraged because this will block all anomalies, including you are not ready to process an exception.