Public vs Private variables 
Author Message
 Public vs Private variables

I know it is generally considered better design to declare variables as
Private rather Public and then wrap them in Property procedures.  What I
can't find an answer to is whether or not Public variables consume more
memory than Private ones.  It would seem that Public vars would need to stay
in memory longer (for the life of the application) as opposed to Private
vars which are cleaned out as each procedure/module is unloaded.

Comments?

--
Mitch Abaza



Mon, 11 Nov 2002 03:00:00 GMT  
 Public vs Private variables
On a related matter, for the longest time I've wondered about the efficiency
factor of constantly creating and destroying variables in each
procedure/module as opposed to simply creating them once globally and being
done with it. (I'm aware of the "spaghetti" nature of global variables, but
my question deals with the efficiency question only.)

Rick


Quote:
> I know it is generally considered better design to declare variables as
> Private rather Public and then wrap them in Property procedures.  What I
> can't find an answer to is whether or not Public variables consume more
> memory than Private ones.  It would seem that Public vars would need to
stay
> in memory longer (for the life of the application) as opposed to Private
> vars which are cleaned out as each procedure/module is unloaded.

> Comments?

> --
> Mitch Abaza




Mon, 11 Nov 2002 03:00:00 GMT  
 Public vs Private variables

Quote:

>On a related matter, for the longest time I've wondered about the
efficiency
>factor of constantly creating and destroying variables in each
>procedure/module as opposed to simply creating them once globally and being
>done with it. (I'm aware of the "spaghetti" nature of global variables, but
>my question deals with the efficiency question only.)

>Rick

Why limit the issue to local versus global?

Let's assume that you measure efficiency in a class module and find that
creating a particular member variable is very time consuming compared to
what you do with it.  So create the object once (say, during class
initialization) and reference it throughout the module.  Easily meaured,
easily changed--and outside clients don't know and don't care.

Then the outside client does his own measuring.  He finds that in one case
using your object is faster by creating on demand.  In another case, using
it is faster created once.  He has the same options that you had, except now
you already create your object most efficiently when he asks for it.

Steven



Mon, 11 Nov 2002 03:00:00 GMT  
 Public vs Private variables
Your programming in VB and worrying about the space variables take?  With all
the overhead of VB I don't think it makes a difference.  You are correct in you
assumption about public/private variables.
Quote:

> I know it is generally considered better design to declare variables as
> Private rather Public and then wrap them in Property procedures.  What I
> can't find an answer to is whether or not Public variables consume more
> memory than Private ones.  It would seem that Public vars would need to stay
> in memory longer (for the life of the application) as opposed to Private
> vars which are cleaned out as each procedure/module is unloaded.

> Comments?

> --
> Mitch Abaza




Mon, 11 Nov 2002 03:00:00 GMT  
 Public vs Private variables

Quote:
> Your programming in VB and worrying about the space variables take?  With
all
> the overhead of VB I don't think it makes a difference.  You are correct
in you
> assumption about public/private variables.


> > I know it is generally considered better design to declare variables as
> > Private rather Public and then wrap them in Property procedures.  What I
> > can't find an answer to is whether or not Public variables consume more
> > memory than Private ones.  It would seem that Public vars would need to
stay
> > in memory longer (for the life of the application) as opposed to Private
> > vars which are cleaned out as each procedure/module is unloaded.

> > Comments?

Public vs Private has no effect on the lifetime of the variable.

A Public variable will use a little less resources than a Private with
property Get & Let procedures because the Property Get & Let take some and
the "hidden" Get/Let procedures for the public variable are pretty
streamlined.  You can see that by doing some timings and you'll see that
public variables are faster the property get/let calls.

On the other hand, Private variables give you more control in controlling
who can modify or view them and when since you can code the get/let
procedures to do whatever you want.  Even for variables that you don't need
hat ability for you should consider that (1) you may in the future and (2)
it makes all cross-module access consistent.  There's nothing wrong with
using Public variables and I would not worry about the marginal resource
differences unless you are making thousands of calls to one during some
long-running procedure.



Mon, 11 Nov 2002 03:00:00 GMT  
 Public vs Private variables
On the note of VB and overhead... True, VB is an overloaded and bloated
language, but hey, why not shave off as much of that as possible?? :)

As for Public vs. Private? It all boils down to what can modify what.. a
variable in VB takes it size based on what it is declared as. Public and
Private, as described by Microsoft, are more geared towards permissions than
anything else. Public and Private take the same space in memory.. At least
that is what I have gotten from Microsoft...


Quote:


> > Your programming in VB and worrying about the space variables take?
With
> all
> > the overhead of VB I don't think it makes a difference.  You are correct
> in you
> > assumption about public/private variables.


> > > I know it is generally considered better design to declare variables
as
> > > Private rather Public and then wrap them in Property procedures.  What
I
> > > can't find an answer to is whether or not Public variables consume
more
> > > memory than Private ones.  It would seem that Public vars would need
to
> stay
> > > in memory longer (for the life of the application) as opposed to
Private
> > > vars which are cleaned out as each procedure/module is unloaded.

> > > Comments?

> Public vs Private has no effect on the lifetime of the variable.

> A Public variable will use a little less resources than a Private with
> property Get & Let procedures because the Property Get & Let take some and
> the "hidden" Get/Let procedures for the public variable are pretty
> streamlined.  You can see that by doing some timings and you'll see that
> public variables are faster the property get/let calls.

> On the other hand, Private variables give you more control in controlling
> who can modify or view them and when since you can code the get/let
> procedures to do whatever you want.  Even for variables that you don't
need
> hat ability for you should consider that (1) you may in the future and (2)
> it makes all cross-module access consistent.  There's nothing wrong with
> using Public variables and I would not worry about the marginal resource
> differences unless you are making thousands of calls to one during some
> long-running procedure.



Fri, 15 Nov 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Declaring variables as Static vs Private/Public -- XML

2. Use a public variable or use Private variable with Get and Set

3. Public Type vs. Public variable() as Double

4. Private Resultset vs. Public Resultset

5. Public vs Private

6. Option Explicit vs. Private/Public - Newbie

7. VB5: Option Private Module vs Public

8. Private vs Public functions & subs

9. Friend vs Private class and public

10. Private Resultset vs. Public Resultset

11. Public vs Private Declairs

12. private vs public API declares

 

 
Powered by phpBB® Forum Software