Object Pascal Style Guide

zhaozj2021-02-11  220

Object Pascal Style Guide - by Charles Calvert

Abstract: This Article Documents a standard style for formatting delphi code. It is based on the convention.............

Object Pascal Style Guide This article documents a standard style for formatting Delphi code. It is based on the conventions developed by the Delphi team. We take it for granted that many well established shops will have conventions different than those specified here. As a result, we strongly recommend using a tool that can convert your code into Borland style before submitting it to Borland, Project JEDI, or any other public source repository. We do not want to force you to change your conventions, but we insist that all code that ships with Borland products follows these conventions. We strongly encourage you to follow these conventions when submitting code into any form of public repository. Object Pascal is a beautifully designed language. One of its great virtues is its readability. These standards are designed to enhance that Readability of Object Pascal Code. When Developers Follow The Simple Conventions Laid Out in this Guide, They Will Be Promoting Standards That Benefit All Delphi developers by using a uniform style That Is Easy to read. Efforts to Enforce THESE STANDARDS WILL INCREASE The VALUE OF A Developer '

s source code, particularly during maintenance and debugging cycles. It goes without saying that these are conventions based primarily on matters of taste. Though we believe in, and admire the style promoted in these pages, we support them not necessarily because we believe they are right and others are wrong, but because we believe in the efficacy of having a standard which most developers follow. The human mind adapts to standards, and finds ways to quickly recognize familiar patterns, thereby assimilating meaning quickly and effortlessly. It is the desire to create a standard that will make reading code as simple as possible for the largest number of people that is behind this effort. If at first our guidelines seem strange to you, we ask you to try them for awhile, and then we are sure you will Grow buy to the theme. or, if you prefer, Keep Your Code in Your Own Format, And Run It THROUGH a Program That Follows Our Guidelines Before Submitting It To Borland OR To a Public Rep ository. Some text editors, such as Visual SlickEdit can help you format your code according to a particular style. Readers who are aware of other tools that provide this same service should write me at that address provided at the end of this section. One free Formatter Developed by EGBERT VAN NES IS Available At the Following Url: http://www.slm.wau.nl/wkao/delforexp.html. a Commercial Option IS CRACKERJAX for Delphi: http:

//www.kineticsoftware.com/html/products.html. Before closing this introduction, I want to reiterate that on the Borland web site, and on the CDs that we ship with our product, these standards are the law. We want to present our code in a unified and easy to read style, and enforcing the rules in this guide is the simplest way to achieve that end. Do not post this specification on other web sites. Instead, simply link to this version of the document. We Accept feedingback in the form of corrections or suggestions. Send Your Communications to Charlie Calvert .contents

1.0 Introduction

1.1 Background 1.2 Acknowledge DGMENTS 2.0 Source Files

2.1 Source-File Naming 2.2 Source-File Organization

2.2.1 CopyRight / ID Block Comment 2.2.2 Unit Declaration 2.2.3 Uses Declarations 2.2.4 Class / Interface Declarations 3.0 Naming Conventions

3.1 Unit Naming 3.2 Class / Interface Naming 3.3 Field Naming 3.4 Method Naming 3.5 Local Variable Naming 3.6 Reserved Words 3.7 Type Declarations 4.0 White Space Usage

4.1 Blank Lines 4.2 Blank Spaces

4.2.1 A Single Blank Space (Not Tab) Should Be Used: 4.2.2 Blanks Should Not Be Used: 4.3 Indentation 4.4 Continuation Lines 5.0 Comments

5.1 Block Comments 5.2 SINGLE-LINE Comments 6.0 Classes

6.1 Class Body Organization 6.2 Method Declarations 6.3 Data Store Declarations 7.0 Interfaces

7.1 Interface Body Organization 8.0 Statements

8.1 Simple Statements

8.1.1 Assignment and Expression Statements 8.1.2 Local Variable Declarations 8.1.3 Array Declarations 8.2 Compound Statements

