Variable Scope - Best Practice

doddwell
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
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
-
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.0 -
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.0 -
Mark Brummel wrote: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* Daniele Rebussi * | * Rebu NAV Diary *0 -
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.0 -
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.0 -
Thanks for your responses. How do you handle Text Constants - do you tend to declare these as Globals? Thanks0
-
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.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions