← Back to fpsonar
Object Pascal has strong naming habits: types start with T, interfaces with I,
pointers with P, fields with F. These rules check that habit. All fifteen are
on by default and minor — they never mean your code is broken, only that a
reader has to work harder than necessary.
Every rule here has a pattern parameter holding a regular expression. If your
project uses a different convention, change the pattern rather than switching the
rule off. The default is given with each rule.
ClassNaming
editminor · on by default · pattern ^T[A-Z][A-Za-z0-9]*$
A class type should be named with a T prefix followed by PascalCase — TCustomer,
TFileReader. The T tells a reader "this is a type" at the point of use, which
matters in Pascal because types and variables share one namespace.
fpsonar complains about this
type
Customer = class // no T prefix
FName: string;
end;
fpsonar accepts this
type
TCustomer = class
FName: string;
end;
RecordNaming
editminor · on by default · pattern ^T[A-Z][A-Za-z0-9]*$
Records follow the same convention as classes: a T prefix and PascalCase.
fpsonar complains about this
type
point3d = record
X, Y, Z: Double;
end;
fpsonar accepts this
type
TPoint3D = record
X, Y, Z: Double;
end;
InterfaceNaming
editminor · on by default · pattern ^I[A-Z][A-Za-z0-9]*$
Interfaces take an I prefix — IReader, IComparable. The distinct prefix is
useful because an interface variable behaves differently from a class variable:
it is reference-counted, and you do not call Free on it.
fpsonar complains about this
type
TReader = interface // T prefix suggests a class
function Read: string;
end;
fpsonar accepts this
type
IReader = interface
function Read: string;
end;
EnumNaming
editminor · on by default · pattern ^T[A-Z][A-Za-z0-9]*$
An enumeration is a type, so it takes the T prefix like any other.
fpsonar complains about this
type
LogLevel = (llDebug, llInfo, llError);
fpsonar accepts this
type
TLogLevel = (llDebug, llInfo, llError);
PointerNaming
editminor · on by default · pattern ^P[A-Z][A-Za-z0-9]*$
A pointer type takes a P prefix, conventionally mirroring the type it points at:
PCustomer for TCustomer. Seeing P at a use site is a reminder that the value
can be nil and that you are responsible for what it points to.
fpsonar complains about this
type
TCustomerRef = ^TCustomer;
fpsonar accepts this
type
PCustomer = ^TCustomer;
HelperNaming
editminor · on by default · pattern ^T[A-Z][A-Za-z0-9]*Helper$
A class or record helper adds methods to a type you may not own. Naming it with a
Helper suffix makes clear that it is an extension rather than a type in its own
right.
fpsonar complains about this
type
TStringTools = class helper for string
function IsBlank: Boolean;
end;
fpsonar accepts this
type
TStringHelper = class helper for string
function IsBlank: Boolean;
end;
AttributeNaming
editminor · on by default · pattern ^[A-Z][A-Za-z0-9]*Attribute$
A custom attribute class is written without the suffix where it is applied —
[Table] — but declared with it. The suffix is what lets a reader tell the
attribute declaration apart from an ordinary class. Note there is no T prefix
here: attributes are the one type family that omits it.
fpsonar complains about this
type
TTable = class(TCustomAttribute)
end;
fpsonar accepts this
type
TableAttribute = class(TCustomAttribute)
end;
FieldNaming
editminor · on by default · pattern ^F[A-Z][A-Za-z0-9]*$
Fields of a class or record take an F prefix. This exists mainly so a property
and its backing field can have the same name without colliding: property Name
stores into field FName.
fpsonar complains about this
type
TCustomer = class
private
name: string;
public
property Name: string read name write name;
end;
fpsonar accepts this
type
TCustomer = class
private
FName: string;
public
property Name: string read FName write FName;
end;
ConstantNaming
editminor · on by default · pattern ^[A-Z][A-Za-z0-9_]*$
A declared constant starts with a capital letter. Underscores are allowed, so both
MaxRetries and MAX_RETRIES pass; maxRetries does not.
fpsonar complains about this
const
maxRetries = 3;
fpsonar accepts this
const
MaxRetries = 3;
VariableNaming
editminor · on by default · pattern ^[A-Z][A-Za-z0-9]*$
Variables start with a capital letter under the default pattern. Many FPC projects
prefer a lowercase l prefix for locals (lCount) or a for arguments
(aCount); if yours does, set pattern to match — this is exactly the rule you
are expected to configure rather than obey blindly.
fpsonar complains about this
var
itemCount: Integer;
fpsonar accepts this
var
ItemCount: Integer;
RoutineNaming
editminor · on by default · pattern ^[A-Z][A-Za-z0-9]*$
Procedures and functions start with a capital letter. A useful habit beyond the
pattern: name a procedure for the action it performs (SaveToFile) and a function
for the value it returns (ItemCount).
fpsonar complains about this
procedure saveToFile(const AName: string);
fpsonar accepts this
procedure SaveToFile(const AName: string);
ConstructorNaming
editminor · on by default · pattern ^Create[A-Za-z0-9]*$
A constructor is called Create, or Create followed by a qualifier —
CreateFromFile, CreateEmpty. Pascal does not require this, but every reader
expects it, and code that calls TFoo.Create unthinkingly is code that works.
fpsonar complains about this
type
TCustomer = class
constructor Init(const AName: string);
end;
fpsonar accepts this
type
TCustomer = class
constructor Create(const AName: string);
end;
UnitNaming
editminor · on by default · pattern ^[A-Z][A-Za-z0-9.]*$
A unit name starts with a capital letter, and may contain dots for namespaced
units. Note this checks the name in the unit clause; whether the file name
matches it is a separate rule, UnitFileNameCaseMismatch, in
FPC source-tree conventions.
fpsonar complains about this
unit customerData;
fpsonar accepts this
unit CustomerData;
DescendantNamingConvention
editminor · on by default
Some base classes imply a naming convention for everything that descends from
them: a form descendant is expected to be named a certain way, an exception class
to start with E. This rule checks that a type follows the convention its base
type requires, which the flat per-kind rules above cannot see.
fpsonar complains about this
type
TInvalidInput = class(Exception) // exceptions conventionally start with E
end;
fpsonar accepts this
type
EInvalidInput = class(Exception)
end;
ConsistentNameCasing
editminor · on by default
Pascal is case-insensitive, so MyValue, myvalue and MYVALUE are the same
identifier and all compile. This rule reports a use spelled differently from the
declaration. It is worth having because the compiler will never tell you, and
inconsistent casing defeats text search and makes diffs noisier than they need to
be.
fpsonar complains about this
var
ItemCount: Integer;
begin
itemcount := 0; // same variable, different spelling
end;
fpsonar accepts this
var
ItemCount: Integer;
begin
ItemCount := 0;
end;
Two rules that live elsewhere
editIdentifierTooShort and DisallowedIdentifier also concern names but are
configured differently — they take a length limit and a list of banned words
rather than a pattern. They are in declarations.
See also
edit- fpsonar — what fpSonar is, and how to run it
← Back to fpsonar