How to avoid magic number warning when initialize static field (for example BigDecimal)?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have static field
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(299_999L);
And I get warning from CheckStyle that 299_999L
is magic number.
How can I avoid this - 299_999 is just long transform into specified BigDecimal
.
I did not find in CheckStyle documentation any suitable solution.
EDIT:
It comes out when I type for example:
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN)
java initialization field bigdecimal checkstyle
add a comment |
I have static field
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(299_999L);
And I get warning from CheckStyle that 299_999L
is magic number.
How can I avoid this - 299_999 is just long transform into specified BigDecimal
.
I did not find in CheckStyle documentation any suitable solution.
EDIT:
It comes out when I type for example:
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN)
java initialization field bigdecimal checkstyle
Interesting! I can't reproduce this behavior. Which version of Checkstyle are you using? Can you share theMagicNumber
config from your checkstyle.xml? According to the docs, you should not get this warning.
– barfuin
Jan 3 at 13:25
Auch my bad. It comes out when I type BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN);
– sajgon338
Jan 3 at 16:37
add a comment |
I have static field
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(299_999L);
And I get warning from CheckStyle that 299_999L
is magic number.
How can I avoid this - 299_999 is just long transform into specified BigDecimal
.
I did not find in CheckStyle documentation any suitable solution.
EDIT:
It comes out when I type for example:
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN)
java initialization field bigdecimal checkstyle
I have static field
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(299_999L);
And I get warning from CheckStyle that 299_999L
is magic number.
How can I avoid this - 299_999 is just long transform into specified BigDecimal
.
I did not find in CheckStyle documentation any suitable solution.
EDIT:
It comes out when I type for example:
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN)
java initialization field bigdecimal checkstyle
java initialization field bigdecimal checkstyle
edited Jan 6 at 20:32
Alex
2,28233052
2,28233052
asked Jan 3 at 1:27
sajgon338sajgon338
86
86
Interesting! I can't reproduce this behavior. Which version of Checkstyle are you using? Can you share theMagicNumber
config from your checkstyle.xml? According to the docs, you should not get this warning.
– barfuin
Jan 3 at 13:25
Auch my bad. It comes out when I type BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN);
– sajgon338
Jan 3 at 16:37
add a comment |
Interesting! I can't reproduce this behavior. Which version of Checkstyle are you using? Can you share theMagicNumber
config from your checkstyle.xml? According to the docs, you should not get this warning.
– barfuin
Jan 3 at 13:25
Auch my bad. It comes out when I type BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN);
– sajgon338
Jan 3 at 16:37
Interesting! I can't reproduce this behavior. Which version of Checkstyle are you using? Can you share the
MagicNumber
config from your checkstyle.xml? According to the docs, you should not get this warning.– barfuin
Jan 3 at 13:25
Interesting! I can't reproduce this behavior. Which version of Checkstyle are you using? Can you share the
MagicNumber
config from your checkstyle.xml? According to the docs, you should not get this warning.– barfuin
Jan 3 at 13:25
Auch my bad. It comes out when I type BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN);
– sajgon338
Jan 3 at 16:37
Auch my bad. It comes out when I type BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN);
– sajgon338
Jan 3 at 16:37
add a comment |
3 Answers
3
active
oldest
votes
After your clarifying comment, I can say that the reason for the warning is in the way the MagicNumber check works. If the potential magic number is in a field definition, and that field is final
, then it is not flagged as long as all parent tokens in the AST up to the node representing the field definition are in a certain list.
This is quite confusing, and I think that to a normal user, it seems arbitrary. But static code analysis is often about heuristics.
The good thing is you can influence this behavior. Configure the check like this:
<module name="MagicNumber">
<property name="constantWaiverParentToken"
value="TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, UNARY_PLUS, ELIST, STAR, ASSIGN, PLUS, MINUS, DIV, LITERAL_NEW, DOT"/>
</module>
The value of constantWaiverParentToken
is the default plus DOT
added to the end. This allows for more complex expressions. You need at least Checkstyle 6.11 for this to work.
1
Great, that solve my problem. Thank you very much!
– sajgon338
Jan 4 at 5:59
It should be noted thatconstantWaiverParentToken
doesn't take into account the method being called, so it will hide violations where the method is changing the constant's value or if there are multiple constants in the method call.
– rveach
Jan 5 at 3:07
add a comment |
It is interesting that you are getting this error while declaring a static field. But any how you can add suppresswarning annotation,
@SuppressWarnings("checkstyle:magicnumber")
add a comment |
You have to declare a constant to describe a little more of what that value means:
private static final Long MAX_AMOUNT_INITIALIZER = 299_000L;
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(MAX_AMOUNT_INITIALIZER);
It sounds redundant but remember that a magic number is any number used along the code that does not have any explanation of its meaning. Even though you have a BigDecimal
constant, your Long
value is not interpreted as a "meaningful value" for the context.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015246%2fhow-to-avoid-magic-number-warning-when-initialize-static-field-for-example-bigd%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
After your clarifying comment, I can say that the reason for the warning is in the way the MagicNumber check works. If the potential magic number is in a field definition, and that field is final
, then it is not flagged as long as all parent tokens in the AST up to the node representing the field definition are in a certain list.
This is quite confusing, and I think that to a normal user, it seems arbitrary. But static code analysis is often about heuristics.
The good thing is you can influence this behavior. Configure the check like this:
<module name="MagicNumber">
<property name="constantWaiverParentToken"
value="TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, UNARY_PLUS, ELIST, STAR, ASSIGN, PLUS, MINUS, DIV, LITERAL_NEW, DOT"/>
</module>
The value of constantWaiverParentToken
is the default plus DOT
added to the end. This allows for more complex expressions. You need at least Checkstyle 6.11 for this to work.
1
Great, that solve my problem. Thank you very much!
– sajgon338
Jan 4 at 5:59
It should be noted thatconstantWaiverParentToken
doesn't take into account the method being called, so it will hide violations where the method is changing the constant's value or if there are multiple constants in the method call.
– rveach
Jan 5 at 3:07
add a comment |
After your clarifying comment, I can say that the reason for the warning is in the way the MagicNumber check works. If the potential magic number is in a field definition, and that field is final
, then it is not flagged as long as all parent tokens in the AST up to the node representing the field definition are in a certain list.
This is quite confusing, and I think that to a normal user, it seems arbitrary. But static code analysis is often about heuristics.
The good thing is you can influence this behavior. Configure the check like this:
<module name="MagicNumber">
<property name="constantWaiverParentToken"
value="TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, UNARY_PLUS, ELIST, STAR, ASSIGN, PLUS, MINUS, DIV, LITERAL_NEW, DOT"/>
</module>
The value of constantWaiverParentToken
is the default plus DOT
added to the end. This allows for more complex expressions. You need at least Checkstyle 6.11 for this to work.
1
Great, that solve my problem. Thank you very much!
– sajgon338
Jan 4 at 5:59
It should be noted thatconstantWaiverParentToken
doesn't take into account the method being called, so it will hide violations where the method is changing the constant's value or if there are multiple constants in the method call.
– rveach
Jan 5 at 3:07
add a comment |
After your clarifying comment, I can say that the reason for the warning is in the way the MagicNumber check works. If the potential magic number is in a field definition, and that field is final
, then it is not flagged as long as all parent tokens in the AST up to the node representing the field definition are in a certain list.
This is quite confusing, and I think that to a normal user, it seems arbitrary. But static code analysis is often about heuristics.
The good thing is you can influence this behavior. Configure the check like this:
<module name="MagicNumber">
<property name="constantWaiverParentToken"
value="TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, UNARY_PLUS, ELIST, STAR, ASSIGN, PLUS, MINUS, DIV, LITERAL_NEW, DOT"/>
</module>
The value of constantWaiverParentToken
is the default plus DOT
added to the end. This allows for more complex expressions. You need at least Checkstyle 6.11 for this to work.
After your clarifying comment, I can say that the reason for the warning is in the way the MagicNumber check works. If the potential magic number is in a field definition, and that field is final
, then it is not flagged as long as all parent tokens in the AST up to the node representing the field definition are in a certain list.
This is quite confusing, and I think that to a normal user, it seems arbitrary. But static code analysis is often about heuristics.
The good thing is you can influence this behavior. Configure the check like this:
<module name="MagicNumber">
<property name="constantWaiverParentToken"
value="TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, UNARY_PLUS, ELIST, STAR, ASSIGN, PLUS, MINUS, DIV, LITERAL_NEW, DOT"/>
</module>
The value of constantWaiverParentToken
is the default plus DOT
added to the end. This allows for more complex expressions. You need at least Checkstyle 6.11 for this to work.
answered Jan 3 at 20:16


