打印本文 打印本文 关闭窗口 关闭窗口
Object Pascal Style Guide
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3914  更新时间:2009/4/23 18:42:47  文章录入:mintao  责任编辑:mintao
n the future, then members that might be needed by sub-classes should be declared protected instead of private, and the properties used to access private data should be given protected status.

You should never allow 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 (if it exists) is always the first one. For greatest compatibility with C++Builder, try to make the parameter lists of your constructors unique. C++ cannot 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) 

Interfaces

Interfaces 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:

  1. Interface method declarations
  2. 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 structured, statement:

begin
  B := C;
  A := B;
end;

8.0.1 Simple Statements

A simple statement contains a single semicolon. If you need to wrap the statement, indent the second line two spaces in from the previous line:

  MyValue := 
    MyValue + (SomeVeryLongStatement / OtherLongStatement);

8.0.1 Compound Statements

Compound Statements always end with a semicolon, unless they immediately precede an end statement, in which case the semicolon is optional.

begin
  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(Count); 		// 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.

type
  TMyArray = array [0..100] of Char;

8.2.3 if statement

If statements should always appear on at least two lines.

Example:

  // INCORRECT
  if A < B then DoSomething; 
  
  // CORRECT
  if A < B then 
    DoSomething;

In compound if statements, put each element separating statements on a new line:

Example:

  // INCORRECT
  if A < B then begin
    DoSomething; 
    DoSomethingElse;
  end else begin
    DoThis;
    DoThat;
  end;
  
  // CORRECT
  if A < B then 
  begin
    DoSomething; 
    DoSomethingElse;
  end 
  else 
  begin
    DoThis;
    DoThat;
  end;
  

Here are a few more variations that are considered valid:

  // CORRECT
  if Condition then
  begin
    DoThis;
  end else
  begin
    DoThat;
  end;

  // CORRECT
  if Condition then
  begin
    DoThis;
  end
  else
    DoSomething;

  // CORRECT
  if Condition then
  begin
    DoThis;
  end else
    DoSomething;

  

One that has fallen out of favor but deserves honorable mention:

  if Condition then
    DoThis
  else DoThat;
  

8.2.4 for statement

Example:

  // INCORRECT
  for i := 0 to 10 do begin
    DoSomething; 
    DoSomethingElse;
  end;
  

  // CORRECT
  for i := 0 to 10 do 
  begin
    DoSomething; 
    DoSomethingElse;
  end;

  

8.2.5 while statement

Example:

  // INCORRECT
  while x < j  do begin
    DoSomething; 
    DoSomethingElse;
  end;
  

  // CORRECT
  while x < j do 
  begin
    DoSomething; 
    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 of
    alLeft, alNone: NewRange := Max(NewRange, Position);
    alRight: Inc(AlignMargin, 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;



上一页  [1] [2] [3] [4] 

打印本文 打印本文 关闭窗口 关闭窗口