Delphi代码创建形式规范关键字 Delphi代码创建形式规范 Delphi代码创建形式规范本规范的目的:给自己的代码一个统一而标准的外观,增强
可读性,可理解性,可维护性
本规范的原则:名称反映含义,形式反映结构
1、单元风格
2、各区风格
3、语句风格
4、命名规则参考:Borland官方Object Pascal风格指南
Delphi5程序员指南编码标准
1、单元风格
 {*******************************************************}
{ }
{ 项目名称 }
{ }
{ 版权所有 (C) 2000,2001 公司名称 }
{ }
{*******************************************************}
unit UnitName;
{*******************************************************
项目:
模块:
描述:
版本:
日期:
作者:
更新:
TODO:
*******************************************************}interfaceuses
----,----,----,----,----,----,----,----,----,----,----, 
----,----, ----,----,----,----;const
--------------------;
--------------------;
--------------------;type
--------------------;
--------------------;
--------------------;
--------------------;
--------------------;
--------------------;var
--------------------;
--------------------;
--------------------;implementationuses
----,----,----,----;{$R *.RES}
{$R *.DFM}--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;end.返回 
2、各区风格
0、注释与空白 
用{ } 不用 //
主题注释,函数过程目的说明,语句注释
空行 :版权块,类之间,方法之间--(两行) 方法内部块(一行)
空格 :用以增强清晰度 
缩进 :两个空格 1、常量区 
基本:Const
----- = ----;
----- = ----;
----- = ----;
----- = ----;扩展
前缀: 少则C_---;多则可以每个主题有一个前缀
Const{ 主题1 }
C_--- = ----; { 含义 }
C_--- = ----; { 含义 }
C_--- = ----; { 含义 }
C_--- = ----; { 含义 }
{ 主题2 }
----- = ----;
----- = ----;
----- = ----;
----- = ----;资源字符串,放在变量区后面resourcestringconst
S_--- = '----';
S_--- = '----';
S_--- = '----';例子:CM_BASE = $B000;
CM_ACTIVATE = CM_BASE + 0;
CM_DEACTIVATE = CM_BASE + 1;
CM_GOTFOCUS = CM_BASE + 2;
CM_LOSTFOCUS = CM_BASE + 3;
NumPaletteEntries = 20;
BoxPoints : array[0..5, 0..2] of GLfloat =
( (-1, 0, 0),
( 0, 1, 0),
( 1, 0, 0),
( 0, -1, 0),
( 0, 0, 1),
( 0, 0, -1) );{ Variant type codes (wtypes.h) }varEmpty = $0000; { vt_empty }
varNull = $0001; { vt_null }
varSmallint = $0002; { vt_i2 }
GIFVersions : array[gv87a..gv89a] of TGIFVersionRec = ('87a', '89a');
2、类型区 
数据类型-->不提供服务的数据类型
T---- = ---------
对象类型-->有状态并提供服务的实体
T---- = class(----)
private
--------
--------
protected
--------
--------
public
--------
--------
published
--------
--------
end;

