Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions content/docs/analyzers/LinterCop/LC0096.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ The rule suppresses the diagnostic in several cases where the parameter is inten
- **Different module**: when the target method is defined in a different module, the developer cannot refactor the external signature.
- **Built-in methods**: calls to built-in methods (`Clear`, `Page.RunModal`, etc.) are excluded.
- **Different variable or field access**: `Customer.DoSomething(Customer2)` or `Customer.DoSomething(Customer."No.")` are not flagged because the argument is not the same record instance.
- **`xRec` arguments**: `Rec.AssistEdit(xRec)` or `DoSomething(xRec)` passes the previous version of the record, a deliberate pattern whose need the callee decides; only `xRec.DoSomething(xRec)` is flagged.
- **`RecordRef` arguments**: a `RecordRef` is its own buffer, not `Rec`, even after `RecRef.GetTable(Rec)`, so passing it is never redundant.

### Edge case: local method called with multiple record instances

A local procedure that takes a record parameter may be called with several different record instances — some of them `Rec`. The diagnostic still fires on the `Rec` call. The usual fix — invoke the method on the record instead (`Other.DoSomething()`) — does not apply here: it would force the local procedure to become public or internal for no reason other than satisfying the rule.
A local procedure that takes a record parameter may be called with several different record instances — some of them `Rec`. The diagnostic still fires on the `Rec` call, but the other callers still need the parameter.

{{< highlight al "hl_lines=13" >}}
table 50100 MyTable
Expand All @@ -120,15 +122,15 @@ table 50100 MyTable
DoSomething(Rec); // Unnecessary record parameter in method call [LC0096]
end;

local procedure DoSomething(MyTable: Record MyTable)
local procedure DoSomething(var MyTable: Record MyTable)
begin
end;
}
{{< /highlight >}}

Add a parameterless overload that assigns `Rec` to a local variable and delegates to the parameterized procedure. The delegating call passes a local variable, not `Rec`, so the diagnostic no longer fires — and the call site no longer implicitly passes the same record twice. The cost is one extra local variable and a record copy.
Invert the delegation: move the implementation into a parameterless procedure that works on `Rec`, and let the procedure with the parameter call it on that parameter. `MyTable.DoSomething()` passes no argument, so the diagnostic no longer fires, and a `var` parameter keeps referring to the caller's record. A `local` procedure cannot be called on another record instance, so both procedures become `internal`, which still keeps them inside your app.

{{< highlight al "hl_lines=13 16-22" >}}
{{< highlight al "hl_lines=13 16-19 21-24" >}}
table 50100 MyTable
{
fields
Expand All @@ -144,16 +146,42 @@ table 50100 MyTable
DoSomething();
end;

local procedure DoSomething()
var
MyTable: Record MyTable;
internal procedure DoSomething()
begin
MyTable := Rec;
DoSomething(MyTable);
// implementation, using Rec
end;

local procedure DoSomething(MyTable: Record MyTable)
internal procedure DoSomething(var MyTable: Record MyTable)
begin
MyTable.DoSomething();
end;
}
{{< /highlight >}}

Once no caller needs the procedure with the parameter any more, delete it.

If the procedure must stay `local` and takes the record by value, a parameterless overload can instead assign `Rec` to a local variable and pass that (`MyTable := Rec; DoSomething(MyTable);`). Do not use this copy for a `var` parameter: the callee would change the local copy, not `Rec`.

### Public procedures and dependent apps

Removing the record parameter from a public procedure changes its signature, which breaks every app that depends on yours; AppSourceCop reports it as [AS0024](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/analyzers/appsourcecop-as0024). Use the same inverted delegation and mark the procedure with the parameter as obsolete, so dependent apps can move to the new overload over a release cycle before you remove it:

{{< highlight al "hl_lines=8-12" >}}
table 50100 MyTable
{
procedure DoSomething()
begin
// implementation, using Rec
end;

[Obsolete('Replaced by the overload without the record parameter.')]
procedure DoSomething(var MyTable: Record MyTable)
begin
MyTable.DoSomething();
end;
}
{{< /highlight >}}

The tag argument of the attribute is optional. Apps that run AppSourceCop add the version in which the procedure became obsolete, as [AS0073](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/analyzers/appsourcecop-as0073) requires.

For a `local` procedure there are no outside callers: remove the parameter directly, or delete the procedure with the parameter once your own callers no longer need it.