skip to content

Delphi Hints vs. Warnings

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);
    Continue;
  end;
end;
 
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.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • You can use context links in the text to create context-related links to pages or sites that provide additional information about a word or phrase.
  • Scripture references will be linked automatically to an online Bible. E.g. John 3:16, Eph 2:8-9 (ESV).
  • You may link to webpages through the weblinks registry

More information about formatting options

Powered by Drupal. CrystalX theme created by Nuvio | Webdesign.