Delphi,Lazarus程序设计改进一法
作者:steven
QQ:1565498246
Delphi/Lazarus有一个思想就是方法、函数名调用,后边可以不使用括号(),比如调用函数Now,这使得程序看上去是访问一个局部变量,降低了程序可读性,所以本方法提出:
1.将局部变量加小写L前缀;
2.将方法调用全部加(),防止和变量名混淆,使代码可读性降低。比如调用函数Now ,正确的方法是 : Now()
我们来看具体实例:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TfrmMain }
TfrmMain = class(TForm)
btnTest: TButton;
procedure btnTestClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.lfm}
{ TfrmMain }
procedure TfrmMain.btnTestClick(Sender: TObject);
var lNow:Tdatetime;
begin
lNow := Now(); //这里的Now() 过程本来按pascal语法可以不要(),但可能和下面的代码混淆
self.caption := formatdatetime('yyyy-MM-dd',lNow);
end;
end.
===================================================
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TfrmMain }
TfrmMain = class(TForm)
btnTest: TButton;
procedure btnTestClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.lfm}
{ TfrmMain }
procedure TfrmMain.btnTestClick(Sender: TObject);
var Now:Tdatetime;
begin
//Now := ;
self.caption := formatdatetime('yyyy-MM-dd',Now); //我们可以 ,这里的now不是调用的标准库的函数
end;
End.
输出的是没有初始化的日期数据。
同样,我们需要将方法,自定义函数,过程,都加上(),举例如下:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TfrmMain }
TfrmMain = class(TForm)
btnTest: TButton;
procedure btnTestClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
procedure test();
end;
var
frmMain: TfrmMain;
implementation
{$R *.lfm}
{ TfrmMain }
procedure TfrmMain.btnTestClick(Sender: TObject);
var Now:Tdatetime;
begin
//Now := ;
//self.caption := formatdatetime('yyyy-MM-dd',Now);
test();
end;
procedure TfrmMain.test();
begin
self.caption := 'this is a test';
end;
end.
<a href="http://files.cnblogs.com/stevenlaz/Delphi%2CLazarus%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E6%94%B9%E8%BF%9B%E4%B8%80%E6%B3%95.pdf">本文PDF格式下载</a>