...
This noncompliant code example calculates approximate dimensions of a sphere, given its radius.:
Code Block | ||
---|---|---|
| ||
double area(double radius) { return 3.14 * radius * radius; } double volume(double radius) { return 4.19 * radius * radius * radius; } double greatCircleCircumference(double radius) { return 6.28 * radius; } |
...
This noncompliant code example attempts to avoid the problem by explicitly calculating the required constants.:
Code Block | ||
---|---|---|
| ||
double area(double radius) { return 3.14 * radius * radius; } double volume(double radius) { return 4.0 / 3.0 * 3.14 * radius * radius * radius; } double greatCircleCircumference(double radius) { return 2 * 3.14 * radius; } |
...
This compliant solution uses the identifier assigned to the constant value in the expression.:
Code Block | ||
---|---|---|
| ||
private static final int BUFSIZE = 512; // ... public void exampleFunction(int nbytes) { int nblocks = 1 + (nbytes - 1) / BUFSIZE; // ... } |
...