Assign this magic number 5 to a well-named constant, and use the constant instead

huangapple go评论92阅读模式
英文:

Assign this magic number 5 to a well-named constant, and use the constant instead

问题

我从SONAR收到以下错误,我真的不理解以下问题的更改将如何解决,有人可以提供建议吗?

 @Component
    public class CatalogRequestValidator {
    	
    	public void validateBaseOffersReq(CatalogRequest baseOffersRequest) {
    		int ZIP_CODE_LENGTH=5;
        }
    }

Assign this magic number 5 to a well-named constant, and use the constant instead

英文:

I am getting below error from SONAR and i am really not understand with what changes below issues will be resolved can some one suggest me please

Class

 @Component
    public class CatalogRequestValidator {
    	
    	public void validateBaseOffersReq(CatalogRequest baseOffersRequest) {
    		int ZIP_CODE_LENGTH=5;
        }
    }

Assign this magic number 5 to a well-named constant, and use the constant instead

答案1

得分: 2

在这个上下文中,常量是类中的public static final字段,而不是方法变量。

英文:

a constant in this context is a public static final field in a class, not a method variable.

答案2

得分: 1

第一个声纳建议意味着您的本地变量ZIP_CODE_LENGTH的格式不正确,不符合标准。它应该是zipCodeLength(驼峰命名法)。

第二个建议意味着数字5应该被赋值给一个常量。

为了解决这两个建议,只需将本地变量定义为方法之外的常量(私有或公有)。

private static final int ZIP_CODE_LENGTH = 5;
英文:

The first sonar advice means that the format of your local variable ZIP_CODE_LENGTH is wrong, according to standards. It should be zipCodeLength (camelCase).

The second advice means that the number 5 should be assigned to a constant.

In order to solve both advices just define the local variable as a constant outside the method (private or public).

private static final int ZIP_CODE_LENGTH=5; 

huangapple
  • 本文由 发表于 2020年9月22日 17:57:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64007331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定