+ Reply to Thread
Results 1 to 2 of 2

Public vs Private?

  1. #1
    Registered User
    Join Date
    08-15-2012
    Location
    Ontario
    MS-Off Ver
    Excel 2021
    Posts
    35

    Public vs Private?

    Why is it important to make the distinction between Public and Private subroutines and variables? I know it's "good form" - but why is that exactly? If there is no overlap between variable or subroutine names, what is the problem with making all of them public? For what it's worth, I am careful about making the distinction but other people I know aren't as discriminating and I'm not sure how to argue what the importance of this is.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Public vs Private?

    This is a matter of good software design. When you make a variable Public, any other module can access it freely. This is called "common data coupling" as first described by Ed Yourdon in 1979. If you are allowing multiple modules to communicate by sharing data, it complicates the interfaces. If that variable gets an unexpected value due to a bug, it is very difficult to trace where that assignment occurred. You have to search for every place in your entire project where that variable is being used to see where it changed. Then you have to analyze what the execution path could be that led it to where the change occurred. Therefore use of Public variables should be limited to those variables where there is no other practical way to communicate the information, such as a UserForm making the result of a user action available to the module that called Show. Even then, you have to have controls in place so that the form is immune to outside code changing that result value. It is better to communicate data by Sub parameters rather than using Public variables.

    Similarly Subs should be Private unless they are needed to provide a service outside the module.

    These concepts began in the days of structured design, and became further ensconced with object-oriented design. I personally approach Excel VBA design like object-oriented design. Sometimes you want to encapsulate a piece of data inside a module and not let any other module manipulate it directly, because you might have certain rules about how it can be changed. You make it Private and then provide Public Subs to change it and enforce the rules.

    From a practical standpoint you can take code that works, make everything Public, and everything will still work (unless you have naming collisions). But that approach is more bug-prone and much more difficult to diagnose.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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] Private Sub Workbook_Open() Vs Public Sub Auto_Open()
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-12-2014, 07:10 PM
  2. Public variable in private routine across worksheets
    By LeanAccountant in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-05-2014, 07:02 AM
  3. Replies: 6
    Last Post: 01-03-2014, 09:55 AM
  4. Convert this private sub to public sub to work
    By tantcu in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-28-2013, 02:59 PM
  5. Public or Private Variable
    By jo15765 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 01-12-2012, 02:46 PM
  6. Option Explicit function and private/public/dim
    By larryg003 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-09-2010, 03:01 PM
  7. Public, Private, Event modules, Forms modules,,,
    By Jim May in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-31-2005, 12:05 AM

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