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

Home Posts Topics Members FAQ

Naming convention for accessor methods (get/set)

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. For example:

class A
{
public:
...
int xyz() { return _xyz; } // instead of get_xyz()
void xyz(const int val) { _xyz = val; } // instead of get_xyz()
...

protected:
...
int _xyz;
...
};
This naming convention is also consistent with the IDL/C++ language
binding, and I wanted to seek your opinion regarding this.

Regards,
KP Bhat

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #1
22 12041
Generic Usenet Account wrote:
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. For example:

class A
{
public:
...
int xyz() { return _xyz; } // instead of get_xyz()
void xyz(const int val) { _xyz = val; } // instead of get_xyz()
...

protected:
...
int _xyz;
...
};
This naming convention is also consistent with the IDL/C++ language
binding, and I wanted to seek your opinion regarding this.


(Cross-post to comp.lang.c++.m oderated removed. Cross-posting to
moderated groups slows down responses.)

My opinion:

I don't see a real problem with your naming scheme, but I wouldn't use
it personally. Overloaded functions are generally used when multiple
functions perform the same (or a similar) task using different
arguments. In this case, the functions do rather different things, so I
would prefer different names.

Set/get member functions aren't inherently bad, but some beginners get
the idea that they are a standard part of every class, which is
completely wrong. Most classes shouldn't have a need for typical set and
get functions (those that correspond to the actual data members in the
class). The interface should be separate from the representation
(obviously) so except in rather simple cases, you can't expect a
well-designed interface to correspond to the members used to represent
the class's state.

One last thing, I would recommend not using names that begin with an
underscore. It's OK in the context above, but there's a lot of cases
where it's not. The easiest way to avoid problems is to get in the habit
of never using identifiers (macros, variables, etc.) that begin with an
underscore.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
Kevin Goodsell wrote:
Generic Usenet Account wrote: .... (Cross-post to comp.lang.c++.m oderated removed. Cross-posting to
moderated groups slows down responses.)

My opinion:

I don't see a real problem with your naming scheme, but I wouldn't use
it personally. Overloaded functions are generally used when multiple
functions perform the same (or a similar) task using different
arguments. In this case, the functions do rather different things, so I
would prefer different names.

Set/get member functions aren't inherently bad, but some beginners get
the idea that they are a standard part of every class, which is
completely wrong. Most classes shouldn't have a need for typical set and
get functions (those that correspond to the actual data members in the
class). The interface should be separate from the representation
(obviously) so except in rather simple cases, you can't expect a
well-designed interface to correspond to the members used to represent
the class's state.

One last thing, I would recommend not using names that begin with an
underscore. It's OK in the context above, but there's a lot of cases
where it's not. The easiest way to avoid problems is to get in the habit
of never using identifiers (macros, variables, etc.) that begin with an
underscore.


Opinion seconded.

Jul 19 '05 #3
le Friday 19 September 2003 13:08, us****@sta.sams ung.com écrivit :
int xyz() { return _xyz; } // instead of get_xyz()
void xyz(const int val) { _xyz = val; } // instead of get_xyz()


There is one small factual inconvienence I can think of.
While you can do :

vector<A> v;
int x;
for_each(v.begi n(), v.end(), bind2nd(mem_fun _ref(&A::set_xy z), x) );

You need specific code to reach the overloaded 'xyz' membre function :

void (A::* pmf)(const int) = &A::xyz;
for_each(v.begi n(), v.end(), bind2nd(mem_fun _ref(pmf), x) );
oh, and it's easier to search for 'set_xyz' in a file. (the overloaded one
can be searched using a regexp like 'xyz([^)]*)' , it's not too complicated
either)

except for those details, I think it does'nt make much of a difference.

--
Samuel.Krempp
cout << "@" << "crans." << (is_spam ? "trucs.en.trop. " : "" )
<< "ens-cachan.fr" << endl;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #4
Gianni Mariani <gi*******@mari ani.ws> wrote in message news:<bk******* *@dispatch.conc entric.net>...
Kevin Goodsell wrote:
One last thing, I would recommend not using names that begin with an
underscore. It's OK in the context above, but there's a lot of cases
where it's not. The easiest way to avoid problems is to get in the habit
of never using identifiers (macros, variables, etc.) that begin with an
underscore.


Opinion seconded.


How about ending with underscore?

-Chaud Lapin-
Jul 19 '05 #5
Le Chaud Lapin wrote:
Gianni Mariani <gi*******@mari ani.ws> wrote in message news:<bk******* *@dispatch.conc entric.net>...
Kevin Goodsell wrote:
One last thing, I would recommend not using names that begin with an
underscore . It's OK in the context above, but there's a lot of cases
where it's not. The easiest way to avoid problems is to get in the habit
of never using identifiers (macros, variables, etc.) that begin with an
underscore .


