Replies: 2 comments 1 reply
-
You're absolutely right that loading the codeunit into memory has a cost, where it will always less performance then assigning a variable directly. And if you need this multiple times, for example in a loop, then I would high recommend to store it in a variable instead of calling the codeunit mulitple times. procedure MyProcedure()
var
TypeHelper: Codeunit "Type Helper";
CRLFSeparator: Text[2];
begin
CRLFSeparator := TypeHelper.CRLFSeparator();
foreach Value in EnumerableCollection do begin
end;
end; Your question reminds me on a great question of Klaus Kaan on X: "Its not my code, but it has to have a more readable solution". Starting the question from the DelChr example, there where multiple code improvement suggested to improve the readability of the code, Example DelChrBankAccountNo := DelChr(BankAccountNo, '<=>', DelChr(BankAccountNo, '<=>', '0123456789')); Example Loopprocedure RemoveNonNumeric(InputString: Text): Text
var
I: Integer;
Result: TextBuilder;
begin
for I := 1 to StrLen(InputString) do begin
if InputString[I] in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] then
Result.Append(InputString[I]);
end;
exit(Result.ToText());
end; Example RegExprocedure RemoveNonNumeric(Input: Text) Result: Text
var
Regex: Codeunit Regex;
begin
Result := Regex.Replace(Input, '\D', '');
end; Example Charprocedure RemoveNonNumeric(Input: Text) Result: TextBuilder
var
i: Integer;
begin
for i := 1 to StrLen(Input) do
if Input[i] in [48 .. 57] then
Result.Append(Input[i]);
end; The if performance is important by Arend-Jan Kauffmann on X has the least readability for me personally, but boy, it is FAST comparing with the others 😅 But here the "it depends" part important Arend-Jan Kauffmann on X;
With this knowledge I'm quite confident that we're talking milliseconds here on the difference and it will only be measurable on loops going more the 10000 times. To measure the exact impact of the "Type Helper" codeunit, I need to run this with some examples on the BCPerfTool. I'll see if I can set this up somewhere in the upcoming weeks. You're question is still valid, where I think it would be great to add this to the documentation of the rule, so in specific situations it's better to not follow this rule. |
Beta Was this translation helpful? Give feedback.
-
procedure MyProcedure()
var
TypeHelper: Codeunit "Type Helper";
LineSeparator: Text;
begin
LineSeparator := TypeHelper.LFSeparator();
while MyCondition do
TextBuilder.Append(TypeHelper.ReadAsTextWithSeparator(InStream, LineSeparator));
end; I've added this example to the documentation of LC0085 to show the possibility of storing the result in a local variable to reuse inside a loop. Although the example loads the TypeHelper for the ReadAsTextWithSeparator method on every loop, but it's the idea here that counts? 🫣 |
Beta Was this translation helpful? Give feedback.
-
Hi,
I agree to standardize and factor the code as much as possible, but concerning this rule (LC0085), I have a question:
The Type Helper Codeunit is not single instance and makes several hundred lines. Is it smart to load it in memory to simply use a function which is supposed to return a single byte rather than writing it yourself in a local variable?
Have a nice day :)
Beta Was this translation helpful? Give feedback.
All reactions