AL icon indicating copy to clipboard operation
AL copied to clipboard

The value assigned to a variable must be used.

Open MarcHansenMicrosoft opened this issue 6 years ago • 6 comments

Title The value assigned to a variable must be used.

Description The value assigned to a variable must be used. Else the variable is not necessary.

Reason for the rule Unused variable can be confusing for the reader and can cost performance to the system.

Bad code sample

procedure AddEntities@1(FilterStr@1000 : Text[250])
var
    Vendor@1001 : Record Vendor;
    Count@1002 : Integer;
begin
    Count := 0;
    Vendor.SETFILTER("No.",FilterStr);
    if Vendor.FINDSET() then
    repeat
        "User ID" := USERID(); 
        "Vendor No." := Vendor."No.";
        if INSERT() then 
            Count += 1;
    until Vendor.NEXT() = 0; 
end;

Good code sample

procedure AddEntities@1(FilterStr@1000 : Text[250])
var
    Vendor@1001 : Record Vendor;
begin
    Vendor.SETFILTER("No.",FilterStr);
    if Vendor.FINDSET() then
    repeat
        "User ID" := USERID(); 
        "Vendor No." := Vendor."No.";
        if INSERT() then;
    until Vendor.NEXT() = 0; 
end;

Good and bad practices for fixing the rule Remove the variable or make use of it if not using it was a mistake.

Remarks

MarcHansenMicrosoft avatar Jan 28 '20 13:01 MarcHansenMicrosoft

As long as its an info/warning message and not an error, I'm OK with this.

Because of the debugger limitations, I often have variables that exist only for that.

mjmatthiesen avatar Feb 06 '20 17:02 mjmatthiesen

We currently ship every rule in CodeCop as info or warning.

MarcHansenMicrosoft avatar Feb 07 '20 15:02 MarcHansenMicrosoft

How should a List be assigned before use?

The following sample shows a warning due to using Contains in the loop, I do not see a good way around it. Suggestions? (My actual code is different, so the sample was just to make it easy to understand)

    procedure DoSomething()
    var
        GLEntry: Record "G/L Entry";
        GLAccountNoList: List of [Code[20]];
    begin
        // Set some filters on GLEntry
        if GLEntry.FindSet() then
            repeat
                DoSomethingWithEntry(GLEntry);
                if not GLAccountNoList.Contains(GLEntry."G/L Account No.") then
                    GLAccountNoList.Add(GLEntry."G/L Account No.");
            until GLEntry.Next() = 0;
        DoSomethingToEachGLAccount(GLAccountNoList);
    end;

    procedure DoSomethingWithEntry(GLEntry: Record "G/L Entry")
    begin
    end;

    procedure DoSomethingToEachGLAccount(GLAccountNoList: List of [Code[20]];)
    begin
    end;

image

AskeHolst avatar Feb 10 '20 09:02 AskeHolst

@AskeHolst I think you're looking for issue #5614

mjmatthiesen avatar Feb 11 '20 04:02 mjmatthiesen

Thank you @mjmatthiesen #5614 was the right one. And there was even a solution there: "List is initialized via .Add()" so problem solved.

AskeHolst avatar Feb 11 '20 06:02 AskeHolst

So isn't this issue done, and can be closed?

dzzzb avatar Apr 06 '21 11:04 dzzzb