Opinion seconded.

How about ending with underscore?


That's fine as long as it's not immediately preceded by another
underscore. Identifiers containing a sequence of two underscores are
also reserved (though I don't know what for off the top of my head). So
this:

identifier_

is OK. These are not:

_identifier (well, it's OK in some places)
_Identifier (pretty much never OK)
_IDENTIFIER (also never OK)
identifier__
__identifier
foo__bar

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
Generic Usenet Account escribió:
Personally I prefer dropping the "set" and "get" prefixes from the
method names altogether. For example:
(snip)
This naming convention is also consistent with the IDL/C++ language
binding, and I wanted to seek your opinion regarding this.


In Spain we say: "aunque la mona se vista de seda, mona se queda". "A
monkey is a monkey even if dressed in silk" can be a translation. It is
the same thing wheter you use or not set and get prefixes. Personal
preference, as you say.

Regards.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #7
How about:

class A {
public:
int& EVIL() { return _evil; }
const int& EVIL() const { return _evil; }
};

This really is a horrible design... because you are exposing the underlying
implementation of your class. Which implies that if the implementation
changes, then the interface may need to be changed as well. That is more
evil than Sadam, Bin Laden, Stalin and Hitler put together. If you need to
be able to create setters and getters, why not just make the variable
public. If you need to have some other unrelated class access
private/protected elements of some other class, use friends instead. At
least you will not be breaking encapsulation.

GRRR

Martin
"Generic Usenet Account" <us****@sta.sam sung.com> wrote in message
news:90******** *************** *@posting.googl e.com...
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. For example:

class A
{
public:
...
int xyz() { return _xyz; } // instead of get_xyz()
void xyz(const int val) { _xyz = val; } // instead of get_xyz()
...

protected:
...
int _xyz;
...
};
This naming convention is also consistent with the IDL/C++ language
binding, and I wanted to seek your opinion regarding this.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #8

"Generic Usenet Account" <us****@sta.sam sung.com> wrote in message
news:90******** *************** *@posting.googl e.com...
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. For example:

class A
{
public:
...
int xyz() { return _xyz; } // instead of get_xyz()
void xyz(const int val) { _xyz = val; } // instead of get_xyz()
...

protected:
...
int _xyz;
...
};
This naming convention is also consistent with the IDL/C++ language
binding, and I wanted to seek your opinion regarding this.


I also prefer to overload a single meaningful name for this.

This is typically (but not always) feasible since one usually
takes an argument and the other does not. This form has worked
well for me so far.
-Mike

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #9
apm
us****@sta.sams ung.com (Generic Usenet Account) wrote in message news:<90******* *************** **@posting.goog le.com>...
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 dropp


So do I. Here's why:

The Uniform Access Principle espoused by Betrand Meyer states that all
services offerd by a module should be available through a uniform
notation which does not betray whether they are implemented through
storage or through computation. Get get/set convention violates this.

-Andrew Marlow

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Jul 19 '05 #10

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

Similar topics

27
6733
by: Derek | last post by:
The company where I work uses a naming convention that I have never used before. They use mixed-case letters for public member functions, but lower-case with underscores for the rest, like this: class Foo { public: void somePublicMemberFunction(); protected:
5
2437
by: Ook | last post by:
Is there any kind of naming convention for accessor and modifiers? What I've been doing is something like this: // accessor int getSize(); // Modifier void setSize( int newsize); private:
4
7149
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per convensions. The thing is that Ive never really get the full reference to check against. Ive seen a couple of articles, but there always seems to be a bit missing. I also always seem to run into conflicting convensions both in code samples themselves...
11
2002
by: tonicvodka | last post by:
What is the naming convention for functions in c#?
14
3144
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
6
4076
by: dm1608 | last post by:
I'm relatively new to ASP.NET 2.0 and am struggling with trying to find the best naming convention for the BAL and DAL objects within my database. Does anyone have any recommendations or best practices for naming my objects? I currently have all my type classses simply called "JobSummaryClass" or "JobDetailsClass". These classes simply contain the public properties and the get/set functions for the object. Is this an appropriate naming...
114
7888
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for any comments. --
35
12200
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or recommended practise? Thanks.
23
2445
by: Thorsten Kampe | last post by:
Okay, I hear you saying 'not another naming conventions thread'. I've read through Google and the 'naming conventions' threads were rather *spelling conventions* threads. I'm not interested in camelCase versus camel_case or anything mentioned in 'PEP 8 -- Style Guide for Python Code'. What I'm looking for is hints or ideas how to name your variables and especially how to name functions, methods and classes.
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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,...
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
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
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
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.