473,800 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The real issue with accessors and mutators

The reason Stroustrup warns against using set and get functions is that an
object of class type should be designed in such a way as to maintain some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be state
as: a std::vector<T> hold a number of elements given by the return value of
the member function std::vector<T>: :size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.

Hence, the value that should not be available for direct manipulation by a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location defined
as a member field of the vector<> under discussion.

I find the general warning against using get and set member functions overly
vague, and potentially harmful. There are many, many, instances where
using such functions is perfectly legitimate, and can be done in such a way
as to preserve a meaningful invariant. Too often such general injunctions
become widely accepted in the same way as the misguidance rendered by the
injunction that a preposition is some thing we should not end a sentence
with has.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #1
11 1544
Steven T. Hatton wrote:
The reason Stroustrup warns against using set and get functions is that an
Can you share at least the chapter number where such warning is?
object of class type should be designed in such a way as to maintain some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be state
as: a std::vector<T> hold a number of elements given by the return value of
the member function std::vector<T>: :size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.

Hence, the value that should not be available for direct manipulation by a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location defined
as a member field of the vector<> under discussion.

I find the general warning against using get and set member functions overly
vague, and potentially harmful.
Potentially? Like scissors? They are potentially harmful.
There are many, many, instances where
using such functions is perfectly legitimate, and can be done in such a way
as to preserve a meaningful invariant. Too often such general injunctions
become widely accepted in the same way as the misguidance rendered by the
injunction that a preposition is some thing we should not end a sentence
with has.


I think you have too much time on your hands.

A warning is a warning. You cannot argue against it because there are
cases where such warning doesn't hold. You cannot argue that warning
against driving too fast is "overly vague and potentially harmful" because
there can be instances when driving fast is essential to survival. If the
author of a book has to make excuses and provide explanations for every
passage they put in a book, there will be no trees left on this planet.

V
Jul 22 '05 #2
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:ce******** ************@sp eakeasy.net...
The reason Stroustrup warns against using set and get functions is that an
object of class type should be designed in such a way as to maintain some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be state
as: a std::vector<T> hold a number of elements given by the return value
of
the member function std::vector<T>: :size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.
This conclusion does not follow. A more accurate conclusion would be that
every operation that changes the value of size() must change the number of
elements correspondingly--in other words, there must not be an operation
that changes the value of size() and does nothing else.
Hence, the value that should not be available for direct manipulation by a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location
defined
as a member field of the vector<> under discussion.


But in fact, there *is* an operation that changes the value of size() --
it's named resize. If is a vector and we call v.resize(n), v.size() will
subsequently be equal to n. Of course, a side effect of that call is to
cause the number of elements in v to *be* n, either by deleting elements or
fabricating new ones.
Jul 22 '05 #3
Victor Bazarov wrote:
Steven T. Hatton wrote:
The reason Stroustrup warns against using set and get functions is that
an
Can you share at least the chapter number where such warning is?


The one place I can find mention of set and get functions right now is
§24.4.2. I don't believe that is the only place where he mentions the
issue. If time permits, I'll try to find the other comment.
I think you have too much time on your hands.

A warning is a warning. You cannot argue against it because there are
cases where such warning doesn't hold. You cannot argue that warning
against driving too fast is "overly vague and potentially harmful" because
there can be instances when driving fast is essential to survival. If the
author of a book has to make excuses and provide explanations for every
passage they put in a book, there will be no trees left on this planet.

V


I think this kind of thing can be important. I'm grappling with a tricky
situation along these lines in code I'm currently working on. I have a set
of basis vectors which are members of one class, and which I want to modify
as a result of calculations in another class. The solution of returning a
mutable reference to the member bases makes the modification code much
cleaner, but compromises my ability to guarantee linear independence as a
class invariant.

The reason I believe such warnings can be potentially harmfull is because
people can and often do accept them uncritically and judge another person's
work accordingly.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #4
Andrew Koenig wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:ce******** ************@sp eakeasy.net...
The reason Stroustrup warns against using set and get functions is that
an object of class type should be designed in such a way as to maintain
some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be
state as: a std::vector<T> hold a number of elements given by the return
value of
the member function std::vector<T>: :size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.


This conclusion does not follow. A more accurate conclusion would be that
every operation that changes the value of size() must change the number of
elements correspondingly--in other words, there must not be an operation
that changes the value of size() and does nothing else.


I believe that is a superset of what I was describing.
Hence, the value that should not be available for direct manipulation by
a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location
defined
as a member field of the vector<> under discussion.


But in fact, there *is* an operation that changes the value of size() --
it's named resize. If is a vector and we call v.resize(n), v.size() will
subsequently be equal to n. Of course, a side effect of that call is to
cause the number of elements in v to *be* n, either by deleting elements
or fabricating new ones.


