发信人: Sun (大灯泡), 信区: BorlandDev
标  题: 代码创建形式规范  1.0 (for delphi)(转载)
发信站: 哈工大紫丁香 (2001年01月14日19:56:41 星期天), 转信

【 以下文字转载自 SoftEng 讨论区 】
【 原文由 Sun 所发表 】
代码创建形式规范  1.0 (for delphi)
bear, 2000-5-1

本规范的目的:给自己的代码一个统一而标准的外观,增强可读性,可理解性,可维护

本规范的原则:名称反映含义,形式反映结构

1、单元风格
2、各区风格
3、语句风格
4、命名规则

参考:borland官方object pascal风格指南
      delphi5程序员指南编码标准
    

1、单元风格
 

{*******************************************************}
{                                                       }
{                     项目名称                          }
{                                                       }
{            版权所有 (c) 2000,2001 公司名称            }
{                                                       }
{*******************************************************}


unit unitname;
{*******************************************************
项目:
模块:
描述:
版本:
日期:
作者:
更新:
todo:
*******************************************************}

interface

uses
   ----,----,----,----,----,----,----,----,----,----,----, 
   ----,----, ----,----,----,----;

const
   --------------------;
   --------------------;
   --------------------;
  
type
   --------------------;
   --------------------;
   --------------------;
    --------------------;
   --------------------;
   --------------------;

var
   --------------------;
   --------------------;
   --------------------;

implementation

uses
   ----,----,----,----;

{$r *.res}
{$r *.dfm}

--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;

end.

返回 

  

2、各区风格
0、注释与空白 
   用{ } 不用 //
   主题注释,函数过程目的说明,语句注释
   空行 :版权块,类之间,方法之间--(两行) 方法内部块(一行)
   空格 :用以增强清晰度                        
   缩进 :两个空格  

1、常量区 
基本:

const
  -----    = ----;
  -----    = ----;
  -----    = ----;
  -----    = ----;

扩展
前缀:  少则c_---;多则可以每个主题有一个前缀
const

  { 主题1 }
  c_---    = ----;  { 含义 }
  c_---    = ----;  { 含义 }
  c_---    = ----;  { 含义 }
  c_---    = ----;  { 含义 }
  { 主题2 }
  -----    = ----;
  -----    = ----;
  -----    = ----;
  -----    = ----;

资源字符串,放在变量区后面

resourcestring

const
  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;

  

   按字母排序

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;

  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-------;
     published

     end;  

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打头 

(完)

--
    太阳当空照,灯泡呵呵笑,
    mm说,早上好,你为什么又不理我了?
    我要做光光,光光没烦恼,
    高高跳,大声叫,光光的乐趣你们不知道!
知道中国软件业为什么落后吗?
忽视软件工程,中国软件业将永远落后下去...

※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: sunner.hit.edu.cn]
--
※ 转载:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: sunner.hit.edu.cn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:230.774毫秒