Delphi Spring Framework編程規範(草稿)V0.1

 

Delphi Spring Framework編程規範(草稿)V0.1 (更新日期:2009-10-28)

1. 前言

爲保證Delphi Spring Framework項目代碼的可讀性質量,特建立本編程規範。本規範僅適用於Delphi Spring Framework各項目成員,大家如有任何意見和建議,請給我留言。

2. 所有項目文件、單元文件和示例程序必須包含下列版權聲明:

{***************************************************************************}
{                                                                           }
{           Delphi Spring Framework                                         }
{                                                                           }
{           Copyright (C) 2009-2010 Delphi Spring Framework                 }
{                                                                           }
{           http://delphi-spring-framework.googlecode.com                   }
{                                                                           }
{***************************************************************************}
{                                                                           }
{  Licensed under the Apache License, Version 2.0 (the "License");          }
{  you may not use this file except in compliance with the License.         }
{  You may obtain a copy of the License at                                  }
{                                                                           }
{      http://www.apache.org/licenses/LICENSE-2.0                           }
{                                                                           }
{  Unless required by applicable law or agreed to in writing, software      }
{  distributed under the License is distributed on an "AS IS" BASIS,        }
{  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{  See the License for the specific language governing permissions and      }
{  limitations under the License.                                           }
{                                                                           }
{***************************************************************************}

3. 命名規範

  • 核心單元文件均採用Spring.*.pas的命名方式,如:Spring.System.pas;
  • 類、記錄、枚舉、集合以及自定義類型以T開始,如TSimpleClass、TDriveType;
  • 接口類型一律以I開頭,如IEnumerable,IList<T>;
  • 異常類以E開頭,如EArgumentException,EFileNotFoundException;
  • 資源字符串(resourcestring)以S作爲前綴,如SFileNotFound;
  • 類字段成員、局部變量和參數應採用Camel風格,如fOperatingSystem, i, value, targetStream;
  • 類的屬性和方法一律採用Pascal風格,Boolean型屬性應使用Is前綴,如Name,IsReadOnly,IsValid,IsEmpty,GetNextID;
  • 抽象基類儘可能使用Base作爲後綴,如TCollectionBase,TStreamBase;
  • 自定義屬性類名無需前綴T(抽象屬性基類則應保留),且以Attribute作爲後綴,如:
    DisplayNameAttribute
    = class(TCustomAttribute)
    private
      fName
    : string;
    public
     
    constructor Create(const name: string);
    end;
    應用屬性時一般省略後綴Attribute,如:
    [DisplayName(‘Paul’)]
    TSomeClass
    = class
    end;
  • 所有保留字全部小寫,如procedure,string,begin,end等。

4. 縮進及換行

使用標準Pascal的縮進和換行方式,如:

for i := 0 to list.Count-1 do
begin
  if condition then
  begin
    DoSomething;
  end
  else
  begin
    //...
  end;
  case driveType of
    dtNetwork:
    begin
      //...
    end;
    else
    begin
      //...
    end;
  end;
end;

5. 使用Xml的風格註釋

  ///
  /// Provides version information for a physical file on disk.
  ///
  ///
  /// Remarks
  ///
  TFileVersionInfo = record
  //...
  end;

注意:爲便於各地開發者閱讀和使用代碼,請使用英文。

6. 必須檢查全局函數和所有公開的方法的參數合法性

Spring.System.TArgument類提供了很多靜態方法用來檢查參數,如CheckTrue,CheckFalse,CheckRange等。(可使用代碼模板減少輸入)

7. 全局變量、全局函數以及公開的靜態類方法儘可能是線程安全的

如不支持線程安全必須註明。

8. 編寫的代碼必須支持Unicode

注意:RTL中有些函數並不支持Unicode,如:

SysUtils.UpperCase

僅支持標準7位Ascii碼中’a’-‘z’的大小寫轉換,應使用Character.TCharacter類的ToUpper和ToLower方法

SysUtils.LowerCase

 

SysUtils.BytesOf

應使用WideBytesOf

SysUtils.StringOf

應使用WideStringOf

9. 除非特別需求,否則永遠不要把下列類型的文件上傳到svn服務器:

  • *.bak
  • *.dcu
  • *.exe
  • *.~*
  • *.ddp
  • *.dsk
  • *.local
  • *.identcache
  • *.tvsconfig
  • · __history (文件夾)

10. 其他建議

  • 儘可能將參數定義成constvarout
  • 應將抽象基類標記爲abstract
  • 工具類(utility class)應優先考慮使用record來實現
  • 儘可能保證每個過程職責清晰,一般不超過20行代碼
  • 儘可能保證代碼是可測試的,並使用DUnit編寫單元測試用例
  • 儘可能保持版本兼容性,使用deprecated, experimental等關鍵字提醒開發者
  • 謹慎使用class constructor和class destructor代替單元的初始化(initialization)和終止化(finalization)
  • 拋異常時優先使用Exception.CreateRes(@SResourceString)等方法

作者:左保權 (Paul)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章