MAGIC号码总是不好吗? [英] Are MAGIC numbers always bad?

查看:89
本文介绍了MAGIC号码总是不好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MAGIC号码总是坏吗?

在程序中使用魔术数字(常数)通常被认为是一种糟糕的编程习惯,而且它是'' s建议在页头文件中的单个可见位置定义常量
。我的问题

是否有任何情况下使用魔术数字不是什么?b $ b必然是坏事?假设我们想要使用一些常量默认数字在类中初始化一些

私有变量,我们可以使用幻数直接初始化它们吗?我们还应该在头文件中定义那些常量吗?如果我们在头文件中定义它们,

它们可供我们班级的客户使用,我们可能不需要b $ b b。我们可以把它们放在cpp文件中吗?


我将非常感谢你的评论。


John

Are MAGIC numbers always bad?

Using magic numbers (constant numbers) in programs are generally
considered a bad programming practice, and it''s recommended that to
define constants in single, visible place in a header file. My question
is that is there any situation where using magic numbers is not
necessarily a bad thing? lets say that we want to initialize some
private variables in a class using some constant default numbers, can
we initialize them directly using magic numbers? should we still define
those constants in a header file? If we define them in a header file,
they become available to the clients of our class, which we may not
want; can we put them in the cpp file?

I''d appreciate your comments.

John

推荐答案

您始终可以在实现文件中将它们定义为枚举

值,或者在未命名的命名空间中。

You can always define them in the implementation file as enumerated
values, or in an unnamed namespace.


yo ******** @ yahoo.com 写道:
MAGIC数字总是不好吗?

在程序中使用幻数(常数)通常被认为是一种糟糕的编程习惯,而且它'建议在头文件中的单个可见位置定义常量。我的问题
是否有任何情况下使用魔术数字不一定是坏事?假设我们想要使用一些常量默认数字在类中初始化一些私有变量,我们可以使用幻数直接初始化它们吗?


你必须在某个地方给一个常数赋值,这样

常数就会取代魔数。


我们还应该在头文件中定义这些常量吗?


你可以。


如果我们在头文件中定义它们,它们可供我们班级的客户使用,我们可能不会
想要;我们可以把它们放在cpp文件中吗?
Are MAGIC numbers always bad?

Using magic numbers (constant numbers) in programs are generally
considered a bad programming practice, and it''s recommended that to
define constants in single, visible place in a header file. My question
is that is there any situation where using magic numbers is not
necessarily a bad thing? lets say that we want to initialize some
private variables in a class using some constant default numbers, can
we initialize them directly using magic numbers?
Well you have to assign a value to a constant somewhere, so that
constant replaces the magic number.

should we still define those constants in a header file?
You could.

If we define them in a header file, they become available to the clients of our class, which we may not
want; can we put them in the cpp file?



如果你指的是静态成员变量,是的。但你可以

也让它们变成私人。


-

Ian Collins。


If you are referring to static member variables, yes. But you could
also make them private.

--
Ian Collins.


无需发布头文件;你可以使用私人头文件。


魔术数字总是很糟糕。但有时替代方案更糟糕。

通常使用静态常量更好。对于enum的成员,

除非您真正设计枚举。任何数据类型都可以是一个静态const,但只有整数类型才能用于枚举。


总有一天我会设计自己的编程语言。唯一的常数

将为零,一,e和两个pi。 :-)

Header files need not be published; you can use private header files.

Magic numbers are always bad. But sometimes the alternatives are worse.
It is generally better to use "static const" to members of an "enum,"
except where you are really designing an enumeration. Any data type can
be a static const, but only integral types can be used in an enum.

Someday I will design my own programming language. The only constants
will be zero, one, e, and two pi. :-)


这篇关于MAGIC号码总是不好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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