Skip to main content
Thoughts from David Cornelius


Consider the following psuedocode fragment:

for each FieldValue in RecordList do begin
  if ValidSearchValue(FieldValue) then
    found := Search(FieldValue)
  else begin
    ShowMessage('Invalid search criteria: ' + FieldValue);
if found then

In Delphi, a warning will be generated near the bottom of this code where it says "if found ..." saying:

Variable 'found' might not have been initialized

To prevent that warning, you would initialize "found" just inside the "for each" loop with something like found := False;

But then, Delphi emits the following hint:

Value assigned to 'found' never used

It seems that Delphi's Hints are smarter than its Warnings. It knows that with the "Continue" statement, the "found" value will either be overwritten or simply not used at all because the Continue statement takes control to the top of the next iteration.

Unfortuneately, the Warning is generated even though the "if found" section will never be encountered unless the found variable was already initialized.

The work around? Well, you could turn off hints, but I wouldn't advise it. I keep all hints on and strive to eliminate every one of them. It makes my code much cleaner and prevents headaches down the road.

Instead, simply moving the "found" initialization outside the "for" loop gets rid of the hint and suppresses the warning.

Add new comment

The content of this field is kept private and will not be shown publicly.