Using CLEAR for .NET Variables

ta5ta5 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

Best Answer

Answers

  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    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-03
  • ta5ta5 Member Posts: 1,164
    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
    Thomas
  • ta5ta5 Member Posts: 1,164
    Hi Slawek
    Thx, fully agree.
    Thomas
Sign In or Register to comment.