java 带有警告“100”的 CheckStyle 是一个神奇的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30044804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CheckStyle with warning '100' is a magic number
提问by mohammed sameen
In my code,it shows the warning with message '100' is a magic number.See the below code,
在我的代码中,它显示带有消息“100”的警告是一个幻数。请参阅下面的代码,
int randomNo = generator.nextInt(100);
I read it here What is a magic number, and why is it bad?but my doubt is declaring 100 by creating variable with static will occupy more space,since i am using this in a single place.Is this a correct way to solve this?
我在这里读到什么是幻数,为什么它不好?但我的疑问是通过使用静态创建变量来声明 100 会占用更多空间,因为我在一个地方使用它。这是解决这个问题的正确方法吗?
public static final int HUNDRED= 100;
Any Suggestion?
有什么建议吗?
回答by yurib
well HUNDRED
really is kinda silly, but why did you choose 100 and what is its meaning?
也HUNDRED
确实是有点儿傻,但你为什么选择100,什么是它的意义?
something like:
就像是:
public static final int RANDOM_UPPER_LIMIT=100;
or something even more informative, depending on what you use the value for:
或者更多信息,具体取决于您使用该值的目的:
public static final int MAX_NUMBER_OF_COLORS=100;
would make more sense and improve readability.
会更有意义并提高可读性。
Space saving should not be a consideration in this case, the space overhead of declaring a variable, if there is such, is completely negligible.
在这种情况下不应该考虑节省空间,声明一个变量的空间开销,如果有的话,完全可以忽略不计。
回答by Bill K
It doesn't really have to do with storage, it's readability. If you want to change some number it can be difficult to find in code, if it's up at the top then it's better (if it's in a config file, better yet in many cases).
它与存储无关,而是可读性。如果你想改变一些在代码中很难找到的数字,如果它在顶部,那么它会更好(如果它在配置文件中,在许多情况下更好)。
Yes, that's a good solution.
是的,这是一个很好的解决方案。
If you don't need it outside that file, you should make it "private", and you might want to be even more readable and use a name that indicates what it really means, like:
如果您在该文件之外不需要它,您应该将其设为“私有”,并且您可能希望更具可读性并使用一个表明其真正含义的名称,例如:
MAX_RANDOM_NUMBER=100
better yet include what it's used for
最好包括它的用途
MAX_RANDOM_FOR_CARD_SELECTION
or something like that.
或类似的东西。
In this way when you go to look into that file 5 months from now because you added 20 new cards, it's completely obvious what you have to change without even glancing at the code.
这样,当您在 5 个月后查看该文件时,因为您添加了 20 张新卡,您甚至无需看代码就完全清楚您必须更改的内容。
回答by Tagir Valeev
It's good to write not the shortest code, but code which is easy to understand and maintain. Storing this 100
to constant you may add a good name explaining why it's really 100
. For example if you want to generate random score and your maximal possible score is 100
, then you can define
最好不要编写最短的代码,而是编写易于理解和维护的代码。将此存储100
为常量,您可能会添加一个好名字来解释为什么它真的是100
. 例如,如果您想生成随机分数并且您的最大可能分数是100
,那么您可以定义
static final int MAX_SCORE = 100;
After that you can use it in other places as well. Everybody will understand why it's 100 and nothing else. And if someday you will need to change it, say, to 200, you will have to replace it in only one place without searching through the code.
之后,您也可以在其他地方使用它。每个人都会明白为什么它是 100 而没有别的。如果有一天您需要将其更改为 200,您将只需要在一个地方替换它,而无需搜索代码。
Also it's possible that in some other part of your program you will have 100 which has different meaning (say, MAX_PERCENT
). If you want to change MAX_SCORE
to 200, but leave MAX_PERCENT
as is, it would be much easier if you have separate constants.
也有可能在你的程序的其他部分你会有 100 个具有不同含义的(比如,MAX_PERCENT
)。如果您想更改MAX_SCORE
为 200,但保持MAX_PERCENT
原样,如果您有单独的常量会容易得多。
回答by ttarczynski
Check out Robert Martin's (Uncle Bob)
看看罗伯特·马丁的(鲍勃叔叔)
Clean Code
干净的代码
book for a thorough explanaition (or any other guide on coding style). Basically a hard coded '100' doesn't mean anything to the reader of your code. It won't mean anything to you in 6 months either, after you are done with your app. It is a magic number since it appears in the code - in 99 out of 100 cases - as almost out of nowhere. Why is it 100 and not 99 or 101? Why should your app limit the random number generation to 100 and not above (or below)?
本书以获得详尽的解释(或任何其他有关编码风格的指南)。基本上,硬编码的“100”对代码的读者没有任何意义。在您完成应用程序后的 6 个月内,它对您也没有任何意义。这是一个神奇的数字,因为它出现在代码中 - 在 100 个案例中有 99 个 - 几乎无处不在。为什么是 100 而不是 99 或 101?为什么您的应用程序应将随机数生成限制为 100 而不是高于(或低于)?
To wrap up, it's a thing of readability of your code for yourself and for present or future readers of your code, it's a matter of coding style.
总而言之,对于您自己以及您的代码的当前或未来读者来说,这是一个代码可读性的问题,这是一个编码风格的问题。