解决方案 »

  1.   

    按字母排序Private
    1、所有数据放在Private 区,以F打头
    2、所有事件属性对应的方法指针放在Private 区,以F打头
    3、属性的Get与Set方法放在Private 区-->不准备被继承
    4、响应消息的方法放在Private 区
    protected
    1、被子类调用的但不能被外界调用的方法与属性
    2、供子类重载的方法 virsual; virsual; abstract
    public
    1、构建析构方法
    2、供外界调用的方法
    3、供外界调用的属性
    published
    1、出现在Object Inspector里供设计时用的属性
    2、出现在Object Inspector里供设计时用的事件响应 例子:TGIFVersion = (gvUnknown, gv87a, gv89a);
    TGIFVersionRec = array[0..2] of char;
    PInterfaceTable = ^TInterfaceTable;
    TInterfaceTable = packed record
    EntryCount: Integer;
    Entries: array[0..9999] of TInterfaceEntry;{ forword declairation } 
    TGIFImage = class;
    TGIFSubImage = class;
    {---------------------------
    TGIFItem
    ---------------------------}
    TGIFItem = class(TPersistent)
    private
    FGIFImage: TGIFImage;
    .............
    end; 3、变量区 
    定义全局变量
    注意不要有缺省的类对象变量,在调用者中声明!
    var
    -----------: -------;
    -----------: -------;
    例子:
    GIFDelayExp: integer = 10; { Delay multiplier in mS.}
    GIFDelayExp: integer = 12; 4、实现区 
    {---------------------------------------------------------
    主题
    ----------------------------------------------------------}{ 方法的目的 }
    procedure ----------------------------
    begin
    --------;
    --------;
    end;{ 方法的目的 }
    function -----------------------------
    begin
    --------;
    --------;
    end;
    5、过程与函数 
    命名
    格式返回 3、语句风格
    1、简单语句
    -------;
    2、复合语句 
    begin
    -----;
    -----;
    end; 3、赋值语句
    -- := -------;
    -- := (-- + --)* (-- / --);4、局部变量
    var
    ---: ---;
    ---: ---;
    对于逻辑上并列的变量组:
    var
    ---,
    ---,
    ---: ---;5、数组声明
    --- = array [*..*] of ---;6、if 语句
    if (--------) then
    -------------;
    if (--------) then
    begin
    -------------;
    -------------;
    -------------;
    end; if (--------) then
    -------------;
    else
    -------------; if (--------) then
    begin
    -------------;
    -------------;
    -------------;
    end else
    -------------;if (--------) then
    begin
    -------------;
    -------------;
    -------------;
    end else
    begin
    -------------;
    -------------;
    -------------;
    end;if (--------) then
    -------------
    else if (--------) then
    -------------; 7、for 循环 for I := -------- to -------- do
    -------------;
    for I := -------- to -------- do
    begin
    -------------;
    -------------;
    -------------;
    end;for I := -------- to -------- do
    if (--------) then
    begin
    -------------;
    -------------;
    -------------;
    end;for I := -------- to -------- do
    with -------- then
    begin
    -------------;
    -------------;
    -------------;
    end; 8、while 循环 while ------ do
    begin
    -------------;
    -------------;
    -------------;
    end; 9、repeat 循环
    repeat
    -------------;
    -------------;
    -------------;
    until ------; 10、case 语句case -------- of
    -------- : -------------;
    -------- : -------------;
    -------- : -------------;
    else -------------;
    end;case -------- of
    -------- :
    -----------------------------------------------------------------;
    -------- :
    -----------------------------------------------------------------;
    -------- :
    -----------------------------------------------------------------;
    else
    -----------------------------------------------------------------;
    end;case -------- of
    -------- : begin
    --------------------------;
    --------------------------;
    --------------------------;
    end;
    -------- : begin
    --------------------------;
    --------------------------;
    --------------------------;
    end;
    -------- : begin
    --------------------------;
    --------------------------;
    --------------------------;
    end
    else begin
    -------------;
    -------------;
    -------------;
    end;
      

  2.   

    end; 11、with 语句 
    with -------- do
    -------------;with -------- do
    begin
    -------------;
    -------------;
    -------------;
    end; 12、try 语句 
    try
    -------------;
    -------------;
    -------------;
    finally
    -------------;
    -------------;
    -------------;
    end; try
    try
    -------------;
    -------------;
    -------------;
    except
    -------------;
    -------------;
    end;
    finally
    -------------;
    -------------;
    -------------;
    end; 13、其它 
    运算:运算符前后要有空格
    w1[n] := ((i + 1) * v0[n] + j * v1[n] + (k - 1) * v2[n]) / depth;-- = --
    -- >= --
    -- <= --
    -- > --
    -- < --
    -- <> --
    -- := --; 赋值
    --: ----; 类型 同一类型且含义逻辑上不并列的变量 20个字符长的变量名
    private
    ------- : -------;
    ------- : -------;
    ------- : -------;
    ------- : -------;
    ------- : -------;
    var
    ------- : -------;
    ------- : -------;
    ------- : -------;
    ------- : -------;
    ------- : -------;
    function ---------------------(--: ----; --: ----; --: ----): ----;同一类型且含义逻辑上并列的变量 如 Error0,Error1,Error2 ; R,G,B
    private
    ------- ,
    ------- ,
    ------- ,
    ------- ,
    ------- : -------
    var
    ------- ,
    ------- ,
    ------- ,
    ------- ,
    ------- : -------
    function ---------------------(--, --, --: ----; var --, --, --: ----): ----;T------- = class(-------)
    private
    F-------: -------;
    F-------: -------;
    F-------: -------;
    function --------------: -------;
    procedure --------------;
    protected
    function --------------: -------;
    procedure --------------;
    function --------------: -------; virtual; abstract;
    public
    constructor Create(-------: -------); override; {if need to do something after Create}
    destructor Destroy; override; {if need to do something before Destroy}
    function --------------: -------;
    procedure --------------;
    property -------: ------- read F-------;
    publishedend; 14、形式反映结构 
    例子:
    TetIndex : array[0..3] of TInteger3v =
    ( (0, 1, 3),
    (2, 1, 0),
    (3, 2, 0),
    (1, 2, 3) );
    Cursors: array[0..4] of TIdentMapEntry = (
    (Value: crDefault; Name: 'crDefault'),
    (Value: crArrow; Name: 'crArrow'),
    (Value: crCross; Name: 'crCross'),
    (Value: crIBeam; Name: 'crIBeam') ); if (dwFlags and PFD_DRAW_TO_WINDOW) = 0)
    or( (dwFlags and PFD_SUPPORT_OPENGL) = 0)
    or( (dwFlags and PFD_DOUBLEBUFFER) = 0)
    or (iPixelType <> PFD_TYPE_RGBA)
    or (cColorBits < 16)
    )
    ) then
    raise Exception.Create('Inappropriate Pixel Format chosen.');glBegin(shadeType);
    glNormal3fv(@n0);
    glVertex3fv(@dodec[a, 0]);
    glVertex3fv(@dodec[b, 0]);
    glVertex3fv(@dodec[c, 0]);
    glVertex3fv(@dodec[d, 0]);
    glVertex3fv(@dodec[e, 0]);
    glEnd();dodec[0, 0] := -alpha; dodec[0, 1] := 0; dodec[0, 2] := beta;
    dodec[1, 0] := alpha; dodec[1, 1] := 0; dodec[1, 2] := beta;
    dodec[2, 0] := -1; dodec[2, 1] := -1; dodec[2, 2] := -1;procedure glutWireTorus(
    innerRadius : GLdouble; //---------
    outerRadius : GLdouble; //---------
    nsides : GLint; //---------
    rings : GLint ); //---------
    case FRunDirection of
    rdRightToLeft : begin
    StY:=CnY;
    StX:=Width - CurrentStep;
    end;
    rdLeftToRight : begin
    StY:=CnY;
    StX:=-CurrentStep;
    end;
    rdBottomToTop : begin
    StX:=CnX;
    StY:=Height - CurrentStep;
    end;
    rdTopToBottom : begin
    StX:=CnX;
    StY:=CurrentStep - RTHeight;
    end;
    else begin
    StX:=CnX;
    StY:=CnY;
    end;
    end;case (DitherMode) of
    dmNearest:
    Ditherer := TDitherEngine.Create(Bitmap.Width, ColorLookup);
    dmFloydSteinberg:
    Ditherer := TFloydSteinbergDitherer.Create(Bitmap.Width, ColorLookup);
    dmStucki:
    Ditherer := TStuckiDitherer.Create(Bitmap.Width, ColorLookup);
    dmSierra:
    Ditherer := TSierraDitherer.Create(Bitmap.Width, ColorLookup);
    dmJaJuNI:
    Ditherer := TJaJuNIDitherer.Create(Bitmap.Width, ColorLookup);
    dmSteveArche:
    Ditherer := TSteveArcheDitherer.Create(Bitmap.Width, ColorLookup);
    dmBurkes:
    Ditherer := TBurkesDitherer.Create(Bitmap.Width, ColorLookup);
    else
    exit;
    end;4、命名规则
    1、文件名称: u模块名称;见名知意
    2、控件名称: 功能_控件缩写;见名知意
    3、变量 : 尽量不用缩写,尽量用名词;见名知意
    4、方法与过程:尽量不用缩写,尽量用动宾词组;见名知意 
    5、常见的惯例
    类名以T打头 (Type之意)
    类的私有数据域以F打头(Field之意)
    对数据的存取操作分别以Set,Get打头
    事件属性以On打头 (完)愿和所有喜欢编程的人做朋友!