java 幻数:检查幻数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14399234/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:08:50  来源:igfitidea点击:

Magic Number: Checks for magic numbers

java

提问by Oomph Fortuity

I got this warning on Sonar as a violation. I want proper solution to remove this warning from sonar.

我在 Sonar 上收到此警告是违规行为。我想要适当的解决方案来从声纳中删除这个警告。

My code is like this:

我的代码是这样的:

void method(){
try {
  int x;
  x=5;
 }
 catch{
    //handling code
 }
}

I got warning for this code like:

我收到此代码的警告,例如:

'5' is a magic number.

So, I want proper solution to remove such warning.

所以,我想要适当的解决方案来删除这样的警告。

回答by PermGenError

Magic number is the direct usage of the number in the code(i.e., hard-coded number in the code in your case using 5 directly)

幻数是代码中数字的直接使用(即你的情况下代码中的硬编码数字直接使用5)

to get rid of the warning try this:

摆脱警告试试这个:

 int x;
 static final int SOME_NUMBER=5;
 x=SOME_NUMBER;

回答by meriton

Sonar is asking you to document why you use that particular number by giving it a name. You can do so by declaring a constant (with an expressive name):

Sonar 要求您通过为其命名来记录使用该特定数字的原因。你可以通过声明一个常量(用一个富有表现力的名字)来做到这一点:

static final int NUMBER_OF_RETRIES = 5;

and then use that constant instead of the "magic" number, thereby expressing the intent of that assignment more clearly:

然后使用该常量而不是“神奇”数字,从而更清楚地表达该赋值的意图:

x = NUMBER_OF_RETRIES;

This also has the advantage that if NUMBER_OF_RETRIES needs to be changed, you can do so in one place, rather than whereever that "magic" number is used.

这还有一个优点,如果 NUMBER_OF_RETRIES 需要更改,您可以在一个地方进行,而不是在使用“神奇”数字的任何地方。

回答by Thales Valias

Well, I'm aware that this question has already been answered satisfactorily, but I'd like to add here the own Sonar explanation, since it's quite well elaborated:

好吧,我知道这个问题已经得到了令人满意的回答,但我想在这里添加自己的声纳解释,因为它已经很好地阐述了:

A magic number is a number that comes out of nowhere, and is directly used in a statement. Magic numbers are often used, for instance to limit the number of iterations of a loops, to test the value of a property, etc.

Using magic numbers may seem obvious and straightforward when you're writing a piece of code, but they are much less obvious and straightforward at debugging time.

That is why magic numbers must be demystified by first being assigned to clearly named variables before being used.

-1, 0 and 1 are not considered magic numbers.

Noncompliant Code Example

public static void doSomething() {
  for(int i = 0; i < 4; i++){                 // Noncompliant, 4 is a magic number
      ...
  }
}

Compliant Solution

public static final int NUMBER_OF_CYCLES = 4;
public static void doSomething() {
  for(int i = 0; i < NUMBER_OF_CYCLES ; i++){
    ...
  }
}

Exceptions

This rule ignores hashCode methods.

幻数是一个不知从哪里冒出来的数字,直接在语句中使用。经常使用幻数,例如限制循环的迭代次数,测试属性的值等。

在编写一段代码时,使用幻数可能看起来很明显和直接,但在调试时它们就不那么明显和直接了。

这就是为什么必须在使用之前首先将幻数分配给明确命名的变量来揭开幻数的神秘面纱。

-1、0 和 1 不被视为幻数。

不合规的代码示例

public static void doSomething() {
  for(int i = 0; i < 4; i++){                 // Noncompliant, 4 is a magic number
      ...
  }
}

合规解决方案

public static final int NUMBER_OF_CYCLES = 4;
public static void doSomething() {
  for(int i = 0; i < NUMBER_OF_CYCLES ; i++){
    ...
  }
}

例外

此规则忽略 hashCode 方法。