Can you spot the error in the following (somewhat artificial) Delphi code?
unit ResultCreator; interface implementation type TResultMode = (Worst, Bad, Good, Better, Best); function GetResult(Year : Integer) : TResultMode; begin .. end; procedure Process; const adjustment = 1; var Arr : array[0..3] of Integer; Idx : TResultMode; Elem : Integer; begin Arr := 55; Arr := 66; Arr := 77; Arr := 88; Idx := GetResult(2016); case Idx of Good : Elem := 2; Better : Elem := 0; Best : Elem := 1; else Elem := 3; end; Writeln(IntToStr(Arr[Elem-Adjustment])); end; end.
This was quite easy, right?
Solution: This code will at least sometimes result in an runtime error, when the local variable Elem contains the value 0. The array index for Arr on the last line of code will then be invalid (-1).
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 Strong Warnings Report, in a section STWA4 "Index error".
Pascal Analyzer has a total of 53 reports. Those are divided into over 230 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.