TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

What is a magic number, and why is it bad?

31 点作者 carlosgg超过 11 年前

10 条评论

bicx超过 11 年前
I just have a Numbers class I import into all my projects:<p>public final static int ZERO = 0; public final static int ONE = 1; public final static int TWO = 2; public final static int THREE = 3; public final static int FOUR = 4; ....<p>(jk)
评论 #6290938 未加载
评论 #6291045 未加载
评论 #6290711 未加载
radio4fan超过 11 年前
The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant.<p>This also simplifies modifying the program, should the value of pi change.<p>-- Early FORTRAN manual for Xerox Computers
dllthomas超过 11 年前
No one seems to be mentioning another use of the phrase &quot;magic number&quot; which isn&#x27;t bad at all - numbers put in files to mark the file type. 0xCAFEBABE in java class files, for instance.
评论 #6291149 未加载
bluedino超过 11 年前
That is about the worst top-rated answer I&#x27;ve seen in a while on SO.
评论 #6290714 未加载
评论 #6290571 未加载
nicholassmith超过 11 年前
I&#x27;d have personally thought the number used in the fast inverse square root (<a href="http://en.wikipedia.org/wiki/Fast_inverse_square_root" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fast_inverse_square_root</a>) would qualify as a &#x27;magic number&#x27;, everything else is just a hardcoded value.
timje1超过 11 年前
I find these a lot with CSS - banishing them seem to be a major goal of LESS, SASS etc.<p>They also seem to crop up too often in JS &#x2F; JQuery, particularly the manipulation of CSS.. where do you folks tend to find them?
dsego超过 11 年前
Also, it can be a good idea to yank out all of those into a separate config file. Although, it destroys locality, so it depends on the use case I guess.
评论 #6294584 未加载
prezjordan超过 11 年前
New term added to my vocabulary - thanks for sharing!
tzs超过 11 年前
I just call any constants that slip into my code &quot;advanced technology numbers&quot; if someone gives me a hard time about them.
a3voices超过 11 年前
Caring about magic numbers is a distraction from real problems, such as getting your software working.
评论 #6290692 未加载
评论 #6290858 未加载
评论 #6290536 未加载
评论 #6290870 未加载
评论 #6290560 未加载
评论 #6290679 未加载
评论 #6290582 未加载