英文:
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;
}
}
英文:
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;
}
}
答案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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论