2014年1月23日 星期四

Delphi 使程式碼簡潔的忠告

一、Boolean值操作應該是直接的if If_Love_Delphi then
  Result:=True
else
  Result:=False;
改成這樣寫比較好:
Result:= If_Love_Delphi;

二、避免使用 if/then/if ,而用and来代替例1:
if If_Love_Delphi then
  if If_Love_Linux then
    TryKylix(Now);
改成這樣寫比較好:
if If_Love_Delphi and If_Love_Linux then
  TryKylix(Now);
例2:
if If_Love_Delphi then
  if If_Love_Linux then
    Result:=True;
改成這樣寫比較好:
Result:= If_Love_Delphi and If_Love_Linux;

三、判斷boolean值時不需用"=True","=False"
if (If_Love_Delphi=True) and (If_Love_Linux=False) then
  DoNotTryLinux;
改成這樣寫比較好:
if If_Love_Delphi and not If_Love_Linux then
  DoNotTryLinux;


四、盡量不要用"+"來進行字串合併ShowMessage('在下身高'+IntToStr(iHeight)+'尺,體重'+IntToStr(iWeight)+'公斤。');改成這樣寫比較好:
ShowMessage(Format('在下身高%d,體重%d。', [iHeight,iWeight]));

沒有留言:

張貼留言