473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

GETs and SETs

Dom
I understand that best practice is to hide fields behind methods, but
for the hundredth time, I've written the following:

get { return x; }
set { x = value; }

If that's all that get and set does, isn't it better to just make "x"
a public member?

May 7 '07 #1
13 1832
On May 7, 11:58 pm, Dom <dolivas...@gmail.comwrote:
I understand that best practice is to hide fields behind methods, but
for the hundredth time, I've written the following:

get { return x; }
set { x = value; }

If that's all that get and set does, isn't it better to just make "x"
a public member?

It all depends on who the code is written for. If its your own
personal thing and making a variable public is easier then go for it.
If your designing a component thats to be used by the wider community
then use properties and methods.

You wouldent want someone setting a variable that you've made public
now would you? Its the perfect control mechanism.

Vince

May 7 '07 #2
Dom
You wouldent want someone setting a variable that you've made public
now would you? Its the perfect control mechanism.

Vince
That's what I don't understand. The get and set are both public
anyway, so you are allowing someone to set a variable. Whether you
are using a set, or a public variable, you still end up writing this.

MyObject.X = 10

May 7 '07 #3
On May 8, 12:24 am, Dom <dolivas...@gmail.comwrote:
You wouldent want someone setting a variable that you've made public
now would you? Its the perfect control mechanism.
Vince

That's what I don't understand. The get and set are both public
anyway, so you are allowing someone to set a variable. Whether you
are using a set, or a public variable, you still end up writing this.

MyObject.X = 10

Not so, if you exclude the SET part then it becomes a readonly
property. A public variable is open to manipulation.

Don't forget you can also drop a whole bunch of code in your get and
set blocks which allow you to maybe set an internal variable before
returning the value also.

Vince

May 7 '07 #4

"Dom" <do********@gmail.comwrote in message
news:11**********************@e51g2000hsg.googlegr oups.com...
>I understand that best practice is to hide fields behind methods, but
for the hundredth time, I've written the following:

get { return x; }
set { x = value; }

If that's all that get and set does, isn't it better to just make "x"
a public member?
Oh, you can do more with a public method Get and Set than just get and set,
like make decisions on setting object state, return formatted data, set data
in a formatted manner, call private methods with in an object to validate
data, set or get objects in a collection, etc, etc.

But more importantly, one doesn't want to address a variable in an object
directly with a public setting with anyone being able to instantiate the
object and address a variable, directly.

It must be done through a public method to address the variable.

It's a best OOP's programming practice, that shouldn't be ignored.
May 7 '07 #5

There is a data binding issue also.

I didn't bookmark the article I read, but it made reference that binding to
a property was "correct", but a public variable didn't work the same way.
(Going from memory, but I know I read something because I remember saying
"ahhh, that's a reason beyond just good programming practices")

"Dom" <do********@gmail.comwrote in message
news:11**********************@e51g2000hsg.googlegr oups.com...
I understand that best practice is to hide fields behind methods, but
for the hundredth time, I've written the following:

get { return x; }
set { x = value; }

If that's all that get and set does, isn't it better to just make "x"
a public member?

May 7 '07 #6
On May 7, 10:24 am, Dom <dolivas...@gmail.comwrote:
You wouldent want someone setting a variable that you've made public
now would you? Its the perfect control mechanism.
Vince

That's what I don't understand. The get and set are both public
anyway, so you are allowing someone to set a variable. Whether you
are using a set, or a public variable, you still end up writing this.

MyObject.X = 10
A field is implementation, and by exposing it directly you're breaking
encapsulation. The object now has no way to know its state has
changed, and that's a bad thing. Most of my property setters and
getters have more than one line of code.

I would stick with properties, just so you're in the habit of doing
so. If you find you're writing alot of similar code, look at code
gen, or in VS snippets and / or refactoring (Encapsulate Field).

May 7 '07 #7
On May 7, 10:43 am, "sloan" <s...@ipass.netwrote:
There is a data binding issue also.

I didn't bookmark the article I read, but it made reference that binding to
a property was "correct", but a public variable didn't work the same way.
(Going from memory, but I know I read something because I remember saying
"ahhh, that's a reason beyond just good programming practices")
That's a good point as well; IIRC, databinding doesn't acknowledge
fields at all, it will ONLY work with properties.

May 7 '07 #8
>You wouldent want someone setting a variable that you've made public
>now would you? Its the perfect control mechanism.

Vince

That's what I don't understand. The get and set are both public
anyway, so you are allowing someone to set a variable. Whether you
are using a set, or a public variable, you still end up writing this.

MyObject.X = 10
It's called "encapsulation". You don't expose member variables to the
outside world. This is a widely documented concept I need not elaborate on
here. Moreover, because properties are a specific language construct unlike
normal accessor functions (dedicated to this purpose), they can be
recognized as such by various tools. For instance, you can create a
"PropertyGrid" control which will automatically read and display all
properties on an arbitrary object (what you see in the forms designer
properties window for instance). An accessor function (or direct exposure to
the member itself) can't be used here because it's just an ordinary function
so there's no way to distinguish between it and an actual property.
May 7 '07 #9

Yep, that was it.

We need a url reference or something.

Geeze, I wish I had kept it.

"Andy" <an***@med-associates.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
On May 7, 10:43 am, "sloan" <s...@ipass.netwrote:
There is a data binding issue also.

I didn't bookmark the article I read, but it made reference that binding
to
a property was "correct", but a public variable didn't work the same
way.
(Going from memory, but I know I read something because I remember
saying
"ahhh, that's a reason beyond just good programming practices")

