Recommended statements in Delphi
{No.1 determining logical type}
VaR B: Boolean;
Begin
B: = Boolean (2); // This is only for debugging. // B: = true;
If B = true then showmessage ('B = true'); // not recommended // insecure
///////
If B then showmessage ('B'); // recommended // brief
End;
VaR B: Boolean;
Begin
If edit1.text = ''is ''then // not recommended // cumbersome
B: = true
Else B: = false;
///////
B: = edit1.text = ''is''; // recommended // 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 // The data is not closed, resulting in a 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 that you disable it after use.
End;
{Number of records retrieved by No. 3}
VaR
Vrecordcount: integer;
Begin
Query1. SQL. Text: = ''select * From table1''; // not recommended // seriously wasting resources, resulting in a lot of unnecessary information
Query1.open;
Vrecordcount: = query1.recordcount;
Query1.close;
/////
Query1. SQL. Text: = ''select count (*) as record count from table1'; // we recommend that you quickly and efficiently process only one record.
Query1.open;
Vrecordcount: = query1.fieldbyname (''number of records ''). asinteger;
Query1.close;
Showmessage (inttostr (vrecordcount ));
End;
{Assignment of field no. 4}
Begin
Table1.edit;
Table1.fieldbyname (''name''). asstring: = edit1.text; // not recommended
Table1.fieldbyname (''date'). asdatetime: = date;
/////
Table1 [''name'']: = edit1.text; // recommended // short, good scalability
// Table1.fieldvalues [''name'']: = edit1.text; // recommended Borland method. And paramvalues []
Table1 ['''date']: = date;
End;
{No. 5 Use self pointer}
Begin
Edit1.parent: = form1; // not recommended // form1 is just a variable // What if no resource is allocated?
///////
Edit1.parent: = self; // recommended
End;
{No. 6 traversing a dataset}
VaR
I: integer;
Begin
Query1.first;
For I: = 0 to query1.recordcount-1 do begin // not recommended // easily affected
Query1.next;
{};
End;
/////
Query1.first;
While not query1.eof do begin // recommended
{}
Query1.next;
End;
End;
{No. 7 generic code using the sender parameter}
Procedure tform1.edit1change (Sender: tobject );
Begin
If edit1.text = ''' then // not recommended
Edit1.color: = clred;
///////
If tedit (sender). Text = ''' then // recommended // it is convenient to copy to editxchange.
Tedit (sender). Color: = clred;
End;
{No. 8 use the default conversion function}
VaR
I: integer;
Begin
I: = strtoint (edit1.text); // not recommended
///////
I: = strtointdef (edit1.text, 0); // recommended // refer to 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 // recommended // good scalability
A [I]: = I;
End;
{No. 10 using maxint constants}
Begin
Caption: = copy (edit1.text, 3, length (edit1.text)-3 + 1); // not recommended
///////
Caption: = copy (edit1.text, 3, maxint); // recommended // you can calculate the value once less.
End;
{No. 11 result function pointer}
Function funcname: Boolean;
Begin
Funcname: = true; // not recommended // and placed on the right of the value assignment number cannot be a common variable
///////
Result: = true; // recommended // good scalability
End;
Function funcsum (A: array of integer): integer;
VaR I: integer;
Begin
Result: = 0;
For I: = low (A) to high (a) do
Result: = Result + A [I]; // cannot use funcsum: = funcsum + A [I];
End;
{Code that must be executed in No. 12, use the try... finally... end statement}
VaR
Vstringlist: tstringlist;
Begin
Vstringlist: = tstringlist. Create;
Vstringlist. loadfromfile (''c: \ temp.txt '');
Showmessage (vstringlist. Text );
Vstringlist. Free; // not recommended // resources cannot be released if an exception occurs
///////
Vstringlist: = tstringlist. Create;
Try
Vstringlist. loadfromfile (''c: \ temp.txt '');
Showmessage (vstringlist. Text );
Finally // suggestion // execute the command even if exit appears
Vstringlist. Free;
End;
End;
// Other cases 1
Begin
Screen. cursor: = crhourglass;
Try
{Time-consuming operations}
Finally
Screen. cursor: = crdefault;
End;
End;
// Other cases 2
Begin
Query1.disablecontrols;
Try
{Operation dataset}
Finally
Query1.enablecontrols;
End;
End;