All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.delphi.rules.community-delphi.VariableInitialization.html Maven / Gradle / Ivy

There is a newer version: 1.12.1
Show newest version

Why is this an issue?

Local variables must be initialized before use to ensure the code behaves deterministically and in an error-free manner.

Unlike object fields and global variables, contents of local variables are generally undefined until initialized. The only exceptions are the following managed types:

  • Strings
  • Variants
  • Interfaces
  • Arrays of the above types

How to fix it

Assign the variable with an initial value before using it.

procedure GetCount(MyList: TList);
var
  ListSize: Integer;
begin
  if (MyList.Count > 0) then begin
    ListSize := MyList.Count;
  end;

  Writeln(Format('The list has %d elements', [ListSize]));
end;
procedure GetCount(MyList: TList);
var
  ListSize: Integer;
begin
  ListSize := 0;
  if (MyList.Count > 0) then begin
    ListSize := MyList.Count;
  end;

  Writeln(Format('The list has %d elements', [ListSize]));
end;

Resources





© 2015 - 2024 Weber Informatics LLC | Privacy Policy