AL icon indicating copy to clipboard operation
AL copied to clipboard

Do not declare parameters by reference if their values are never changed.

Open MarcHansenMicrosoft opened this issue 6 years ago • 3 comments

Title Do not declare parameters by reference if their values are never changed.

Description Unintentional value changes might propagate. In addition, it might lead other developers to think that value changes are intended.

Reason for the rule Defining a parameter as passed by reference and not actually changing it may lead to an unexpected variable value if the variable is later changed in the method. In addition, other developers may think that the value is changed in the method, even though that is not true.

Bad code sample 1 - developer's error - forgot to remove var property of MyList in Method1, after redesigning Method2

procedure Method1(var MyB: boolean)
begin
    Method2(MyB);
end;

procedure Method2(MyC: boolean)
var
    someVar: boolean;
begin
    someVar := MyC;
end;

Good code sample 1

procedure Method1(MyB: boolean)
begin
    Method2(MyB);
end;

procedure Method2(MyC: boolean)
var
    someVar: boolean;
begin
    someVar := MyC;
end;

Bad code sample 2 - developer's error

procedure Method1(var InvtValue: array[5] of Integer)
var                    
    MyInt: Integer;
begin
    MyInt := InvtValue[0];
end;

Good code sample 2

procedure Method1(InvtValue: array[5] of Integer)
var                    
    MyInt: Integer;
begin
    MyInt := InvtValue[0];
end;

Good and bad practices for fixing the rule Remove not needed var property from the parameter declaration.

Remarks Variables of the following types are analyzed: Integer, BigInteger, Boolean, Byte, Char, Code, Date, DateTime, Decimal, Time, and Text. Also lists and arrays of above types are analyzed - these types should be changed with appropriate methods if they are passed by reference. Event subscribers, Integration Events and ConfirmHander are not analyzed also.

MarcHansenMicrosoft avatar Jan 28 '20 13:01 MarcHansenMicrosoft

Isn't this already released? ...albeit with subsequent bugs opened about false positives, e.g.

  • https://github.com/microsoft/AL/issues/6184
  • https://github.com/microsoft/AL/issues/6179
  • https://github.com/microsoft/AL/issues/6143
  • https://github.com/microsoft/AL/issues/6211

dzzzb avatar Jan 06 '21 16:01 dzzzb

@PaulIvan could you verify?

atoader avatar Jan 20 '21 19:01 atoader

Thank you for reporting this issue. Unfortunately, we do not find it important enough to address at this point.

mazhelez avatar Jun 21 '22 13:06 mazhelez