Fpsonar units and imports

← Back to fpsonar

Eleven rules about uses clauses, unit files and program files. The recurring theme is dependency hygiene: a uses clause is a promise about what a unit needs, and it goes stale faster than any other part of a file.

Pascal gives you two uses clauses per unit — one in the interface section and one in the implementation. The distinction matters: an interface uses propagates to everyone who uses your unit, an implementation uses does not.

RemoveUnusedImports

edit

minor · on by default

A unit in a uses clause that nothing in the file references. It slows compilation, drags in transitive dependencies, and misleads a reader about what the unit depends on.

fpsonar complains about this

uses
  Classes, SysUtils, Math;    // Math never referenced

fpsonar accepts this

uses
  Classes, SysUtils;

MoveImportToImplementation

edit

minor · on by default

A unit used only by the implementation does not belong in the interface uses. Moving it down shrinks what your unit forces on its own consumers, and often breaks a circular dependency.

fpsonar complains about this

interface

uses
  Classes, Math;              // Math used only in the implementation

type
  TReport = class
    function Total: Integer;
  end;

implementation

fpsonar accepts this

interface

uses
  Classes;

type
  TReport = class
    function Total: Integer;
  end;

implementation

uses
  Math;

InterfaceUsesTooBroad

edit

minor · off by default

The stricter, project-wide form of the rule above: it looks across the whole project rather than one file, so it can see that no interface declaration references the unit even when the implementation does.

fpsonar complains about this

interface

uses
  Classes, DateUtils;         // no interface declaration mentions DateUtils

fpsonar accepts this

interface

uses
  Classes;

implementation

uses
  DateUtils;

FullyQualifiedImports

edit

minor · on by default

When two used units both declare a name, which one you get depends on the order of the uses clause — and reordering it silently changes meaning. Qualify the reference with its unit name so it cannot drift.

fpsonar complains about this

uses
  UnitA, UnitB;               // both declare Log

begin
  Log('x');                   // which Log?
end;

fpsonar accepts this

begin
  UnitB.Log('x');
end;

DisallowedImportByPath

edit

major · on by default · parameter targets

Reports units whose path matches a list you configure. Unlike the other Disallowed* rules this matches on location rather than name, which is what you want for architectural boundaries: a rule unit must not reach into the vendored parser directory, a UI unit must not use anything under db/.

{ "rules": { "DisallowedImportByPath":
    { "enabled": true, "params": { "targets": ["*/vendor/*", "*/internal/*"] } } } }

fpsonar complains about this

uses
  Vendor.Parser.Internals;

fpsonar accepts this

uses
  Vendor.Parser;              // the public surface

UnitFileNameCaseMismatch

edit

minor · off by default

The file name should be the lowercase form of the unit name — report.pp for unit Report — or the unit name verbatim. Anything else builds on a case-insensitive filesystem and fails on a case-sensitive one, which is how a Windows-developed unit breaks on Linux.

fpsonar complains about this

// in a file named Report_Main.pp
unit ReportMain;

fpsonar accepts this

// in a file named reportmain.pp
unit ReportMain;

MissingModeDirective

edit

minor · off by default

A unit with no {$mode} directive inherits its dialect from the command line. The same source then compiles differently depending on how it is built — and string, object and operator behaviour all change with the mode. State it in the file.

fpsonar complains about this

unit Report;

interface

fpsonar accepts this

unit Report;

{$mode objfpc}{$H+}

interface

UnitNotEmpty

edit

minor · on by default

A unit that declares nothing and does nothing. Usually a placeholder that was never filled in, or one whose contents moved elsewhere.

fpsonar complains about this

unit Report;

interface

implementation

end.

fpsonar accepts this

// delete the file, or give it its intended contents

NoLegacyInitializationSection

edit

minor · on by default

A unit can end with a bare begin..end. block, which runs at initialisation. The explicit initialization keyword says the same thing unambiguously, and pairs with finalization so cleanup has an obvious home.

fpsonar complains about this

implementation

begin
  Register;
end.

fpsonar accepts this

implementation

initialization
  Register;

finalization
  Unregister;

end.

ProjectFileNoRoutines

edit

minor · on by default

A routine declared in the .lpr/.dpr program file cannot be used or tested from anywhere else. Move it into a unit, where it can be.

fpsonar complains about this

program Reporter;

procedure Run;                // unreachable from any unit
begin
end;

begin
  Run;
end.

fpsonar accepts this

program Reporter;

uses
  Runner;

begin
  Runner.Run;
end.

ProjectFileNoVariables

edit

minor · on by default

The same for a global variable in the program file: nothing else can reach it, so it is invisible state at the top of the application.

fpsonar complains about this

program Reporter;

var
  Verbose: Boolean;

begin
end.

fpsonar accepts this

program Reporter;

uses
  Config;                     // Config.Verbose, reachable and testable

begin
end.
edit

Unit naming is in naming; the dotted-unit rules — which check the FPC_DOTTEDUNITS guard and the alias table — are in FPC source-tree conventions.

See also

edit
  • fpsonar — what fpSonar is, and how to run it

← Back to fpsonar