Variable Scope - Best Practice

doddwelldoddwell Member Posts: 65
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

Comments

  • mdPartnerNLmdPartnerNL Member Posts: 802
    You are correct.

    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.
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    General rules are always use locals unless you really need it global, which is with nicely structured code almost never.

    Never use locals in field validation triggers, always create a function that has the variables.
  • geordiegeordie Member Posts: 655
    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
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    It is part of the PRS story. It will increase maintainability of the solution and make automatic merging easier.

    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.
  • Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    Mark,

    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.
  • doddwelldoddwell Member Posts: 65
    Thanks for your responses. How do you handle Text Constants - do you tend to declare these as Globals? Thanks
  • Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    Text Constants don't normally get passed around as parameters, so fairly simple: if used once, local, if used multiple times, 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.
Sign In or Register to comment.