AL icon indicating copy to clipboard operation
AL copied to clipboard

Variables must be initialized before usage.

Open MarcHansenMicrosoft opened this issue 6 years ago • 5 comments

Title 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 sample - developer's error - forgot to mark variable z with var

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 sample - developer's error - forgot to mark variable z with var - fixed

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 sample 2

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

Good code sample 2

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

Good and bad practices for fixing the rule Initialize variable

Remarks ​Variables of the following types are analyzed: Integer, BigInteger, Boolean, Byte, Char, Code, Date, DateTime, Decimal, List, Record, Time Some variables need special initialization, e.g. List is initialized via .Add(), Record 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 via evaluate, for, foreach statements If variable is passed in method via var, the rule consider it is initialized in the method

MarcHansenMicrosoft avatar Jan 22 '20 08:01 MarcHansenMicrosoft

Personally, I don't see the need for the init. You should know the default values of these variable type.

Bad code sample 2 Good code sample 2

Nothing changes except more lines. It's explicit, but didn't add value.

Same applies to bools being false by default.

mjmatthiesen avatar Feb 06 '20 17:02 mjmatthiesen

I agree for this simple example, but when it gets more complicated it is not that easy to spot as a developer.

    procedure TestFunc()
    var
        a: Integer;
        b:​ Integer;
    begin
...
// A lot of code 
...
​        b := a + 1;
    end;

In the A lot of code are there one code path that did not assign a value to a ???

MarcHansenMicrosoft avatar Feb 07 '20 15:02 MarcHansenMicrosoft

If you have so much code in function that its not easy to spot the value of a variable, then you should refactor this function. Move code to new functions, make it readable. Then there is no need to explicitly init these variables.

StefanMaron avatar Sep 09 '20 05:09 StefanMaron

Isn't this one done and can be closed, too?

dzzzb avatar Apr 06 '21 11:04 dzzzb

The suggestion has been addressed.

atoader avatar Apr 09 '21 09:04 atoader