Five tips to make Delphi code concise

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.