Five advice on code of code - Delphi Garden

xiaoxiao2021-03-06  20

Write code is an art. Using Delphi, anyone can easily develop some software to complete some tasks. The perfect code only has a real master to write. In addition to correct indentation, casement, naming rules, please keep in mind the famous sayings of Iinstein - Simple is beautiful. The five code issues will be discussed below, which may be an income of beginners, or even some old birds. The assignment of the admissibility of a bully variable should be straightforward. For example, in an IF / THEN / ELSE statement, the IF clause assigns a Boolean variable to true, and the ELSE clause is assigned to false. The way the code below is not good: if if_love_delphi life result: = trueElse result: = false; do this should be better: result: = if_love_delphi; Admissions Second avoid using nested IF / THEN / IF statements, and Replace with and The following code is too Luo: if if_love_delphi kili if if_love_linux lifekylix (now); if if_love_delphi and if_love_linux dam yl (now); Don't worry that the following judgment statement will be executed. Project | Options | Complete | Syntax Options | Complete Boolean EVAL options are typically closed (unless you select this item), this guarantees that the execution order will not reverse. For the first two advice, if you have such code: if if_love_delphi dam = true; you can change it: result: = if_love_delphi and if_love_linux; Simple, if the result depends on one condition, Then, Result: = true or result: = false This statement is more. When initializing the Boolean variable, you can assign them. But at all, you don't need to initialize a Boolean variable to False - Delphi, you have given it FALSE when you create this variable. Similar situations: Boolean properties of the object (Boolean), automatically initialized to false (0); integer, automatically initialized to 0; string, automatically initialized to empty strings . Advice three judges the value of the Boolean variable, there is no need to use "= true" or "= false". The following is not good: if (if_love_delphi = true) THEN DONOTTRYLINUX; the return value of the function or a property is a Boolean case, it should be written: if if_love_delphi and not ife_love_linux thrylinux; Advice 4 Don't Use the " " operator to combine the string. This is too low. The following example is not good: ShowMessage ('is at the lower body' INTTOSTR (Iheight) 'rice, weight' IWeight 'kg.'); This will better: showMessage (Format) , Body weight% d. ', [IHEIGHT, IWEIGHT])); Advice 5 Try to use the WITH statement. It is not only efficient, but also makes the code more easy to read.

转载请注明原文地址:https://www.9cbs.com/read-45182.html

New Post(0)