What I meant by direct manipulation was the ability to write directly to the
memory location where size is stored. By calling resize(n), the user
specifies the desired value of size, but leaves it to the details of the
function to affect the actual change.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #5
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:r4******** ************@sp eakeasy.net...

The reason I believe such warnings can be potentially harmfull is because
people can and often do accept them uncritically and judge another person's work accordingly.


The 'potential harm' of which you speak does not come from
e.g. warnings by experts, but by those who read but don't think.

-Mike
Jul 22 '05 #6
>The reason Stroustrup warns against using set and get functions is that an
object of class type should be designed in such a way as to maintain some
invariant.
Strange. I would think get and set functions were ways to help maintain the
invariant. Making the corresponding data members public certainly makes
maintaining the invariant less likely. This of course assumes that the get and
set functions read and write data members that are part of the visible state of
the object and are implemented such that implementation dependencies are not
created and invariants cannot be violated.
I find the general warning against using get and set member functions overly
vague, and potentially harmful. There are many, many, instances where
using such functions is perfectly legitimate, and can be done in such a way
as to preserve a meaningful invariant. Too often such general injunctions
become widely accepted in the same way as the misguidance rendered by the
injunction that a preposition is some thing we should not end a sentence
with has.


I agree with you. Too many times I have heard or seen statements such as...

"Getters and setters are evil"

....or other blanket statements regarding them.

I rarely find statements involving the words "evil" or "always" or "never" to
be of value, except of course where they describe constructs that always lead
to undefined or unspecified behavior.
Jul 22 '05 #7
Steven T. Hatton wrote:
[...]
The reason I believe such warnings can be potentially harmfull is because
people can and often do accept them uncritically and judge another person's
work accordingly.


This is sheer nonsense. Anything can be potentially harmful. People
do strange things. If you're (or somebody else is) unable to apply
critical thinking to something you (they) read or hear why is somebody
else (especially one who gives the warning) to blame?

What would you prefer, not to hear a warning at all, or have more
information available to you to research the roots of the warning? If
the former, you're not reading the right books. Switch to Schildt.

And just listen to yourself... "Potentiall y harmful warning". What
a bunch of...

V
Jul 22 '05 #8
DaKoadMunky wrote:
[..]
I rarely find statements involving the words "evil" or "always" or "never" to
be of value, [..]


Would you say that you [almost] *never* find them helpful, or,
rather, that they are [almost] *always* *evil*?
Jul 22 '05 #9
>>Would you say that you [almost] *never* find them helpful, or,
rather, that they are [almost] *always* *evil*?

In order for me to answer this question I will need to consult with my
attorney.
Switch to Schildt.


The cheap American beer?
Jul 22 '05 #10

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

Similar topics

12
2018
by: Christopher J. Bottaro | last post by:
If I have the following class: class MyClass: def __init__(self): m_dict = {} m_dict = 1 m_dict = 2 m_dict = 3 Is there anyway to generate automatic accessors to the elements of the dict?
22
12041
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is almost never an excuse for accessor methods. Personally, I do not go that far. I do feel that they serve a useful purpose (albeit in a limited manner). Personally I prefer dropping the "set" and "get" prefixes from the method names altogether. ...
2
1738
by: cody | last post by:
Does the clr allow more than one set and one get method for a property? Is it possible to use overloading for example set_Color(int c), set_Color(Color c)? from msdn: PropertyInfo.GetAccessors Method () Return Value An array of MethodInfo objects that reflect the public get, set, *and other
7
2212
by: | last post by:
Hello, I do not understand why the func getName() does not receive any data (actually, none of the get__receive any data. The class I am using as an almost exact example (the exception being that there are no strings involved only ints) has setFunctions (mutators) activate first, then the getFunctions (inspectors), then the constructor. Any thoughts will be appreciated.
10
5790
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this (also copied below for easier reference): http://groups.google.it/groups?hl=en&lr=&safe=off&selm=6beiuk%24cje%40netlab.cs.rpi.edu&rnum=95 I don't agree anymore.
1
1481
by: Brad Williams | last post by:
When I try to define accessors for this event, the event invocation line stops compiling. What's wrong? public class TestClass { public delegate int MyDelegate(string s); public event MyDelegate myEvent { add { Console.WriteLine("myEvent.Add"); } remove { Console.WriteLine("myEvent.Remove"); }
3
2073
by: Peter Cresswell | last post by:
Hello everyone, I would like to serialize an object to XML. Currently my code will serialize all of the public properties that are value types, but not my public properties that have get accessors. e.g: public class MyObject { public string name; // This is serialized OK
112
13876
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them because they smell "Javaish." But now my code base is expanding and I'm beginning to appreciate the wisdom behind them. I welcome example code and illustrations.
4
1916
by: Travis | last post by:
Is it considered good practice to call a mutator when inside the same class or modify the attribute directly? So if there's a public method SetName() would it be better from say ::Init() to call SetName("none") or just set name="none"?
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10275
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.