How to avoid magic number warning when initialize static field (for example BigDecimal)?

how to avoid magic numbers in java
add a private constructor to hide the implicit public one
checkstyle disable magic number
checkstyle is a magic number
groovy ternary operator
groovy string concatenation
groovy string format
groovy variable in string

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)

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"

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.

Coding Rules, How to avoid magic number warning when initialize static field (for example BigDecimal)? private static final BigDecimal MAX_AMOUNT = BigDecimal. sajgon338. Apparently, this user prefers to keep an air of mystery about them. 1 How to avoid magic number warning when initialize static field (for example

It is interesting that you are getting this error while declaring a static field. But any how you can add suppresswarning annotation,


MagicNumberCheck (checkstyle 8.33 API), Noncompliant Code Example This rule tracks three types of non-invariant stop conditions: Instead remove the field's static modifier, or initialize it statically. constructorMax - Maximum authorized number of parameters for a constructor, INTEGER One might assume that writing new BigDecimal(0.1) in Java creates a  BigDecimal Class in Java The BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion and hashing. It can handle very large and very small floating point numbers with great precision but compensating with the time complexity a bit.

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.

Groovy Language Documentation, Checks that there are no "magic numbers" where a magic number is a numeric literal that is not defined as a constant. By default Constant definition is any variable/field that has 'final' modifier. static final int SECONDS_PER_DAY = 24 * 60 * 60; static final double Config example of constantWaiverParentToken option: The unchecked assignment warning is a standard Java compiler warning. A quick Google will reveal that the name of the unchecked assignment warning to pass to @SuppressWarnings is "unchecked". IDE's however tend to flag warnings for much more than what the Java compiler flags warnings for.

Java: Magic numbers should not be used, Java-style array initialization Differences with mixins; Static methods, properties and fields; Inheritance of state But best just avoid using a slashy string in such a case. Even if BigDecimal is the default for decimal numbers, such literals are def magicNumber = { -> 42 } // this call will fail because the closure doesn't  For example, a trigger could be invoked by an SOAP API call that inserted a batch of records. So if a batch of records invokes the same Apex code, all of those records need to be processed as a bulk, in order to write scalable code and avoid hitting governor limits. Here is an example of poorly written code that only handles one record:

[PDF] A Java Reference, Noncompliant Code Example. public static void doSomething() { for(int i = 0; i < 4; i++){ // Noncompliant, 4 is a magic number } }  C++ Core Guidelines. May 28, 2020. Editors: Bjarne Stroustrup; Herb Sutter; This is a living document under continuous improvement. Had it been an open-source (code) project, this would have been release 0.8. Copying, use, modification, and creation of derivative works from this project is licensed under an MIT-style license.

Code Issues – Embold Help Center, 1.3.5 Static Methods: Abstracting Computation . . . . . . . . . . . 17 5.9.2 Instance and field initializers . This example illustrates a number of things about Java: I'd rather keep it simple, so we'll start with straightforward interaction with a textual This loop fits a very common pattern: one variable (here, N) is first initialized,. Furthermore, we can use a field value from other beans. Suppose we have a bean named someBean with a field someValue equal to 10. Then 10 will be assigned to the field in this example: @Value("#{someBean.someValue}") private Integer someBeanValue; We can manipulate properties to get a List of values. In the following sample, we get a list of

  • 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.
  • Auch my bad. It comes out when I type BigDecimal.valueOf(299_999L).setScale(4, BigDecimal.ROUND_HALF-EVEN);
  • 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.