Hello
I have reviewed the code in our NAV install and I'm struggling to understand when it is best to use Local or Global variables.
I understand Scope - so I know that if you define the variable as a Local one, it is only available to that Function or Trigger. A Global variable is available to the whole object.
The code I am reviewing has Global variables that are only used in one Function.
My instinct tells me that if a variable is only required by one Function or Trigger, I should declare it as Local.
Is there a rule of thumb I can adopt?
Many Thanks, Simon
0
Comments
I guess the developer thought 1 function in 1 codeunit, could be global or local. In 2013 it's all converted to C# and then local memory is reused earlier. But that's a another topic and it all depends what is in the vars.
Never use locals in field validation triggers, always create a function that has the variables.
Is there any specific reason in order to do that (except chance of calling the same function from different points, indeed)?
Thanks
If the naming of the function is self explaining you will also decouple functional and technical coding allowing a consultant with the debugger to understand the business reason of the change.
Given that globals are not real globals, just "protecteds" (i.e. global for one object only) IMHO it is OK to use them typically as record variables, for example you have a report with functions like WriteThisKindOfLine, WriteThatKindOfLine to Excel Buffer. In all kinds you have variables like Item, Customer, to write out some master data like names or descriptions. Why bother passing them around as parameters? It's easier to keep them global.
I consider it a good process to investigate every possible error at the beginning of a data processing routine, give error messages, then basically be silent until the end (except progress bar etc.) so in a large codeunit really only the first and last function would need text constants. In this case local.