Writing code is an art. With Delphi, anyone can easily develop some kind of software and accomplish certain tasks. And the perfect code is only the real master to write. In addition to the correct indentation, capitalization, naming rules, always keep in mind Einstein's famous quote-Simple is beautiful. The following five code issues will be mentioned, may be beginners, even some old birds will make mistakes.
Advice one
The assignment operation of a Boolean variable should be direct. For example, in a 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 a good one:
if If_Love_Delphi then
Result:=True
else
Result:=False;
And this is better to write:
Result:= If_love_delphi;
Advice two
Avoid the use of nested if/then/if statements and replace them with and. The following code is too verbose:
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);
Do not worry that the subsequent judgment statement will be executed in advance. Project| options| compiler| Syntax options| The Complete Boolean eval option is usually turned off (unless you select this item), which ensures that the order of execution is not reversed.
Synthesize the first two tips, if you have a piece of this code:
if If_Love_Delphi then
if If_Love_Linux then
Result:=True;
You can change it to:
result:= If_love_delphi and If_love_linux;
In simple terms, if the results depend on a conditional judgment, then result:=true or result:=false such statements are superfluous. When you initialize a Boolean variable, you can assign a value to it. But there's no need to initialize a Boolean variable to False--delphi. When you create this variable, you assign it a position of false. Similarly, there are:
The Boolean property (Boolean) of the object that is automatically initialized to False (0);
Integer variable (integer), automatically initialized to 0;
String, which is automatically initialized to an empty string.