Muokkaa

Jaa


CodeCop Warning AA0205

Variables must be initialized before usage.

Description

Always initialize a variable before usage. This can improve readability and make debugging easier.

Reason for the rule

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.

Bad code example 1

var
    x: Integer;
    z: Integer;

procedure Proc1()
var
begin
    Proc2(z);
    x := z;
end;

procedure Proc2(z: Integer)
var
begin
    z := 10;
end;

Good code example 1

var
    x: Integer;
    z: Integer;

procedure Proc1()
var
begin
    Proc2(z);
    x := z;
end;

procedure Proc2(var z: Integer)
var
begin
    z := 10;
end;

Bad code example 2

 procedure TestFunc()
    var
        a: Integer;
        b:​ Integer;
    begin
​        b := a + 1;                                                                                                                                                                                                       
    end;

Good code example 2

procedure TestFunc()
    var
        a: Integer;
        b: Integer;
    begin
        a := 0;
        
        b := a + 1;
    end;

Remarks

​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.

CodeCop Analyzer
Get Started with AL
Developing Extensions