意见)过度使用符号常量 [英] Opinion) Overuse of symbolic constants

查看:96
本文介绍了意见)过度使用符号常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从第一版K& R发布之时起,

使用符号常量的优势,而不是魔术数字,

被强调----并且有充分的理由。我不会对

所有人提出异议。然而,当人们进行这种练习时,它会让我感到紧张.......
太远了。考虑这些例子(来自

尊敬的同事所写的代码):


#define DASH'' - ''

#define SLASH''/''

#define SINGLE_BYTE 1


使用有意义的符号常量是一回事(在某些情况下)
个案例,抽象)名称,但是这样使用符号常量的方法就是完全浪费。我们来看第一个例子。要么是
,DASH的定义永远不会改变(在这种情况下,它的用法是多余的)或者DASH的定义将来会改变(在

这种情况​​会完全误导)。

有什么意见吗?


--SS

Right from the time the first edition of K&R was released, the
advantages of using symbolic constants, as opposed to "magic numbers",
has been emphasized ---- and for good reason. I don''t dispute that at
all. However, it gets on my nerves when people carry this practice
too far. Consider these examples (from the code written by a
distinguished colleague):

#define DASH ''-''
#define SLASH ''/''
#define SINGLE_BYTE 1

It is one thing to use symbolic constants with meaningful (and in some
cases, abstract) names, but methinks that use of symbolic constants in
this way is a complete waste. Let us take the first example. Either
the definition of DASH will never change (in which case it''s usage is
superfluous) or the definition of DASH will change in the future (in
which case it will be completely misleading).
Any opinions?

--SS

推荐答案

>从第一版K& R发布之时起,
> Right from the time the first edition of K&R was released, the
使用符号常量的优势,而不是魔术数字,
已被强调----和有充分理由。
所有我都不反对。然而,当人们进行这种练习时,它会让我感到紧张。考虑这些例子(来自尊敬的同事编写的代码):

#define DASH'' - ''
#define SLASH''/''
#define SINGLE_BYTE 1

使用符号常量与有意义的(在一些
情况下,抽象)名称是一回事,但是这样使用符号常量的方法完全是浪费。我们来看第一个例子。要么DASH的定义永远不会改变(在这种情况下它的用法是多余的),或者DASH的定义将来会改变(在这种情况下它会完全改变)误导)。
advantages of using symbolic constants, as opposed to "magic numbers",
has been emphasized ---- and for good reason. I don''t dispute that at
all. However, it gets on my nerves when people carry this practice
too far. Consider these examples (from the code written by a
distinguished colleague):

#define DASH ''-''
#define SLASH ''/''
#define SINGLE_BYTE 1

It is one thing to use symbolic constants with meaningful (and in some
cases, abstract) names, but methinks that use of symbolic constants in
this way is a complete waste. Let us take the first example. Either
the definition of DASH will never change (in which case it''s usage is
superfluous) or the definition of DASH will change in the future (in
which case it will be completely misleading).




我认为定义或常量有三个重要用途:


1.对于可能发生变化的值。

2.区分某个值的使用与其他用法,以便

搜索更有效。

3.更有意义名称(例如PATH_NOT_FOUND而不是值)。


我认为上面显示的示例不适用于任何这些用法。


Niels Dybdahl



I consider three important uses for defines or constants:

1. For values that are subject to change.
2. To distinguish a certain usage of a value from other usages, so that
searches are more efficient.
3. More meaningfull names (e.g. PATH_NOT_FOUND instead of a value).

I do not think that the examples shown above fits into any of these usages.

Niels Dybdahl


在comp.lang.c中,Sandeep Sharma< sa ********* @ yahoo.com>写道:


(仅限comp.lang.c,我没有看到clc ++对此很关心)
In comp.lang.c Sandeep Sharma <sa*********@yahoo.com> wrote:

(comp.lang.c only, I don''t see clc++ caring much about this)
#define DASH' ' - ''
#define SLASH''/''
#define SINGLE_BYTE 1
#define DASH ''-''
#define SLASH ''/''
#define SINGLE_BYTE 1




我正在处理类似的代码,除了我们的样子好像


#define Splat''*''

#define Andpersand''&''

#定义Asterik''*''


这些是逐字的 - 是的,&符号和星号拼写错误,并且

常量不是大写的。我在* I *编写的代码中随时都不使用这些

。作者是我的老板,所以

我猜他是杰出的也是:


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



I''m dealing with similar code, except that ours look like

#define Splat ''*''
#define Andpersand ''&''
#define Asterik ''*''

Those are verbatim - yes, ampersand and asterisk are misspelled, and
the constants aren''t capitalized. I make a point of not using these
at any time in the code that *I* write. And the author is my boss, so
I guess he''s "distinguished" too :)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Christopher Benson-Manica< at *** @ nospam.cyberspace.org>潦草地写道:
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
在comp.lang.c中,Sandeep Sharma< sa ********* @ yahoo.com>写道:
(仅限comp.lang.c,我没有看到clc ++关心这一点)
In comp.lang.c Sandeep Sharma <sa*********@yahoo.com> wrote: (comp.lang.c only, I don''t see clc++ caring much about this)
#define DASH' ' - ''
#define SLASH''/''
#define SINGLE_BYTE 1
#define DASH ''-''
#define SLASH ''/''
#define SINGLE_BYTE 1



我正在处理类似的代码,除了我们看起来像
#define Splat''*''
#define Andpersand''&''
#define Asterik''*''
这些是逐字的 - 是的,&符号和星号拼写错误,并且常数不是大写的。我在* I *编写的代码中随时都不使用这些
。作者是我的老板,所以
我猜他是杰出的。也是:)


I''m dealing with similar code, except that ours look like #define Splat ''*''
#define Andpersand ''&''
#define Asterik ''*'' Those are verbatim - yes, ampersand and asterisk are misspelled, and
the constants aren''t capitalized. I make a point of not using these
at any time in the code that *I* write. And the author is my boss, so
I guess he''s "distinguished" too :)




你的老板显然没有读过足够的欧洲漫画。 =)


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)------- ------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

"''所谓的''意味着'''这有很长的解释,但我没有

时间在这里解释一下。''"

- JIPsoft



Your boss obviously hasn''t read enough European comics. =)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"''So called'' means: ''There is a long explanation for this, but I have no
time to explain it here.''"
- JIPsoft


这篇关于意见)过度使用符号常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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