(VC Coder ==> Delphi Coder) 帮我看看以下代码,代码可以正常执行,但我想优化它。(200分)

  • 主题发起人 tigerfox
  • 开始时间
T

tigerfox

Unregistered / Unconfirmed
GUEST, unregistred user!
由于工作需要,将自己二年看来积累的VC类库转化成Delphi类库,以提高代码重用率
我对delphi只是在学校时期学过Object Passcal语言.

这是我写的类对象的一个功能函数。以下代码是极其简单的。但我想看看如果是大家写的会是什么样子?
//
// 功能: 取得共享内存值
// 参数: strValue :共享内存名称
// 返回值:共享内存值
//
function TShareMem.GetMemValue( strName:string;var strValue:string) : string;
var
strTemp : string;
size : integer;
begin
strTemp :='';
if m_pData = nil then
begin
result := '';
exit;
end;

if StrComp(PChar(strName),PChar(m_strName)) <> 0 then
begin
result :='';
exit;
end;

size := strLen(m_pData);
if size > m_nSize then
size := m_nSize;

SetLength(strTemp,size);
strCopy(PChar(strTemp) ,m_pData);

strValue := strTemp;
end;

另外再问一个delphi界有没有通用的命名规范。比如说,在VC很多人都在用匈牙利命名法
 
命名规范都是人定的,你在Delphi中也可以用匈牙利命名法呀!
我也是用VC,最近正在研究Delphi
 
function TShareMem.GetMemValue( strName:string;var strValue:string) : string;
var
strTemp : string;
size : integer;
begin
size:=0;
strTemp :='';
if not (m_pData = nil)or(StrComp(PChar(strName),PChar(m_strName)) <> 0) then
begin
size := strLen(m_pData);
if size > m_nSize then
size := m_nSize;

SetLength(strTemp,size);
strCopy(PChar(strTemp) ,m_pData);
end;

strValue := strTemp;

end;
 
楼上比我的强
 
逻辑错:
if (m_pData <> nil)and(StrComp(PChar(strName),PChar(m_strName)) <> 0) then
 
上面写的也错了。 ^_^

大家有没有什么好的命名法
 
代码创建形式规范 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打头
 
命名這個東西因人因隊而異。
DELPHI*開發人員指南上小提了一下。我們基本是那樣命名的。
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
772
import
I
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
817
import
I
I
回复
0
查看
621
import
I
顶部