The feeling found online is very inspiring to beginners and collect it!
No.1 Determine logic type}
var B: Boolean;
Begin
B := Boolean(2); //This is just for debugging//B := True;
if B = True then ShowMessage('B = True'); //Not recommended//Unsafe
//////////////////
if B then ShowMessage('B'); //Suggestions//Short
end;
var B: Boolean;
Begin
if Edit1.Text = 'Yes' then //Not recommended//Trifty
B := True
else B := False;
//////////////////
B := Edit1.Text = 'Yes'; //Suggested//Short
end;
{ No.2 Temporary SQL Query}
Begin
QueryTemp.Close;
QueryTemp.SQL.Text := 'SELECT SUM(Amount) AS Total FROM Sales Table';
QueryTemp.Open; //Not recommended//Data is not closed, resulting in waste of resources
ShowMessage(Query1.FieldByName('Total').AsString);
///////
QueryTemp.SQL.Text := 'SELECT SUM(Amount) AS Total FROM Sales Table';
QueryTemp.Open;
ShowMessage(Query1.FieldByName('Total').AsString);
QueryTemp.Close; //It is recommended to use //Close it after use
end;
{ No.3 Get the number of records}
var
vRecordCount: Integer;
Begin
Query1.SQL.Text := 'SELECT * FROM Table1'; //Not recommended//Seriously waste resources and will obtain a lot of unnecessary information
Query1.Open;
vRecordCount := Query1.RecordCount;
Query1.Close;
///////
Query1.SQL.Text := 'SELECT COUNT(*) AS record number FROM Table1'; //Suggestions//Fast and effective, only one record is processed
Query1.Open;
vRecordCount := Query1.FieldByName('Records').AsInteger;
Query1.Close;
ShowMessage(IntToStr(vRecordCount));
end;
{ No.4 field assignment}
Begin
Table1.Edit;
Table1.FieldByName('Name').AsString := Edit1.Text; //Not recommended
Table1.FieldByName('date').AsDateTime := Date;
///////
Table1['Name'] := Edit1.Text; //Suggestions//Short and good expansion
//Table1.Fieldvalues['Name'] := Edit1.Text; // Method suggested by Borland. and Paramvalues[]
Table1['Date'] := Date;
end;
{ No.5 Using Self Pointer}
Begin
Edit1.Parent := Form1; //Not recommended //Form1 is just a variable//What should I do if no resources are allocated?
//////////////////
Edit1.Parent := Self; //Suggestions
end;
{ No.6 traversal of dataset}
var
I: Integer;
Begin
Query1.First;
for I := 0 to Query1.RecordCount - 1 do begin //Not recommended //Easy to be affected
Query1.Next;
{};
end;
///////
Query1.First;
While not Query1.Eof do begin //Suggestions
{ }
Query1.Next;
end;
end;
{ No.7 Use Sender parameters to make the code common}
PRocedure TForm1.Edit1Change(Sender: TObject);
Begin
if Edit1.Text = '' then //Not recommended
Edit1.Color := clRed;
//////////////////
if TEdit(Sender).Text = '' then //Suggested//It is very convenient to copy it into EditXChange
TEdit(Sender).Color := clRed;
end;
{ No.8 Use default conversion function}
var
I: Integer;
Begin
I := StrToInt(Edit1.Text); //Not recommended
//////////////////
I := StrToIntDef(Edit1.Text, 0);//Suggestions//Reference StrToFloatDef, StrToDateDef... But these are only available in Delphi6.
end;
{ No.9 traversal array}
var
I: Integer;
A: array[0..9] of Integer;
Begin
for I := 0 to 9 do //Not recommended
A[I] := I;
//////////////////
for I := Low(A) to High(A) do //Suggested//Good expansion
A[I] := I;
end;
{ No.10 utilizes MaxInt constant}
Begin
Caption := Copy(Edit1.Text, 3, Length(Edit1.Text) - 3 + 1); //Not recommended
//////////////////
Caption := Copy(Edit1.Text, 3, MaxInt); //Suggested//Hehehe, calculate once less
end;
{ No.11 Result function pointer}
function FuncName: Boolean;
Begin
FuncName := True; //Not recommended // and cannot be used as a normal variable on the right side of the assignment number
//////////////////
Result := True; //Suggestions//Good expansion
end;
function FuncSum(A: array of Integer): Integer;
var I: Integer;
Begin
Results := 0;
for I := Low(A) to High(A) do
Result := Result + A[I]; //FuncSum := FuncSum + A[I] cannot be used;
end;
{ No.12 The code that must be executed, use try ... finally ... end statement}
var
vStringList: TStringList;
Begin
vStringList := TStringList.Create;
vStringList.LoadFromFile('c:/temp.txt');
ShowMessage(vStringList.Text);
vStringList.Free; //Not recommended//If an exception occurs, the resource will not be released
//////////////////
vStringList := TStringList.Create;
try
vStringList.LoadFromFile('c:/temp.txt');
ShowMessage(vStringList.Text);
Finally //Suggestions//Enforce it even if Exit appears
vStringList.Free;
end;
end;
//Other cases 1
Begin
Screen.Cursor := crHourGlass;
try
{Time-consuming operation}
Finally
Screen.Cursor := crDefault;
end;
end;
//Other cases 2
Begin
Query1.DisableControls;
try
{Operational dataset}
Finally
Query1.EnableControls;
end;
end;