访问器方法的命名约定(get / set) [英] Naming convention for accessor methods (get/set)

查看:87
本文介绍了访问器方法的命名约定(get / set)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个新闻组中有很多关于邪恶的说法。 set / get

访问器方法。亚瑟瑞尔(先锋训练),在他的班上,

Heuristis for O-O Analysis& 设计说,几乎从来没有一个借口方法的借口。就个人而言,我没有那么远。我觉得他们觉得它们有用的目的(尽管是以有限的方式)。

就个人而言,我更喜欢放弃套装。和得到来自

方法名称的前缀完全相同。例如:


class A

{

public:

...

int xyz(){return _xyz; } //而不是get_xyz()

void xyz(const int val){_ xz = val; } //而不是get_xyz()

...


受保护:

...

int _xyz;

...

};

这个命名约定也与IDL / C ++语言一致

绑定,我想就此寻求你的意见。


问候,

KP Bhat


[见 http://www.gotw.ca/resources/clcm。 htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

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++.moderated. First time posters: Do this! ]

推荐答案

Generic Usenet Account写道:
Generic Usenet Account wrote:
在这个新闻组中有很多关于 ;恶"设置/获取
访问器方法。亚瑟瑞尔(先锋训练),在他的课堂上,
Heuristis for O-O Analysis&设计,说几乎从来没有借口方法的借口。就个人而言,我没有那么远。我确实觉得它们有用的目的(尽管是以有限的方式)。
我个人更喜欢放弃套装。和得到来自
方法名称的前缀完全相同。例如:

A类
{
公开:
...
int xyz(){return _xyz; } //而不是get_xyz()
void xyz(const int val){_ xyz = val; } //而不是get_xyz()
...

受保护:
...
int _xyz;
...
};

这个命名约定也与IDL / C ++语言
绑定一致,我想征求你对此的看法。
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.




(交叉发布到comp.lang.c ++。审核已删除。交叉发布到

审核组会降低响应速度。)


我的意见:


我没有看到你的命名方案真正的问题,但我个人不会使用

。当多个

函数使用不同的

参数执行相同(或类似)任务时,通常会使用重载函数。在这种情况下,函数做了相当不同的事情,所以我

会更喜欢不同的名字。


设置/获取成员函数本身并不坏,但是一些初学者得出的结论是,他们是每个班级的标准部分,这是完全错误的。大多数课程不需要典型的设置和

获取功能(那些与

类中的实际数据成员相对应的功能)。界面应该与代表

(显然)分开,所以除了相当简单的情况外,你不能指望

精心设计的界面对应成员曾经代表

班级的状态。


最后一件事,我建议不要使用以
$ b开头的名字$ b下划线。在上面的上下文中它是可以的,但是有很多情况

它不是。避免问题的最简单方法是养成从不使用以

下划线开头的标识符(宏,变量等)的习惯。




-Kevin

-

我的电子邮件地址有效,但会定期更改。

请联系我使用最近发布的地址。



(Cross-post to comp.lang.c++.moderated 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.


Kevin Goodsell写道:
Kevin Goodsell wrote:
Generic Usenet Account写道:
.. ..(交叉发布到comp.lang.c ++。审核已删除。交叉发布到
审核组会减慢响应。)

我的意见:

我没有看到你的命名方案存在真正的问题,但我不会亲自使用它。当多个
函数使用不同的
参数执行相同(或类似)任务时,通常会使用重载函数。在这种情况下,函数做了不同的事情,所以我更喜欢不同的名字。

设置/获取成员函数本身并不坏,但是一些初学者得到了
认为它们是每个班级的标准部分,这是完全错误的。大多数类不需要典型的set和
get函数(那些与
类中的实际数据成员相对应的函数)。界面应该与表示分开(显然),除非在相当简单的情况下,你不能指望一个设计良好的界面对应于用来代表的成员
班级的状态。

最后一件事,我建议不要使用以
下划线开头的名字。在上面的上下文中它是可以的,但是有很多情况
它不是。避免问题的最简单方法是养成从不使用以
下划线开头的标识符(宏,变量等)的习惯。
Generic Usenet Account wrote: .... (Cross-post to comp.lang.c++.moderated 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.


le Friday 2003年9月19日13:08, us **** @ sta.samsung.com écrivit:
le Friday 19 September 2003 13:08, us****@sta.samsung.com écrivit :
int xyz(){return _xyz; } //而不是get_xyz()
void xyz(const int val){_ xyz = val; } //而不是get_xyz()
int xyz() { return _xyz; } // instead of get_xyz()
void xyz(const int val) { _xyz = val; } // instead of get_xyz()




我可以想到一个小的事实上的不一致。

虽然你能做到:


vector< A> v;

int x;

for_each(v.begin(),v.end(),bind2nd(mem_fun_ref(& A :: set_xyz),x)) ;


您需要特定的代码来达到重载的''xyz''membre函数:


void(A :: * pmf)( const int)=& A :: xyz;

for_each(v.begin(),v.end(),bind2nd(mem_fun_ref(pmf),x));

哦,并且在文件中搜索''set_xyz''更容易。 (重载的一个

可以使用像'xyz([^)] *''这样的正则表达式来搜索,它不是太复杂了

)<除了这些细节,我认为它没有太大区别。


-

Samuel.Krempp

cout<< " @" << "。克莱恩" << (is_spam?" trucs.en.trop。":"")

<< " ens-cachan.fr" << endl;

[见 http:// www。 gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



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

vector<A> v;
int x;
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&A::set_xyz), x) );

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

void (A::* pmf)(const int) = &A::xyz;
for_each(v.begin(), 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++.moderated. First time posters: Do this! ]


这篇关于访问器方法的命名约定(get / set)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