Fpsonar calls

← Back to fpsonar

Seven rules about call sites: RTL routines whose contract is easy to get subtly wrong, and calls that can be written more directly.

AssertWithoutMessage

edit

minor · on by default

Assert(C) without a message raises EAssertionFailed carrying only a file and line. Assert(C, 'why') carries the reason, which is the whole value of an assertion when one fires in a log six months later.

fpsonar complains about this

Assert(Count > 0);

fpsonar accepts this

Assert(Count > 0, 'Build called before any lines were loaded');

Note the related trap in exceptions: assertions vanish under {$C-}, so nothing load-bearing may live inside the asserted expression.

IfThenNotShortCircuit

edit

major · on by default

IfThen is a function, not a statement, so Pascal evaluates both value arguments before calling it. Unlike if, it does not short-circuit — which means a guard written with IfThen does not guard.

This is a genuine bug source: IfThen(Assigned(L), L.Count, 0) evaluates L.Count even when L is nil.

fpsonar complains about this

N := IfThen(Assigned(L), L.Count, 0);    // L.Count runs even when L is nil

fpsonar accepts this

if Assigned(L) then
  N := L.Count
else
  N := 0;

SingleOverloadOfMathFunction

edit

minor · on by default

Several math routines have both Single and Double/Extended overloads, and argument types decide which you get. Binding to the Single version loses precision silently — the result is a valid number, just a less accurate one.

fpsonar complains about this

var
  X: Single;
  R: Double;
begin
  R := Sqrt(X);               // computed at Single precision, then widened
end;

fpsonar accepts this

var
  X: Single;
  R: Double;
begin
  R := Sqrt(Double(X));
end;

TListLastByIndex

edit

minor · on by default

L[L.Count - 1] says "the element before the count" and breaks silently on an empty list. L.Last says what you mean.

fpsonar complains about this

Item := L[L.Count - 1];

fpsonar accepts this

Item := L.Last;

ExplicitDefaultArrayProperty

edit

minor · on by default

A class with a default array property can be indexed directly: L[0] rather than L.Items[0]. Where the two are equivalent, the shorthand is the convention.

fpsonar complains about this

Item := L.Items[0];

fpsonar accepts this

Item := L[0];

Two more call-site rules, documented elsewhere

edit

StringListDuplicatesNeedsSorted and DefaultFormatSettingsInDateFormat are also about calling an RTL routine correctly, but what goes wrong in both is about TStringList semantics and locale settings respectively, so they are in strings.

IOResultNotChecked

edit

major · off by default

An I/O call under {$I-} whose IOResult nothing reads before the next I/O call.

{$I-} switches off automatic I/O error checking: instead of raising, a failed call sets IOResult, which you are then expected to read. Reading it also clears it — so if you never read it, the error is discarded, and the code carries on as though the file had opened.

The rule is satisfied by a read of IOResult before the following I/O call, so checking after each operation is what it asks for.

fpsonar complains about this

{$I-}
Reset(lFile);
ReadLn(lFile, lLine);         // the Reset may have failed
{$I+}

fpsonar accepts this

{$I-}
Reset(lFile);
if IOResult <> 0 then
  Exit;
ReadLn(lFile, lLine);
{$I+}
edit

Format argument checking — count, types, malformed specifiers — is in strings. FreeAndNil and constructor-call rules are in memory and lifetime. OverrideOnlyCallsInherited and RedundantInherited are in classes and interfaces, with the rest of the inheritance rules. Byte counts handed to BlockRead/BlockWrite are in portability.

See also

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

← Back to fpsonar