8.2.3 if statement 8.2.4 for statement 8.2.5 while statement 8.2.6 repeat until statement 8.2.7 case statement 8.2.8 try statement 1.0 Introduction This document is not an attempt to define a grammar for the Object Pascal language. For instance, it is illegal to place a semicolon before an else statement; the compiler simply will not let you do it As a result, I do not lay that rule out in this style guide This document is meant to define the proper course.. of action in places where the language gives you a choice. I usually remain mute on matters that can only be handled one way. 1.1 Background The guidelines presented here are based on the public portions of the Delphi source. The Delphi source should follow these guidelines precisely. If you find cases where the source varies from these guidelines, then these guidelines, and not the errant source code, should be considered your standard. nevertheless, you should use the source as a supplement to these guidelines, at least so far asit can help you get a general feel for how your code should look. 1.2 Acknowledgments The format of this document and some of its language is based on work done to define a style standard for the Java language. Java has had no influence on the rules for formatting Object Pascal source, but documents found on the Sun web site formed the basis for this document. In particular the style and format of this document were heavily influenced by "A Coding style Guide for Java WorkShop and Java Studio Programming"

. By Achut Reddy That document can be found at the following URL: http://www.sun.com/workshop/java/wp-coding The Delphi team also contributed heavily to the generation of this document, and indeed, it would not have been possible to create it without their help. 2.0 Source Files Object Pascal source is divided up primarily into units and Delphi Project files, which both follow the same conventions. A Delphi Project file has a DPR extension. It is the main source file for a project. Any units used in the project will have a PAS extension. Additional files, such as batch files, html files, or DLLs, may play a role in a project, but this paper only treats the formatting of DPR and PAS files. . 2.1 Source-File Naming Object Pascal supports long file names If you are appending several words to create a single name, then it is best to use capital letters for each word in the name:. MyFile.pas This is known as InfixCaps, or Camel Caps. Extensions Should Be in Lower Case. For Historical Reasons, the Delphi source itself often confines itself to 8: 3 naming patterns, but developers no longer need feel constrained by those limits, even if turning in source that might be used by the Delphi team If you are translating a C / C .

header file, then your Pascal header translation will usually have the same name as the file you are translating, except it should have a PAS extension. For instance, Windows.h would become Windows.pas. If the rules of Pascal grammar force you to combine multiple header files into a single unit, then use the name of the base unit into which you are folding the other files. For instance, if you fold WinBase.h into Windows.h, then call the resulting file Windows.pas. 2.2 Source-File OrganizationAll Object Pascal units should contain the following elements in the following order: Copyright / ID block comment Unit Name Interface section Implementation A closing end and a period At least one blank line should separate each of these elements Additional elements can be.. structured in the order you find most appropriate, except that the top of the file should always list the copyright first, the unit name second, then any conditional defines, compiler directives or include statements, then the Uses Clause: {*************************************************** *********}

{}

{Borland Delphi Visual Component Library}

{}

{CopyRight (C) 1995, 98 Inprise Corporation}

{}

{********************************************************** ******}

Unit button;

{$ S-, W-, R-}

{$ C preload}

Interface

Uses

Windows, Messages, Classes,

Controls, Forms, Graphics,

Stdctrls, Extctrls, CommCtrl

It does not matter if you place a type section before a const section, or if you mix type and const sections up in any order you choose. The implementation should list the word implementation first, then the uses clause, then any include statements or other Directives: importationUses

Consts, Sysutils, Actnlist,

Imglist;

{$ R buttons.res}

