Five tips for concise code-Delphi

Source: Internet
Author: User

Writing code is an art. With Delphi, anyone can easily develop a software and complete some tasks. The perfect code can only be written by real experts. In addition to correct indentation, Case sensitivity, and naming rules, keep in mind Einstein's famous saying that simplicity is beauty. The five code problems that will be discussed below may be the mistakes made by beginners or even some old birds.

 

Advice 1
The value assignment operation for Boolean variables should be direct. For example, in an IF/then/else statement, the if clause assigns a Boolean variable to true, while the else clause assigns it to false. The following code is not well written:

  1. If if_love_delphi then
  2. Result: = true
  3. Else
  4. Result: = false;
  5. In this case, it is better to write:
  6. Result: = if_love_delphi;

If if_love_delphi then result: = trueelse result: = false; therefore, it is better to write: Result: = if_love_delphi;

 

Advice 2
Avoid using nested if/then/if statements instead of and. The following code is too cool:

  1. If if_love_delphi then
  2. If if_love_linux then
  3. Trykylix (now );
  4. It should be written as follows:
  5. If if_love_delphi and if_love_linux then
  6. Trykylix (now );

If if_love_delphi then if if_love_linux thentrykylix (now); It should be written as follows: If if_love_delphi and if_love_linux then trykylix (now );
Do not worry that the subsequent judgment statements will be executed ahead of schedule. Project | options | compiler | syntax options | the complete Boolean eval option is usually disabled (unless you select this option), which ensures that the execution sequence is not reversed.

 

Based on the first two tips, suppose you have a piece of code like this:

  1. If if_love_delphi then
  2. If if_love_linux then
  3. Result: = true;
  4. You can change it:
  5. Result: = if_love_delphi and if_love_linux;

If if_love_delphi then if if_love_linux thenresult: = true; you can change it to: Result: = if_love_delphi and if_love_linux;
Simply put, if the result is determined by a condition, the statement result: = true or result: = false is the same. You can assign values to boolean variables during initialization. However, you don't need to initialize a Boolean variable to false at all -- Delphi has granted the variable a position of false when creating it. Similar situations include:

 

Boolean attribute of the object, automatically initialized to false (0 );
Integer variable (integer), automatically initialized to 0;
String, which is automatically initialized as a null string.

Advice 3
When determining the value of a Boolean variable, you do not need to use a statement such as "= true" or "= false. The following statement is not good:

  1. If (if_love_delphi = true) and
  2. (If_love_linux = false) then
  3. Donottrylinux;
  4. If the return value of a function or an attribute is Boolean, write the following statement:
  5. If if_love_delphi and
  6. Not if_love_linux then
  7. Donottrylinux;

If (if_love_delphi = true) and (if_love_linux = false) Then donottrylinux; if the return value or attribute of a function is Boolean, write if if_love_delphi and not if_love_linux thendonottrylinux;

 

Advice 4
Try not to use the "+" operator to merge strings. The efficiency is too low. The following example is not good:

  1. Showmessage ('height below '+ inttostr (iheight) + 'meters, weight' + inttostr (iweight) + 'kg. ');
  2. This will be better written:
  3. Showmessage (format ('lower height % d, weight % d. ', [Iheight, iweight]);

Showmessage ('height below '+ inttostr (iheight) + 'meters, weight' + inttostr (iweight) + 'kg. '); This write will be better: showmessage (format (' lower height % d, weight % d. ', [Iheight, iweight]);

 

Tip 5
Use the with statement whenever possible. It not only has high efficiency, but also makes the code easier to read. For example, this Code:

  1. If sender is tedit then
  2. If (tedit (sender). Text = ') or
  3. (Tedit (sender). Text [tedit (sender). selstart] = ') or
  4. (Tedit (sender). sellength =
  5. Length (tedit (sender). Text ))
  6. And (key in ['A'... 'Z']) then
  7. Key: = uppercase (key );
  8. This code is not as concise and easy to read as follows:
  9. If sender is tedit then
  10. With sender as tedit do
  11. If (text = ') or
  12. (Text [selstart] = ') or
  13. (Sellength = length (text) and
  14. (Key in ['A'... 'Z'] Then
  15. Key: = upcase (key );

Http://vir.jxstnu.edu.cn/xieyunc/read.php? 29

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.