Lead: http://www.delphibbs.com/keylife/iblog_show.asp? Xid = 29323
By: 600666
Title: Delphi code specifications
Keywords:
Category: personal zone
Confidentiality level: Public
The purpose of this specification: to provide a unified and standard appearance for your code and enhance
Readability, comprehensibility, and maintainability
Principles of this provision: names reflect meanings and forms reflect Structures
1. Unit style
2. Regional Style
3. Statement Style
4. Naming rules
Reference: Borland official Object Pascal Style Guide
Delphi5 programmer guide coding standards
1. Unit style
{*************************************** ****************}
{}
{Project name}
{}
{Copyright (c) company name}
{}
{*************************************** ****************}
Unit unitname;
{*************************************** ****************
Project:
Module:
Description:
Version:
Date:
Author:
Update:
Todo:
**************************************** ***************}
Interface
Uses
----,----,----,----,----,----,----,----,----,----,----,
----,----,----,----,----,----;
Const
--------------------;
--------------------;
--------------------;
Type
--------------------;
--------------------;
--------------------;
--------------------;
--------------------;
--------------------;
VaR
--------------------;
--------------------;
--------------------;
Implementation
Uses
----,----,----,----;
{$ R *. Res}
{$ R *. DFM}
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
--------------------------------;
End.
Return
2. Regional Style
0. Comments and blank spaces
Use {} No //
Topic comment, function process description, and statement comment
Blank row: Copyright block, between classes, between methods -- (two rows) Internal block of the method (one row)
Space: Used to enhance Definition
Indent: two spaces
1. Constant Zone
Basic:
Const
----- = ----;
----- = ----;
----- = ----;
----- = ----;
Extension
Prefix: if it is less, then C _ ---; if it is more, each topic can have a prefix.
Const
{Topic 1}
C _ --- = ----; {Meaning}
C _ --- = ----; {Meaning}
C _ --- = ----; {Meaning}
C _ --- = ----; {Meaning}
{Topic 2}
----- = ----;
----- = ----;
----- = ----;
----- = ----;
Resource string, placed behind the Variable Area
Resourcestring
Const
S _ --- = '----';
S _ --- = '----';
S _ --- = '----';
Example:
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. Type Zone
Data Type --> data type that does not provide services
T ---- = ---------
Object Type --> an object that has a status and provides services
T ---- = Class (----)
Private
--------
--------
Protected
--------
--------
Public
--------
--------
Published
--------
--------
End;
Sort by letter
Private
1. Put all data in the private zone and start with F
2. Place the method pointer corresponding to all event properties in the private area and start with F
3. Put the get and set methods of attributes in the private zone --> do not prepare to be inherited
4. Put the Response Message method in the private area
Protected
1. Methods and attributes called by the quilt class but not called by the outside world
2. The method virsual; abstract
Public
1. Build the Structure Method
2. Methods for external calls
3. attributes for external calls
Published
1. attributes that appear in the object inspector for Design
2. Event Response that appears in the object inspector for Design
Example:
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. Variable Area
Define global variables
Be sure not to have default class object variables, which should be declared in the caller!
VaR
-----------:-------;
-----------:-------;
Example:
Gifdelayexp: integer = 10; {delay multiplier in ms .}
Gifdelayexp: integer = 12;
4. Implementation Zone
{---------------------------------------------------------
Topic
----------------------------------------------------------}
{Purpose of the method}
Procedure ----------------------------
Begin
--------;
--------;
End;
{Purpose of the method}
Function -----------------------------
Begin
--------;
--------;
End;
5. Process and Function
Name
Format
Return
3. Statement Style
1. Simple statements
-------;
2. Compound statements
Begin
-----;
-----;
End;
3. Assignment Statement
--: = -------;
--: = (-- + --)*(--/--);
4. Local Variables
VaR
---:---;
---:---;
For logically tied variable groups:
VaR
---,
---,
---:---;
5. array declaration
--- = Array [* .. *] ---;
6. If statement
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 Loop
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 Loop
While ------ do
Begin
-------------;
-------------;
-------------;
End;
9. repeat loop
Repeat
-------------;
-------------;
-------------;
Until ------;
10. Case statement
Case --------
--------:-------------;
--------:-------------;
--------:-------------;
Else -------------;
End;
Case --------
--------:
-----------------------------------------------------------------;
--------:
-----------------------------------------------------------------;
--------:
-----------------------------------------------------------------;
Else
-----------------------------------------------------------------;
End;
Case --------
--------: Begin
--------------------------;
--------------------------;
--------------------------;
End;
--------: Begin
--------------------------;
--------------------------;
--------------------------;
End;
--------: Begin
--------------------------;
--------------------------;
--------------------------;
End
Else begin
-------------;
-------------;
-------------;
End;
End;
11. With statement
With -------- do
-------------;
With -------- do
Begin
-------------;
-------------;
-------------;
End;
12. Try statement
Try
-------------;
-------------;
-------------;
Finally
-------------;
-------------;
-------------;
End;
Try
Try
-------------;
-------------;
-------------;
Except
-------------;
-------------;
End;
Finally
-------------;
-------------;
-------------;
End;
13. Others
Operation: there must be spaces before and after the operator
W1 [N]: = (I + 1) * V0 [N] + J * V1 [N] + (k-1) * V2 [N])/depth;
-- = --
--> = --
-- <= --
--> --
-- <--
-- <> --
--: = --; Value assignment
--: ----; Type
Variable names of the same type and whose meanings are not logically parallel. Variable names are 20 characters long.
Private
-------:-------;
-------:-------;
-------:-------;
-------:-------;
-------:-------;
VaR
-------:-------;
-------:-------;
-------:-------;
-------:-------;
-------:-------;
Function ---------------------(--:----;--:----;--:----):----;
Logically tied variables of the same type, such as error0, error1, error2; R, G, and 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. reflected structure
Example:
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 gluwiretorus (
Innerradius: gldouble ;//---------
Outerradius: gldouble ;//---------
Nsides: glint ;//---------
Rings: glint );//---------
Case frundirection
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)
Dmnearest:
Ditherer: = tditherengine. Create (bitmap. Width, colorlookup );
Dmfloydsteberger:
Ditherer: = tfloydsteinbergditherer. Create (bitmap. Width, colorlookup );
Dmstucki:
Ditherer: = tstuckiditherer. Create (bitmap. Width, colorlookup );
Dmsisierra:
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;
Return
4. Naming rules
1. File Name: U Module name; known
2. Control name: Abbreviation of function _ Control
3. variables: Use nouns whenever possible without abbreviations.
4. methods and processes: Try not to use abbreviations and use dynamic object phrases whenever possible.
5. Common practices
Class name starts with T (type)
The private data fields of the class start with F (field meaning)
The data access operations start with set and get respectively.
The event property starts with "on ".
(End)