2.2.1 Copyright / ID block commentEvery source file should start with a block comment containing version information and a standard copyright notice The version information should be in the following format:. {************** ************************************************

{}

{Widgets galore}

{}

{CopyRight (C) 1995, 98 Your Company}

{}

{********************************************************** ******} The Copyright Notice Should Contain At Least The Following Line:

Copyright (c) Yearlist CopyRightholder.

IF you are a third party creating a file for use by borland, you may add your name at the bottom of the copyright NOTTOTO: {********************** ****************************************}

{}

{Borland Delphi Visual Component Library}

{CopyRight (c) 1995, 99 Borland International}

{Created by project jedi}

{}

{********************************************************** ******} 2.2.2 Unit DeclarationEvery Source File Should Contain A Unit Declaration. The Word Unit BE in Lower Case. The name of the unit shop be in Mixed Upper And LowerCase, And Must be the same as the name buy by the operation system's file system. example; unit myunit;

This unit would be called MyUnit.pas when an entry is placed in the file system. 2.2.3 uses declarations Inside units, a uses declaration should begin with the word uses, in lowercase. Add the names of the units, following the capitalization conventions Used in the declaration found inside the unit: uses myunit; Each Unit Must Be Separated from Its Neighbor By A Comma, And The Last Unit SHOULD HAVE A SEMICOLON AFTER IT: Uses

Windows, Sysutils, Classes, Graphics, Controls, Forms,

Typinfo;

IT IS CORRECT To Start The Uses Clause On The Next Line, As In The Previous Example, or You May Start The List of Units on The Same Line: Uses Windows, Sysutils, Classes, Graphics, Controls, Forms,

Typinfo;

You may format the list of units in your uses clause so that they wrap just shy of 80 characters, or so that one unit appears on each line. 2.2.4 class / interface declarations A class declaration begins with two spaces, followed by an identifier prefaced by a capital T. Identifiers should begin with a capital letter, and should have capital letters for each embedded word (InfixCaps) Never use tab characters in your Object Pascal source Example:.. TMyClass Follow the identifier with a space, then an equals sign, then the word class, all in lower case: TMyClass = class If you want to specify the ancestor for a class, add a parenthesis, the name of the ancestor class, and closing parenthesis: TMyClass = class (TObject) Scoping directives should Be Two Spaces in from the margin, and declared in the order shown in this example: TMYCLASS = Class (TOBJECT)

Private

protected

public

Published

END;

Data Should Always Be Decland Only In The Private Section, And Its Identifier Should Be Prefaced by An F. All Type Declarations Should Be Four Spaces in from The Margin:

TMYCLASS = Class (TOBJECT)

Private

FMYDATA: Integer;

Function GetData: Integer;

Procedure setData (Value: integer);

public

Published

Property MyData: Integer Readdata Write SetData;

END;

Interfaces follow the same rules as class declarations, except you should omit any scoping directives or private data, and should use the word interface rather than class. Naming Conventions Except for reserved words and directives, which are in all lowercase, all Pascal identifiers should use Infixcaps, Which Means The First Letter Should Be A Capital, And Any Embedded Words in An Identifier Should Be in Caps, AS Well As An Acronym That IS Embedded: MyIdentifierMyftpClass

The major exception to this rule is in the case of header translations, which should always follow the conventions used in the header. For instance, write WM_LBUTTONDOWN, not wm_LButtonDown. Except in header translations, do not use underscores to separate words. Class names should ................ ..

Good Type Names:

AddressForm, ArrayIndexOfboundsexception

Bad Type Names:

ManageLayout // Verb Phrase

Delphi_is_new_to_me // understandcores

3.1 Unit Naming Use InfixCaps, as described at the beginning of this section See also the section on unit declarations 3.2 Class / Interface Naming Use InfixCaps, as described at the beginning of this section Begin each type declaration with a capital T:.. TMyType See also the section on class / interface declarations. 3.3 Field Naming Use InfixCaps, as described at the beginning of this section. Begin each type declaration with a capital F, and declare all data types in the private section, using properties or getters and setters to provide public access. for example, use the name GetSomething to name a function returning an internal field value and use SetSomething to name a procedure setting that value. Do not use all caps for const declarations except where required in header translations. Delphi is created in California, SO We discourage the use of notation, except where required in header translations: CORRECTFMYSTRING: STRING DIS

Incorrect

LPSTRMYSTRING: STRING;

The Exception To The Hungarian NOTATION RULE IS IN ENUMERATED TYPES. TBITBTNKIND = (BKCUSTOM, BKOK, BKCANCANCENCEL, BKHELP,

Bkyes, BKNO, BKCLOSE, BKABORT, BKRETRY,

Bkignore, bkall);

In this case the letters bk are inserted before each element of this enumeration. Bk stands for ButtonKind. When thinking about naming conventions, consider that one-character field names should be avoided except for temporary and looping variables. Avoid variable l ( "el" ) because it is hard to distinguish it from 1 ( "one") on some printers and displays. 3.4 Method NamingMethod names should use the InfixCaps style. Start with a capital letter, and capitalize the first letter of any subsequent word in the name, . as well as any letters that are part of an acronym All other characters in the name are lower case Do not use underscores to separate words Note that this is identical to the naming convention for non-constant fields;.. however it should always be Easy to distinguish the two from context. Method name Should Be Imperative Verbs or Verb phrases. Examples: // good method names:

Showstatus, Drawcircle, AddLayoutComponent

// Bad Method Names:

MouseButton // Noun Phrase; Doesn't Describe Function

Drawcircle // Starts with Lower-Case Letter

Add_Layout_component // Underscores

// The function of this method is unclear. Does

// IT Start The Server Running (Better: StartServer),

// or test WHETHER or Not IT Is Running

// (better: isserverrunning)?

Serverrunning // Verb Phrase, But Not Imperative

A Method To Get Or Set Some Property Of The Class Should Be Called GetProperty Or SetProperty Respective, Where Property Is The Name of the Property. Examples:

GetHeight, SetHeight

A Method to Test Some Boolean Property of The Class Should Be Called IsVisible, Where Visible Is The Name of The Property. Examples:

IsResizable, isvisible

3.5 Local Variable NamingLocal variables follow the same naming rules as field names, except you omit the initial F, since this is not a Field of an object. (See section 3.3). 3.6 Reserved Words Reserved words and directives should be all lowercase. This can be a bit confusing at times. For instance types such as Integer are just identifiers, and appear with a first cap. Strings, however, are declared with the reserved word string, which should be all lowercase. 3.7 Type Declarations All type declarations should begin with the letter T, and should follow the same capitalization specification laid out in the beginning of this section, or in the section on class declarations. 4.0 White Space Usage 4.1 Blank Lines Blank lines can improve readability by grouping sections of the code that are Logically Related. A Blank Line Should Also Be Used in The Following Places: After The Copyright Block Comment, Package Declaration, And Import Section. Between Class Declarations. Between Method Decl arations. 4.2 Blank Spaces Object Pascal is a very clean, easy to read language. In general, you do not need to add a lot of spaces in your code to break up lines. The next few sections give you some guidelines to follow when Placing Spaces in your code. 4.2.2 Blanks Should Not BE Used:

Between a method name and its opening parenthesis. Before or after a. (Dot) operator. Between a unary operator and its operand. Between a cast and the expression being cast. After an opening parenthesis or before a closing parenthesis. After an opening square Bracket [Or Before A Closing Square Bracket]. Before A SEMICOLON. EXAMPLES OF CORRECT USAGE: FUNCTION TMYCLASS.MYFUNC (VARUE: Integer);

MyPointer: = @MyRecord;

Myclass: = tmyclass (mypointer); Myinteger: = MyintegeraRray [5];

Examples of incorrect usage: function TMYCLASS.MYFUNC (VARUE: INTEGER);

MyPointer: = @ MyRecord;

Myclass: = TMYCLASS (MyPointer);

Myinteger: = MyintegeraRray [5];

4.3 Indentation You should always indent two spaces for all indentation levels. In other words, the first level of indentation is two spaces, the second level four spaces, the third level 6 spaces, etc. Never use tab characters. There are few exceptions. The reserved words unit, users, type, interface, implementation, initialization and finalization should always be flush with the margin. The final end statement at the end of a unit should be flush with the margin. In the project file, the word program, and the main begin and end block should all be flush with the margin. The code inside the begin..end block, should be indented at least two spaces. 4.4 Continuation Lines Lines should be limited to 80 columns. Lines longer than 80 columns should be broken into one or more continuation lines, as needed All the continuation lines should be aligned and indented from the first line of the statement, and indented two characters Always place begin statements on their own line Examples...:

// Correct

Function CreateWindowEx (DWEXStyle: DWORD;

LPClassName: pchar; lpwindowname: pchar;

DWStyle: DWORD; X, Y, NWIDTH, NHEIGHT: INTEGER;

HWNDPARENT: HWND; HMENU: HMENU; Hinstance: hinst;

LPPARAM: POINTER: hWnd; stdcall;

// Correct

IF ((x = y) or (y = x) OR

(Z = p) or (f = j) THEN

Begin

S: = j;

END;

Never wrap a line between a parameter and its type, unless it is a comma separated list, then wrap at least before the last parameter so the type name follows to the next line. The colon for all variable declarations contains no whitespace between it and the Variable. there. there. there. The color

Procedure foo (Param1: Integer; param2: integer);

// incorrect

Procedure foo (Param: Integer; param2: integer);

A continuation line should never start with a binary operator. [???] Avoid breaking a line where normally no white space appears, such as between a method name and its opening parenthesis, or between an array name and its opening square bracket. If . you must break under these circumstances, then one viable place to begin is after the opening parenthesis that follows a method name Never place a begin statement on the same line with any other code.Examples:

// incorrect

While (LONGEXPIRESSION1 or LONGEXPIPRESSION2) DO BEGIN

// DOSMETHING

// DOSMETHINGELSE;

END;

// Correct

While (LONGEXPRESSION1 or LONGEXPIPRESSION2) DO

Begin

// DOSMETHING

// DOSMETHINGELSE;

END;

// Correct

IF (LONGEXPIESSION1) OR

(LONGEXPRESSION2) OR

(Longexpression3) THEN

5.0 Comments The Object Pascal Language Supports Two Kinds of Comments: Block, and Single-Line Comments. Some General Guidelines for Comment Usage Include:

It is helpful to place comments near the top of unit to explain its purpose It is helpful to place comments before a class declaration It is helpful to place comments before some method declarations Avoid making obvious comments...:

i: = i 1; // add one to i

Remember that misleading comments are worse than no comments at all. Avoid putting any information into comments that is likely to become out of date. Avoid enclosing comments in boxes drawn with asterisks or other special typography. Temporary comments that are expected to be changed or removed later should be marked with the special tag "???:" so that they can easily be found afterwards Ideally, all temporary comments should have been removed by the time a program is ready to be shipped Example:.. // ??? : Change this to call sort by is fixedlist.mys;

. 5.1 Block Comments Object Pascal supports two types of block comments The most commonly used block comment is a pair of curly braces:.. {} The Delphi team prefers to keep comments of this type as spare and simple as possible For instance, you should avoid using asterisks to create patterns or lines inside your comments. Instead, make use of white space to break your comments up, much as you would in a word processing document. The words in your comments should start on the same line as the first curly Brace, As Shown in this Excerpt from dsgnintf.pas: {TPROPERTYEDITOR

Edits a Property of a Component, or List of Components,

SELECTED INTO The Object Inspector. The Property

Editor is created based on the Type of the

Property Being Edited as Determined by The Types

Registered by ...

ETC ...

GetxxxValue

Gets the value of the first property in the

Properties Property. Calls the appropriate

Tproperty getxxxvalue method to retrieve the

Value.

SetXxxValue Sets The Value of All The Properties

In The Properties Property. Calls the appropriate

Tproperty setxxxxvalue method.}

A block comment is always used for the copyright / ID comment at the beginning of each source file It is also used to "comment out" several lines of code Block comments used to describe a method should appear before the method declaration Example...: // Correct

{TMYOBJECT.MYMETHOD

This routine allows you to execute code.}

PROCEDURE TMYOBJECT.MYMETHOD;

Begin

END;

// incorrect

PROCEDURE TMYOBJECT.MYMETHOD;

{********************************************************** *****

TMYOBJECT.MYMETHOD

This Routine Allows you to execute code.

*********************************************************** *****}

Begin

END;

A second kind of block comment contains two characters, a parenthesis and an asterisk:. (* *) This is sometimes called starparen comments These comments are generally useful only during code development, as their primary benefit is that they allow nesting of comments,. as long as the nest level is less than 2. Object Pascal does not support nesting comments of the same type within each other, so really there is only one level of comment nesting:. curly inside of starparen, and starparen inside of curly As long as you do not nest them, any other standard Pascal comments between comments of this type will be ignored. As a result, you can use this syntax to comment out a large chunk of code that is full of mixed code and comments :( * Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

Dothis; // start the process

Dothat; // Continue Iteration

{We need a way to report errors here, Perhaps Using

a try or block ???}

Callmorecode; // firmize the process

end; *) In this example, the entire Button1Click method is commented out, including any of the subcomments found between the procedure's begin..end pair 5.2 Single-Line Comments A single-line comment consists of the characters // followed by text. . Include a single space between the // and the comment itself. Place single line comments at the same indentation level as the code that follows it. You can group single-line comments to form a larger comment. A single-line comment or comment group should always be preceded by a blank line, unless it is the first line in a block. If the comment applies to a group of several statements, then the comment or comment group should also be followed by a blank line. If it applies only To the next statement (Which May Be a Compound Statement), Then Do Not Follow It With a blank line.example: // open the database

Table1.open;

Single-line comments can also follow the code they reference. These comments, sometimes referred to as trailing comments, appear on the same line as the code they describe. They should have at least one space-character separating them from the code they reference. IF More Than One Trailing Comment Appears in a Block of Code, The Should All Be aligned to the Same Column.example:

IF (not isvisible) THEN

Exit; // Nothing to do

INC (Strlength); // Reserve Space for Null Terminator

Avoid commenting every line of executable code with a trailing comment. It is usually best to limit the comments inside the begin..end pair of a method or function to a bare minimum. Longer comments can appear in a block comment before the method or function declaration Classes 6.1 class body Organization The body of a class declaration should be organized in the following order:. Field declarations Method declarations Property declarations The fields, properties and methods in your class should be arranged alphabetically by name 6.1.1 Access levels Except for. Code Inserted by The Ide, The Scoping Directives for a class shop

Private declarations Protected declarations Public declarations Published declarations There are four access levels for class members in Object Pascal:.. Published, public, protected, and private - in order of decreasing accessibility By default, the access level is published In general, a member should be given the lowest access level which is appropriate for the member. for example, a member which is only accessed by classes in the same unit should be set to private access. Also, declaring a lower access level will often give the compiler increased opportunities for optimization. On the other hand, use of private makes it difficult to extend the class by sub-classing. If there is reason to believe the class might be sub-classed in the future, then members that might be needed by sub-classes SHOULD BE DECED Protected Instead of Private, And The Properties Used To Access Private Data Should Be Given Protected Status. You Should Never ALOW PUBLIC Access To Data. Data Should Always be declared in the private section, and any public access should be via getter and setter methods, or properties. 6.1.8 Constructor declarations Methods should be arranged alphabetically. It is correct either to place your constructors and destructors at the head of this list in the public section, or to arrange them in alphabetical order within the public section. If there is more than one constructor, and if you choose to give them all the same name, then sort them lexically by formal parameter list, with constructors having more parameters Always Coming After Those With Fewer Parameters. This Implies That a Constructor with no arguments IS Always The First One. for Greatest Compatibility with C

Builder, try to make the parameter lists of your constructors unique. C can not call constructors by name, so the only way to distinguish between multiple constructors is by parameter list. 6.2 Method Declarations If possible, a method declaration should appear on one line.Examples : // Broken Line is aligned Two Spaces in from Left.

Procedure ImageUpdate (Image IMG, Infoflags: Integer,

x: Integer, Y: Integer, W: Integer, H: Integer

InterfacesInterfaces Are Declared In a Manner That Runs Parallel To The Declaration for Classes:

InterfaceName = Interface ([Inherited Interface])

InterfaceBody

END;

An interface declaration should be indented two spaces. The body of the interface is indented by the standard indentation of four spaces. The closing end statement should also be indented two characters. There should be a semi-colon following the closing end statement. There are . no fields in an interface declaration Properties, however, are allowed All interface methods are inherently public and abstract;.. do not explicitly include these keywords in the declaration of an interface method Except as otherwise noted, interface declarations follow the same style guidelines as Classes. 7.1 Interface Body Organization The Body of An Interface Declaration Should Be Organized in The Following Order:

Interface method declarations Interface property declarations The declaration styles of interface properties and methods are identical to the styles for class properties and methods. 8.0 Statements Statements are one or more lines of code followed by a semicolon. Simple statements have one semicolon, while compound statements have More Than One Semicolon and ThereFore Consist of Multiple Simple Statements. Here Is A Simple Statement: A: = B; Here Is A Compound, OR Structure, Statement: Beginb: = C;

A: = B;

End; 8.0.1 Simple Statements A Simple Statement Contains a Single Semicolon. If You Need To Wrap The Statement, Indenter: =

MyValue (someverylongStateMent / OtherLongStatement);

8.0.1 Compound Statements Compound Statements Always End With a SEMICOLON

Mystatement;

MYNEXTSTATEMENT;

MylastStatement // SEMICOLON OPTIONAL

End; 8.1.1 Assignment and Expression Statements Each Line Should Contain At Most One Statement. for example:

A: = B C; INC (count); // incorrect

A: = B C; // Correct

INC; // Correct

8.1.2 Local variable declarations Local variables should have Camel Caps, that is, they should start with a capital letter, and have capital letters for the beginning of each embedded word. Do not preface variable names with an F, as that convention is reserved FOR FIELDS IN A Class Declaration: var

MyData: integer;

MyString: String;

You May Declare Multiple Identifiers of The Same Type On A Single Line: VAR

ArraySize, ArrayCount: Integer; This practice is discouraged in class declarations There you should place each field on a separate line, along with its type 8.1.3 Array declarations There should always be a space before the opening bracket "[" and after.. The closing bracket. TypetMyArray = array [0..100] of char;

8.2.3 IF Statement IF Statements SHOULD ALWAYS APPEAR ON AT Least Two Lines. EXAMPLE:

// incorrect

IF a

// Correct

IF a

DOSMETHING;

In Compound IF Statements, Put Each Element Separating Statements on a new line: Example:

// incorrect

IF a

DOSMETHING;

Dosomethingelse;

ELSE BEGIN

Dothis;

Dothat;

END;

// Correct

IF a

Begin

DOSMETHING;

Dosomethingelse;

end

Else

Begin

Dothis;

Dothat;

END;

Here Are A Few More Varids That Are Considered Valid:

// Correct

IF condition the

Begin

Dothis;

END ELSE

Begin

Dothat;

END;

// Correct

IF condition the

Begin

Dothis;

end

Else

DOSMETHING;

// Correct

IF condition the

Begin

Dothis;

END ELSE

DOSMETHING;

One That Has Fallen Out of Favor But Deserves Honorable Mention:

IF condition the

Dothis

Else Dothat;

8.2.4 for Statement Example:

// incorrect

For i: = 0 to 10 do begin

DOSMETHING;

Dosomethingelse;

END;

// Correct

For i: = 0 to 10 do

Begin

DOSMETHING;

Dosomethingelse;

END;

8.2.5 While Statement EXAMPLE:

// incorrect

While x

DOSMETHING;

Dosomethingelse;

END;

// Correct

While x

Begin

DOSMETHING;

Dosomethingelse;

END;

8.2.6 Repeat Until Statement EXAMPLE:

// Correct

Repeat

x: = j;

J: = UpdateValue;

Until J> 25;

8.2.7 Case Statement Example:

// Correct

Case Control.Align Ofalleft, Alnone: NEWRANGE: = Max (newrange, position);

Alright: Inc (Align Margin, Control.Width);

END;

// Correct

Case X of

CSSTART:

Begin

J: = UpdateValue;

END;

CSBEGIN: X: = J;

Cstimeout:

Begin

J: = x;

x: = UpdateValue;

END;

END;

// Correct

Case Scrollcode of

SB_LineUp, SB_Linedown:

Begin

InCr: = FinCrement Div Flinediv;

FinalInCr: = FinCrement Mod Flinediv;

Count: = flinediv;

END;

SB_PAGEUP, SB_PAGEDOWN:

Begin

InCr: = fpageincrement;

FinalInCr: = INCR MOD FPAGEDIV;

InCr: = Incr Div FPAGEDIV;

Count: = fpagediv;

END;

Else

Count: = 0;

INCR: = 0;

FinalInCr: = 0;

END;

8.2.8 Try Statement EXAMPLE:

// Correct

Try

Try

ENUMTHREADWINDOWS (CURRENTTHREADID, @disable, 0);

Result: = taskwindowlist;

Except

EnabletaskWindows (TaskWindowList);

Raise;

END;

Finally

Taskwindowlist: = savewindowlist;

TaskactiveWindow: = SaveActiveWindow;

END;

转载请注明原文地址:https://www.9cbs.com/read-5310.html

New Post(0)