barfuinbarfuin
11.2k760101
11.2k760101
1
Great, that solve my problem. Thank you very much!
– sajgon338
Jan 4 at 5:59
It should be noted thatconstantWaiverParentToken
doesn't take into account the method being called, so it will hide violations where the method is changing the constant's value or if there are multiple constants in the method call.
– rveach
Jan 5 at 3:07
add a comment |
1
Great, that solve my problem. Thank you very much!
– sajgon338
Jan 4 at 5:59
It should be noted thatconstantWaiverParentToken
doesn't take into account the method being called, so it will hide violations where the method is changing the constant's value or if there are multiple constants in the method call.
– rveach
Jan 5 at 3:07
1
1
Great, that solve my problem. Thank you very much!
– sajgon338
Jan 4 at 5:59
Great, that solve my problem. Thank you very much!
– sajgon338
Jan 4 at 5:59
It should be noted that
constantWaiverParentToken
doesn't take into account the method being called, so it will hide violations where the method is changing the constant's value or if there are multiple constants in the method call.– rveach
Jan 5 at 3:07
It should be noted that
constantWaiverParentToken
doesn't take into account the method being called, so it will hide violations where the method is changing the constant's value or if there are multiple constants in the method call.– rveach
Jan 5 at 3:07
add a comment |
It is interesting that you are getting this error while declaring a static field. But any how you can add suppresswarning annotation,
@SuppressWarnings("checkstyle:magicnumber")
add a comment |
It is interesting that you are getting this error while declaring a static field. But any how you can add suppresswarning annotation,
@SuppressWarnings("checkstyle:magicnumber")
add a comment |
It is interesting that you are getting this error while declaring a static field. But any how you can add suppresswarning annotation,
@SuppressWarnings("checkstyle:magicnumber")
It is interesting that you are getting this error while declaring a static field. But any how you can add suppresswarning annotation,
@SuppressWarnings("checkstyle:magicnumber")
answered Jan 3 at 14:16
Murat GüvençMurat Güvenç
614
614
add a comment |
add a comment |
You have to declare a constant to describe a little more of what that value means:
private static final Long MAX_AMOUNT_INITIALIZER = 299_000L;
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(MAX_AMOUNT_INITIALIZER);
It sounds redundant but remember that a magic number is any number used along the code that does not have any explanation of its meaning. Even though you have a BigDecimal
constant, your Long
value is not interpreted as a "meaningful value" for the context.
add a comment |
You have to declare a constant to describe a little more of what that value means:
private static final Long MAX_AMOUNT_INITIALIZER = 299_000L;
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(MAX_AMOUNT_INITIALIZER);
It sounds redundant but remember that a magic number is any number used along the code that does not have any explanation of its meaning. Even though you have a BigDecimal
constant, your Long
value is not interpreted as a "meaningful value" for the context.
add a comment |
You have to declare a constant to describe a little more of what that value means:
private static final Long MAX_AMOUNT_INITIALIZER = 299_000L;
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(MAX_AMOUNT_INITIALIZER);
It sounds redundant but remember that a magic number is any number used along the code that does not have any explanation of its meaning. Even though you have a BigDecimal
constant, your Long
value is not interpreted as a "meaningful value" for the context.
You have to declare a constant to describe a little more of what that value means:
private static final Long MAX_AMOUNT_INITIALIZER = 299_000L;
private static final BigDecimal MAX_AMOUNT = BigDecimal.valueOf(MAX_AMOUNT_INITIALIZER);
It sounds redundant but remember that a magic number is any number used along the code that does not have any explanation of its meaning. Even though you have a BigDecimal
constant, your Long
value is not interpreted as a "meaningful value" for the context.
edited Jan 3 at 1:39
answered Jan 3 at 1:33


nguenongueno
96411019
96411019
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015246%2fhow-to-avoid-magic-number-warning-when-initialize-static-field-for-example-bigd%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Interesting! I can't reproduce this behavior. Which version of Checkstyle are you using? Can you share the
MagicNumber
config from your checkstyle.xml? According to the docs, you should not get this warning.– barfuin
Jan 3 at 13:25
Auch my bad. It comes out when I type BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN);
– sajgon338
Jan 3 at 16:37