That's a good point as well; IIRC, databinding doesn't acknowledge
fields at all, it will ONLY work with properties.

May 7 '07 #10

Ding Ding Ding:

http://www.codinghorror.com/blog/archives/000654.html

Reflection works differently on variables vs. properties, so if you rely on
reflection, it's easier to use all properties.
You can't databind against a variable.
Changing a variable to a property is a breaking change.

There's lot more stuff/debate at the blog.


"sloan" <sl***@ipass.netwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
>
Yep, that was it.

We need a url reference or something.

Geeze, I wish I had kept it.

"Andy" <an***@med-associates.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
On May 7, 10:43 am, "sloan" <s...@ipass.netwrote:
There is a data binding issue also.
>
I didn't bookmark the article I read, but it made reference that
binding
to
a property was "correct", but a public variable didn't work the same
way.
(Going from memory, but I know I read something because I remember
saying
"ahhh, that's a reason beyond just good programming practices")
That's a good point as well; IIRC, databinding doesn't acknowledge
fields at all, it will ONLY work with properties.


May 7 '07 #11
"Dom" <do********@gmail.comschrieb im Newsbeitrag
news:11**********************@e51g2000hsg.googlegr oups.com...
>I understand that best practice is to hide fields behind methods, but
for the hundredth time, I've written the following:

get { return x; }
set { x = value; }

If that's all that get and set does, isn't it better to just make "x"
a public member?
Hi Dom,

one reason, for doeing so is, that in OOP one class should be unaware of the
implementation of other classes, for all, if they are from another component
(= assembly in .NET). Fields are implementation, while properties aren't.
(Only the code inside of the set and get is.)

second: For many of your public fields there will come a time, when you want
to change them into properties, because you have to do some validation in
the setter or you want to change the implementation of the storage or ...
This may come sooner as you think, and if it comes later, this even can be
worse. Then, if you simply change the code of your class, all the compiled
client code wouldn't be compatible anymore. Atleast that clients would have
to be recompiled. And for some cases, even that wouldn't be sufficent.
Now, this actually is some how the practicle side of the first reason.

Christof
May 7 '07 #12
Dom wrote:
I understand that best practice is to hide fields behind methods, but
for the hundredth time, I've written the following:

get { return x; }
set { x = value; }
You shouldn't have to write the above for the hundred and first time:
Don't write getters and setters, generate them instead. I expect your
IDE allows you to generate getters and setters automatically. I'm used
to Eclipse (for Java) which does this for any fields defined in a class.
The SharpDevelop IDE for C# seems to have this feature. I've never used
Visual Studio but I'd be surprised if it didn't have this feature too.
>
If that's all that get and set does, isn't it better to just make "x"
a public member?
Other people have covered encapsulation.
http://en.wikipedia.org/wiki/Information_hiding#Uses
May 9 '07 #13
On May 9, 1:22 pm, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
wrote:
Dom wrote:
I understand that best practice is to hide fields behind methods, but
for the hundredth time, I've written the following:
get { return x; }
set { x = value; }

You shouldn't have to write the above for the hundred and first time:
Don't write getters and setters, generate them instead. I expect your
IDE allows you to generate getters and setters automatically. I'm used
to Eclipse (for Java) which does this for any fields defined in a class.
The SharpDevelop IDE for C# seems to have this feature. I've never used
Visual Studio but I'd be surprised if it didn't have this feature too.
With a caveat: you should be selective about which properties have
setters.

Really, you should start off assuming that all properties are read-
only unless you have a good reason to make them settable. I find that
most properties in most of my classes are read-only. They are set
either through the constructor or as an effect of some method in the
class. I do have a healthy number of settable properties, but I would
estimate it at less than half.

One of the problems with property generators (and with simply using
fields) is that programmers become lazy. They automatically, without
thinking, just generate setters (or use fields) for everything,
whether they're needed or not.

In the end, I find that it's not that much more work to use
properties, even if they're just fronting a field.

Nobody has brought up the subject of classes that are just holding
buckets for data, but even these cases I tend to use properties,
simply because such classes are so rare that it isn't worth breaking
the pattern just for a handful of exceptional cases.

May 10 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Raymond Hettinger | last post by:
I've gotten lots of feedback on the itertools module but have not heard a peep about the new sets module. * Are you overjoyed/outraged by the choice of | and & as set operators (instead of + and...
5
by: Raymond Hettinger | last post by:
For Py2.4, I'm working on a C implementation of Sets.py with the possibility of having a set() type as a builtin. There is a question as whether the current design for set of sets has been useful....
7
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
0
by: Dan | last post by:
hi, i have a page with two usercontrols: 1. a static menu control. 2. a dynamically loaded control as user interface the main page receives events from the menu and sets the apropriate ui...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
12
by: Jack | last post by:
Since, I have not got some desired advise, I am reposting this for some asnwer/valuable suggestion. Thanks. THE FOLLOWING IS A PART OF CODE FROM A ASP PAGE <% sql01 = "SELECT COUNT(*) AS...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
1
by: JosAH | last post by:
Greetings, Introduction This week I'll write a bit about generics (those funny angular brackets). I need an example and decided to use sets and some of their operations. This weeks' article...
15
by: raashid bhatt | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> void func(char *p) { char i; strcpy(i, p); }
18
by: David Moss | last post by:
Hi, I want to manage and control access to several important attributes in a class and override the behaviour of some of them in various subclasses. Below is a stripped version of how I've...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.