Can you find this Delphi coding error (part 3)?

Can you spot the error in the following Delphi code? It is an error that will NOT manifest itself as a runtime error. Instead it will quietly pass by unobserved, which is even worse...

function GetNet(Yield, Balance : Integer) : Integer;
var
  ModBalance, ExtraYield : Integer;
begin
  Result := 0;
  ModBalance := Balance + GetDiffs(Yield);
  ExtraYield := GetExtraYield(Yield);
  Inc(Yield, ExtraYield);
  Result := ModBalance + ExtraYield;
end;

OK, not too hard, was it?

Solution: The parameter Yield is passed by value to the function. It is however modified inside the function code. This modified value will not be returned to the caller, and thus is pointless. The parameter should have been passed as "var", if the value was intended to be modified.

Not so hard to spot! But imagine finding this problem among thousands of lines of code spread over hundreds of units! And consider also a code base that is evolving all the time, so the code must constantly be rechecked.

The good news is that with Pascal Analyzer, our popular static code analyzer, you can automatically find these and many other kinds of coding issues. This particular error above is detected by the Warnings Report, in a section WARN11 "Value parameters that are set".

Pascal Analyzer has a total of 53 reports. Those are divided into roughly 200 sections, all providing statistics, references, errors and warnings for your code. Pascal Analyzer can be run as a standalone Windows application, or as a command-line program. The second choice is suitable if you want to integrate static code analysis with your build process.

Or use Pascal Expert, our RAD Studio plugin, to find the problems immediately while coding. Pascal Expert contains much of the same capabilities like Pascal Analyzer, and is obtainable with a discount when buying both products.

See our orders page for more details or get free evaluations from the download page.