title | description | ms.author | ms.date | ms.topic | author | ms.reviewer |
---|---|---|---|---|---|---|
CodeCop Warning AA0205 |
Always initialize a variable before usage. |
solsen |
05/14/2024 |
reference |
SusanneWindfeldPedersen |
solsen |
Variables must be initialized before usage.
Always initialize a variable before usage. This can improve readability and make debugging easier.
Using uninitialized variables may lead to strange behavior, the code may become harder to debug and in some cases readability of the code can decrease.
var
x: Integer;
z: Integer;
procedure Proc1()
var
begin
Proc2(z);
x := z;
end;
procedure Proc2(z: Integer)
var
begin
z := 10;
end;
var
x: Integer;
z: Integer;
procedure Proc1()
var
begin
Proc2(z);
x := z;
end;
procedure Proc2(var z: Integer)
var
begin
z := 10;
end;
procedure TestFunc()
var
a: Integer;
b: Integer;
begin
b := a + 1;
end;
procedure TestFunc()
var
a: Integer;
b: Integer;
begin
a := 0;
b := a + 1;
end;
Variables of the following types are analyzed: Integer
, BigInteger
, Boolean
, Byte
, Char
, Code
, Date
, DateTime
, Decimal
, List
, Record
, and Time
.
Some variables need special initialization, for example, List
is initialized using .Add()
, Record
is initialized via .Init()
, .Clear()
, or assignment. Local record variables and arrays are ignored. Global record variables can be initialized in other method.
Initialization is possible using evaluate, for, foreach
statements. If a variable is passed in a method through var
, the rule considers it initialized in the method.