Using CLEAR for .NET Variables

ta5
Member Posts: 1,164
Hello
We often see CLEAR commands for .NET Variables at the end of functions, for example in Codeunit 5054 "WordManagement".
The question: Why is it used? As far as I understand, local variables are cleared anyway when function ends and global functions are cleared as soon as the instance of the objects is not used anymore.
Btw: I know I seems to be good practice, but I wonder if it is really needed and what happens if not following this good practice (of course apart from the fact we'll get caught by the boegeyman)
Looking forward to your opinions.
Thanks, Thomas
We often see CLEAR commands for .NET Variables at the end of functions, for example in Codeunit 5054 "WordManagement".
The question: Why is it used? As far as I understand, local variables are cleared anyway when function ends and global functions are cleared as soon as the instance of the objects is not used anymore.
Btw: I know I seems to be good practice, but I wonder if it is really needed and what happens if not following this good practice (of course apart from the fact we'll get caught by the boegeyman)

Looking forward to your opinions.
Thanks, Thomas
0
Best Answer
-
Hi Thomas... and one reason to use CLEAR is always, because Microsoft does it in their own Codeunits.
For localy defined variables CLEAR at the end of the function does not do anyting useful - exceution wise.
It could make the code a bit more easy to understand by stressing that this var is no longer used. It is also 'future proof' in this sense that if anyone wants to refactor the code and make the var global (although the trend should be exactly opposite), this CLEAR(var) may help avoid troubles.
But - looking at C5054 end of ExecuteMerge small things like this:IF NOT ISNULL(WordOLEFormat) THEN CLEAR(WordOLEFormat);
suggests that it was a junior developer who coded this function.
In my personal opinion looking at other's code, Microsoft's inlcuding, an using it as a template, is generally a good thing, as long as there are some thoughts put behind it. Why it has been done like this? How it works? What impact does it have, here and overall? Just "Because Microsoft does it" without asking such questions, without trying to understand the code seems to be a bit mindless Ctrl+C + Ctrl+V excercise.Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035
Answers
-
Hi,
I think it is uses as a part of good practice. For local variables it does not matter.
For global is quite important imho, as you never know when your object cease to exists. If you don't clear it up yourself in your code you are basically counting on other developers that they will clear their vars. Which may or may not happen.
I've seen many, many problems caused by using global vars and not clearing them afterwars. The scenario in most cases was - someone usd global var and at the time everyting was fine. Next time / next change the object with global var was itself declared as global var - also not cleared between call. Results - easy to guess.Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-030 -
Hi Slawek
Thanks for your comment on this topic.
Have a look at Codeunit 5054 "Word Management", Function "Execute Merge". This (local) Function uses CLEAR at the end. Would you basically say the CLEAR functions issued at the end do not matter?
This is also my opinion and also what I've experienced, but on the other hand why is the code there if it is "for nothing"?
Why am I asking this: I have and had discussions with fellow programmers and one reason to use CLEAR is always, because Microsoft does it in their own Codeunits.
Thanks
Thomas0 -
Hi Thomas... and one reason to use CLEAR is always, because Microsoft does it in their own Codeunits.
For localy defined variables CLEAR at the end of the function does not do anyting useful - exceution wise.
It could make the code a bit more easy to understand by stressing that this var is no longer used. It is also 'future proof' in this sense that if anyone wants to refactor the code and make the var global (although the trend should be exactly opposite), this CLEAR(var) may help avoid troubles.
But - looking at C5054 end of ExecuteMerge small things like this:IF NOT ISNULL(WordOLEFormat) THEN CLEAR(WordOLEFormat);
suggests that it was a junior developer who coded this function.
In my personal opinion looking at other's code, Microsoft's inlcuding, an using it as a template, is generally a good thing, as long as there are some thoughts put behind it. Why it has been done like this? How it works? What impact does it have, here and overall? Just "Because Microsoft does it" without asking such questions, without trying to understand the code seems to be a bit mindless Ctrl+C + Ctrl+V excercise.Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035 -
Hi Slawek
Thx, fully agree.
Thomas0
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