Source Code Files, Units and Their Structure

From RAD Studio
Jump to: navigation, search

Go Up to Delphi’s Object Pascal Style Guide


Object Pascal source code is divided primarily into units and Delphi project files, which both follow the same coding conventions. A Delphi Project file has a .dpr file extension. It is the main source file for a project. Any units used in the project will have a .pas file extension.

Being identifiers, source code file names should follow the Pascal Casing rule (they also need to be valid file names for the file system).

We recommend using dotted unit names (unit scope names) referring to the library or area the unit is part of, such as Vcl.Forms or System.SysUtils. We also suggest each third-party vendor and customer to follow a similar approach.

Copyright, Directives, Resource Inclusion Blocks

Object Pascal units should contain a copyright/ID block comment before the unit name, followed by any conditional defines, compiler directives, or include statements, then the uses clause with the actual code of the unit. At least one blank line should separate each of these elements. The copyright notice should contain at least the following line:

Copyright (c) year-list copyright-holder.

An actual example:

{*******************************************************}
 {                                                       }
 {             Delphi Visual Component Library           }
 {                                                       }
 {  Copyright(c) 1995-2021 Embarcadero Technologies, Inc.}
 {              All rights reserved                      }
 {                                                       }
 {*******************************************************}
 unit Vcl.Buttons;
 {$S-,W-,R-,H+,X+}
 interface

Resource inclusion directives should be at the beginning of the implementation section after the implementation section uses a statement.

implementation
uses
  System.Types, Vcl.Consts, ..., System.Math;
{$R Buttons.res}

Any data type or symbol used in a single unit should be declared in the implementation section of the unit, not in its interface section.

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 units starting on the following line with an indentation of 2 spaces:

uses 
  MyUnit;

When referring to a unit with a dotted name, the complete name should be listed (that is, System.SysUtils and not SysUtils). Each unit must be separated from its neighbor by a comma followed by a space:

uses
  System.Types, Vcl.Consts, System.SysUtils, Vcl.ActnList, Winapi.UxTheme,
  Winapi.DwmApi, System.Math;

You may format the list of units in your uses clause so that they wrap to a new line (as in the example above). For better support of differencing in version control systems, it is considered acceptable to format uses statements so that each unit app appears on a separate line:

uses
  System.Types, 
  Vcl.Consts, 
  System.SysUtils,
  System.Math;

Next

White Space Usage