Should methods in class use its own properties or private variables 
Author Message
 Should methods in class use its own properties or private variables

I'm fairly new to OO concepts, and I had never really considered this
question until I looked at some code.  Let's say

A class has a private variable int "_PrivateNumber"
It is exposed via a property "Number"

There is a method "Square".  Should the code in Square() be
_PrivateNumber * _PrivateNumber
or
Number * Number

I thought it should use the private variable and avoid the "overhead" of
going through the property (though probably insignificant), but the book I'm
reading consistly uses the property in cases like this.

And thinking about why I come up with perhaps this:  The point of properties
is to protect the private variable through an interface which could include
code and verification, so using the property in other methods ensures the
same kind of "protection" within the class itself.

Which is the normal practice, and do I have the reason?

--
Daniel Billingsley, MCP
Programmer/Analyst/System Administrator
The Durcon Company
======================================



Mon, 13 Dec 2004 23:29:40 GMT  
 Should methods in class use its own properties or private variables
You've got it.


Quote:
> I'm fairly new to OO concepts, and I had never really considered this
> question until I looked at some code.  Let's say

> A class has a private variable int "_PrivateNumber"
> It is exposed via a property "Number"

> There is a method "Square".  Should the code in Square() be
> _PrivateNumber * _PrivateNumber
> or
> Number * Number

> I thought it should use the private variable and avoid the "overhead" of
> going through the property (though probably insignificant), but the book
I'm
> reading consistly uses the property in cases like this.

> And thinking about why I come up with perhaps this:  The point of
properties
> is to protect the private variable through an interface which could
include
> code and verification, so using the property in other methods ensures the
> same kind of "protection" within the class itself.

> Which is the normal practice, and do I have the reason?

> --
> Daniel Billingsley, MCP
> Programmer/Analyst/System Administrator
> The Durcon Company
> ======================================



Mon, 13 Dec 2004 23:39:45 GMT  
 Should methods in class use its own properties or private variables


Quote:
> I'm fairly new to OO concepts, and I had never really considered this
> question until I looked at some code.  Let's say

> A class has a private variable int "_PrivateNumber"
> It is exposed via a property "Number"

> There is a method "Square".  Should the code in Square() be
> _PrivateNumber * _PrivateNumber
> or
> Number * Number

> I thought it should use the private variable and avoid the "overhead" of
> going through the property (though probably insignificant), but the book
I'm
> reading consistly uses the property in cases like this.

> And thinking about why I come up with perhaps this:  The point of
properties
> is to protect the private variable through an interface which could
include
> code and verification, so using the property in other methods ensures the
> same kind of "protection" within the class itself.

> Which is the normal practice, and do I have the reason?

Well, it would be smartest in the long run to use the Properties.
While it may seem unnecessary now, in the future, it's possible that
you may add validation logic or special cases to the get() or set()
of the property, in which case you'd either have to go through your
class and replace all calls to the private member to the property
or add that validation logic to all the calls to the private member.
Either case is not favorable as going through and ripping/replacing
large sections of code usually adds instability.

-c



Mon, 13 Dec 2004 23:43:52 GMT  
 Should methods in class use its own properties or private variables
Daniel,

    Basically, using the property is better in my opinion because sometimes
we need to protect our code from ourselves =)

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> I'm fairly new to OO concepts, and I had never really considered this
> question until I looked at some code.  Let's say

> A class has a private variable int "_PrivateNumber"
> It is exposed via a property "Number"

> There is a method "Square".  Should the code in Square() be
> _PrivateNumber * _PrivateNumber
> or
> Number * Number

> I thought it should use the private variable and avoid the "overhead" of
> going through the property (though probably insignificant), but the book
I'm
> reading consistly uses the property in cases like this.

> And thinking about why I come up with perhaps this:  The point of
properties
> is to protect the private variable through an interface which could
include
> code and verification, so using the property in other methods ensures the
> same kind of "protection" within the class itself.

> Which is the normal practice, and do I have the reason?

> --
> Daniel Billingsley, MCP
> Programmer/Analyst/System Administrator
> The Durcon Company
> ======================================



Mon, 13 Dec 2004 23:55:24 GMT  
 Should methods in class use its own properties or private variables
I agree with the above. The performance trade-off is likely to be neglible.


Quote:
> I'm fairly new to OO concepts, and I had never really considered this
> question until I looked at some code.  Let's say

> A class has a private variable int "_PrivateNumber"
> It is exposed via a property "Number"

> There is a method "Square".  Should the code in Square() be
> _PrivateNumber * _PrivateNumber
> or
> Number * Number

> I thought it should use the private variable and avoid the "overhead" of
> going through the property (though probably insignificant), but the book
I'm
> reading consistly uses the property in cases like this.

> And thinking about why I come up with perhaps this:  The point of
properties
> is to protect the private variable through an interface which could
include
> code and verification, so using the property in other methods ensures the
> same kind of "protection" within the class itself.

> Which is the normal practice, and do I have the reason?

> --
> Daniel Billingsley, MCP
> Programmer/Analyst/System Administrator
> The Durcon Company
> ======================================



Tue, 14 Dec 2004 03:40:33 GMT  
 Should methods in class use its own properties or private variables
Yes. And in a release build, the use of the properties should be inlined
anyhow so there would be no overhead using the property as opposed to
accessing the private field directly.

-glenn-


Quote:
> I agree with the above. The performance trade-off is likely to be
neglible.



> > I'm fairly new to OO concepts, and I had never really considered this
> > question until I looked at some code.  Let's say

> > A class has a private variable int "_PrivateNumber"
> > It is exposed via a property "Number"

> > There is a method "Square".  Should the code in Square() be
> > _PrivateNumber * _PrivateNumber
> > or
> > Number * Number

> > I thought it should use the private variable and avoid the "overhead" of
> > going through the property (though probably insignificant), but the book
> I'm
> > reading consistly uses the property in cases like this.

> > And thinking about why I come up with perhaps this:  The point of
> properties
> > is to protect the private variable through an interface which could
> include
> > code and verification, so using the property in other methods ensures
the
> > same kind of "protection" within the class itself.

> > Which is the normal practice, and do I have the reason?

> > --
> > Daniel Billingsley, MCP
> > Programmer/Analyst/System Administrator
> > The Durcon Company
> > ======================================



Tue, 14 Dec 2004 07:42:36 GMT  
 Should methods in class use its own properties or private variables
Hi Daniel.

I have given this a long thought. All the reasons from previous post are
valid. However, you will likely to have private data inside your class for
internal use only and don't have property for them. In that case, there
might be some inconsistence where you will access/modify directly on some
private data and other through the use of property. You can definitely
create private property but this adds to more typing which might make the
code look messy.

I definitely agree that property add more flexibility and safeguard your
private data. So the extra coding might be worthy. Just my 2 cents.

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net?  http://www.gotdotnet.com



Wed, 15 Dec 2004 04:32:22 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Advice on using a class method to set its own property

2. Assigning initial values to private variables in a class

3. Vector private memeber variable in ATL dialog class

4. Throwing an exception from method own own thread

5. Trying to use a member variable of my own class

6. Using a class as a member variable in the document class

7. Hidden properties and methods in inherited classes over XML Web services

8. Static VS Local Class Level variables and Methods

9. VC++ Class properties/method access in VB program?

10. vc6: View method to refer to App class member variable

11. Web Services: Proxy class make own class definition for user class

12. Using classes for own DLL

 

 
Powered by phpBB® Forum Software