Advice 1
The assignment operation of Boolean variables should be direct. For example, in an if/then/else statement, the if clause assigns a Boolean variable to True, and the else clause assigns it to False. The following code is not written well:
if If_Love_Delphi then
Result:=True
else
Result:=False;
And writing this way is better:
Results:= If_Love_Delphi;
Advice 2
Avoid using nested if/then/if statements, and instead use and instead. The following code is too wordy:
if If_Love_Delphi then
if If_Love_Linux then
TryKylix(Now);
It should be written like this:
if If_Love_Delphi and If_Love_Linux then
TryKylix(Now);
Don't worry that the subsequent judgment statement will be executed in advance. Project|Options|Compiler|Syntax Options|Complete Boolean eval option is usually turned off (unless you select this item), which ensures that the execution order is not reversed.
Combining the first two advices, if you have a code like this:
if If_Love_Delphi then
if If_Love_Linux then
Result:=True;
You can change it to:
Results:= If_Love_Delphi and If_Love_Linux;
Simply put, if the result depends on a conditional judgment, then statements such as Result:=True or Result:=False are unnecessary. When initializing Boolean variables, you can assign values to them. However, there is no need to initialize a Boolean variable to False -- Delphi has already assigned it to False when creating this variable. Similar situations include:
The Boolean property of the object is automatically initialized to False (0);
Integer variable (Integer), automatically initialized to 0;
String, automatically initialized to an empty string.
Advice 3
When judging the value of a Boolean variable, there is no need to use a statement such as "=True" or "=False". The following is not well written:
if (If_Love_Delphi=True) and
(If_Love_Linux=False) then
DoNotTryLinux;
For the return value of a function or if a property is a Boolean type, it should be written like this:
if If_Love_Delphi and
not If_Love_Linux then
DoNotTryLinux;
Advice 4
Try not to use the "+" operator for string merging. This is too inefficient. The following example is not good:
ShowMessage(''lower height''+IntToStr(iHeight)+''m, weight''+IntToStr(iWeight)+''kg.'');
Writing this way would be better:
ShowMessage(Format(''at the lower height %d, weight %d.'', [iHeight,iWeight]));
Advice 5
Use with statements as much as possible. Not only is it efficient, it also makes the code more readable. For example, this code:
if Sender if TEdit then
if (TEdit(Sender).Text='') or
(TEdit(Sender).Text[TEdit(Sender).SelStart]='') or
(TEdit(Sender).SelLength=
Length(TEdit(Sender).Text))
and (Key in [''a''..''z'']) then
Key:=UpperCase(Key);
It's not as concise and easy to read as such code:
If Sender is TEdit then
with Sender as TEdit do
if (Text='') or
(Text[SelStart]='') or
(SelLength=Length(Text)) and
(Key in [''a''..''z''] then
Key:=UpCase(Key);