+ Reply to Thread
Results 1 to 25 of 25

Coding Convention Questions #2 (To clean up or not to clean up)

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Question Coding Convention Questions #2 (To clean up or not to clean up)

    Here's the 2nd question. I have come across conflicting ideas on cleaning up variables before exiting a procedure.

    Suppose we have a sub like so:

    Please Login or Register  to view this content.
    Now the 3 schools of thought I am comparing:
    1. Forget about cleaning up. VBA automatically scraps all procedure level variables when the sub ends, you don't need to waste effort and code bloat your procedure by carefully setting all procedure variables to vbNull/vbNullString/Nothing
    2. You MUST clean up all procedure variables before ending the sub (Then hint darkly at possibly dangerous vague consequences apparently caused by VBA not always cleaning up after itself?)
    3. If you have objects in your sub then you really should erase the objects. All other procedure level variables can be ignored (they don't need to be erased/reset).

    So. I'm confused. Can anyone throw some light on this?
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    I know I don't always clean everything, but I defintely close anything I open....especially database connections....
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    I don't clean up anything unless I write code that requires explicit tear down, it's superfluous and offers no benefit. This means that when I actually come across code that I have manually released memory for, I know it's there for a reason.

  4. #4
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    276

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by Kyle123 View Post
    This means that when I actually come across code that I have manually released memory for, I know it's there for a reason.
    There's no thumbs up smiley...

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    I will agree with onedaywhen here: http://stackoverflow.com/questions/1...-vba-pros-cons
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Thank you to those who have responded.

    Is there anyone prepared to put their hand up for 2? If so, I really would be interested in hearing from you as to the whys and wherefores.

    Because I have these conflicting doubts as to whether 2 is the only real choice or whether 2 is merely a tradition that has been passed on from other languages (where it is necessary).

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    #2 was required with DAO, as stated in earlier link. This created theory that you must always do this.

    Edit: I misread - that is #3. #2 is nonsense.
    Last edited by Izandol; 02-05-2014 at 06:54 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    I am adopting method 3 - I will close all database connections & set all objects to nothing (and not worry about erasing arrays & setting variables to vbnull/vbnullstring)

    I am marking this thread as solved. If anyone can think of any situation where you would need to do more than this - please post to this thread. I'd like to hear more.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Coding Convention Questions #2 (To clean up or not to clean up)


    Hi,

    have you read the VBA help of End statement ?

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by Marc L View Post

    Hi,

    have you read the VBA help of End statement ?
    I haven't read the help but I have an idea what End does.
    (Reads help)
    Not intending to be rude - what point are you making?

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Coding Convention Questions #2 (To clean up or not to clean up)


    Read help and I guess you'll understand ! (for a main procedure …)

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by Marc L View Post

    Read help and I guess you'll understand ! (for a main procedure …)
    But I just did.

    If your point is that the End Function or End Sub resets all local & module level variables - I already knew that it is meant to do that.

    I can't be sure that VBA resets 100% of variables 100% of the time. VBA (and Excel for that matter) have their share of bugs.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    End Function or End Sub resets all local & module level variables
    Local variables, not module-level variables.
    Entia non sunt multiplicanda sine necessitate

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by mc84excel View Post
    If your point is that the End Function or End Sub resets all local & module level variables
    Neither End Function nor End Sub but the End statement alone in a main procedure !
    Like the last code line in this post

  15. #15
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by shg View Post
    Local variables, not module-level variables.
    You are right. If the procedure in question is called from another procedure then reaching End Function or End Sub in the called procedure does not cause module level variables to get reset (unless of course the caller sub is in another module).

  16. #16
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by Marc L View Post
    Neither End Function nor End Sub but the End statement alone in a main procedure !
    Like the last code line in this post
    I didn't know which End statement you were referring to earlier (the Help covers all the End statements). My fault, sorry.

    Re: stand-alone version of End - how is this an argument for using method #2?

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by mc84excel View Post
    'do we clean? if so, what?
    End statement cleans all and even closes opened files, what else ?‼

    Regards !

  18. #18
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Coding Convention Questions #2 (To clean up or not to clean up)


  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Coding Convention Questions #2 (To clean up or not to clean up)


    I agree too for a bad code conception !

    I use End - I don't count anymore with 2 digits ! - for years and I never had a problem !
    Am I very lucky ? I don't think so !

    For some code using much ressources, it just avoid me to warn users sometimes
    they could have to close Excel between operations or even reboot computer …

    But for those who are not very clear with their code
    (as often they just do a Copy / Paste instead of using brain !), yes, avoid it !

  20. #20
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by Marc L View Post
    yes, avoid it !
    With this also I agree.

    I do not see any necessity to use End - there are better ways to clean up resources. Especially if you will use Ribbon code.

  21. #21
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Coding Convention Questions #2 (To clean up or not to clean up)


    In some case I had to …

    But 'cause sometimes I'm lazy, I prefere to just enter three letters instead of
    affect nothing to each object variable used, erase some module variables
    and big array variables and static ones, and close all memory opened text files …

  22. #22
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    You don't need to clear anything unless there's a bug in the library you're using (which is pretty unlikely now) or you've created circular references. There isn't really a best practice catch all, it's more important to understand what your code is doing and treat it apropriately

  23. #23
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by Marc L View Post
    But 'cause sometimes I'm lazy,
    This I understand! But question refers to good coding convention, I think.

  24. #24
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Coding Convention Questions #2 (To clean up or not to clean up)


    OK, so I very get lucky from years …

    Even after using it some app didn't crash anymore
    (but in this times, computers had less memory ressources) …

  25. #25
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Coding Convention Questions #2 (To clean up or not to clean up)

    Quote Originally Posted by Marc L View Post
    End statement cleans all and even closes opened files, what else ?‼

    Regards !
    Thanks Marc but that is what End does. My question was more 'Why would you need to?'

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Coding Convention Questions #1 (On Error Goto 0 before End Sub)
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-05-2014, 01:37 AM
  2. [SOLVED] VBA Code to optimize and clean data- clean out numerical/ or symbol
    By tracylsr in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-05-2012, 04:21 PM
  3. Clean Function to Clean Entire Sheet
    By gema in forum Excel General
    Replies: 7
    Last Post: 11-05-2009, 10:07 AM
  4. Clean Up My Coding
    By Jedody in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-04-2007, 03:12 PM
  5. 'Filter/Clean' Cells; 2 questions
    By EHS in forum Excel General
    Replies: 12
    Last Post: 11-15-2006, 07:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1