Existing Style Guides
Use columns
I disagree - spaces can be used to improve readability. Consider these
| Borland's Suggestion |
|---|
Port := IBQuery_Basic.FieldByName('Port').AsString ;
Sort_Order := IBQuery_Basic.FieldByName('Sort_Order').AsString ;
Comment := IBQuery_Basic.FieldByName('Comment').AsString ;
Active := 'Y';
|
| With Spaces |
Port := IBQuery_Basic.FieldByName('Port') .AsString ;
Sort_Order := IBQuery_Basic.FieldByName('Sort_Order').AsString ;
Comment := IBQuery_Basic.FieldByName('Comment') .AsString ;
Active := 'Y';
|
I suggest that any time spaces can be added to make code easier to read ... do it.
On the other hand, when only a single space is used before the colon, then searching for an assignment statement becomes much easier - it is nearly impossible with a variable number of spaces.
if..then..else
| Borland's Suggestion | Fewer lines |
|---|---|
if a = b then begin ... end else begin ... end; |
if a = b then begin ... end else begin ... end; |
Long Strings
| Borland's Suggestion |
|---|
s :=
'select "Experiment_Master_tbl"."Name" ' +
' FROM "Experiment_Master_tbl" INNER JOIN "Project_Master_tbl" ' +
' ON "Project_Master_tbl"."Project_ID" = "Experiment_Master_tbl"."Project_ID" ' +
' where "Project_Master_tbl"."Name" = ''' + Project + '''' +
' ORDER BY "Experiment_Master_tbl"."Name"'
;
|
| Starting with Operators |
s :=
'select "Experiment_Master_tbl"."Name" '
+ ' FROM "Experiment_Master_tbl" INNER JOIN "Project_Master_tbl" '
+ ' ON "Project_Master_tbl"."Project_ID" = "Experiment_Master_tbl"."Project_ID" '
+ ' where "Project_Master_tbl"."Name" = ''' + Project + ''''
+ ' ORDER BY "Experiment_Master_tbl"."Name"'
;
|
I also do this with long if statements that require multiple lines - except in this case I start the lines with and or or and line up the parameters.
Underscores
| Borland's Suggestion |
|---|
ThisIsAComponent.Parameter |
| With Underscores |
This_is_a_Component.Parameter |
*with* Statement
However, the with statement makes development and debug much more difficult.
Naming Conventions
General Naming Conventions
The following definitions are followed but not enforced
| TName | All type definitions begin with T |
| FProperty | The private class variable used to store a property's value starts with F (field) |
| OnMouseClick | Most event names begin with On |
| LVarName | Local variables, not usually used for single letter variable names - i, j, k, x, y, z |
| PVarName | A variable passed as a parameter. The Delphi VCL uses A instead of P. The common parameters Value and Message are used without a prefix. |
| PTName | Pointer to an object of type TName. |
Most property methods start with either Set or Get; events start with On.
User Interface Components
SaveData_UIButtonI have used this convention in several different languages (not just Delphi) and it is one of the reasons that I don't agree with the "suggestion" not to use underscores in variable names.
Menus
Menu_File_Open Menu_File_Save Menu_File_SaveAs Menu_File_Exit Menu_Edit_Copy Menu_Edit_Paste ...Other form components that are part of a larger group should follow a similar format, including
Custom DialogBoxes and Sub-Forms
Custom DialogBoxes and Sub-Forms
Form_Grp1_Action1 Form_Grp1_Action2 Form_Grp1_Action3 Form_Grp2_Action1 Form_Grp2_Action2 Form_Grp3_Action1 ...Forms are actually a bit more complicated - they have both a form name and a unit name (which is also used to name the *.dfm & *.pas files) ... and these two names must be different. In most cases, this is no big deal since an application has only one form
Form name xyz_Form Unit name xyz_frm File names xyz_frm.dfm, xyz_frm.pas, xyz_frm.dcu ...However, when an application contains lots of forms, .... (I am still working on this - 08-30-06)
Other References