转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Object Pascal Style Guide         ★★★★

Object Pascal Style Guide

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3911 更新时间:2009/4/23 18:42:47
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] 


[系统软件]14.5.10.1 Object creation expressions  [VB.NET程序]VB.Net中文教程(8)  对象(Object)基本概念
[Delphi程序]The Delphi Object Model (PART III)  [Delphi程序]The Delphi Object Model (PART II)
[Delphi程序]The Delphi Object Model (PART I)  [Delphi程序]Object Pascal:从对象指针谈起
[Delphi程序]浅谈Object Pascal的指针  [Delphi程序]kmp模式匹配算法的pascal实现
[Delphi程序]Object TreeView简要说明  [Delphi程序]Pascal 精要--第一章
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台