Java Reference
In-Depth Information
Click here to view code image
private static final int SIZE = 25;
Although final can be used to specify immutable constants, there is a caveat when
dealing with composite objects. See Guideline 73 , “ Never confuse the immutability of a
reference with that of the referenced object , ” for more details.
Noncompliant Code Example
Thisnoncompliantcodeexamplecalculatesapproximatedimensionsofasphere,givenits
radius:
Click here to view code image
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;
}
The methods use the seemingly arbitrary literals 3.14 , 4.19 , and 6.28 to represent
various scaling factors used to calculate these dimensions. A developer or maintainer
readingthiscodewouldhavelittleideaabouthowtheyweregeneratedorwhattheymean
and consequently would not understand the function of this code.
Noncompliant Code Example
This noncompliant code example attempts to avoid the problem by explicitly calculating
the required constants:
Click here to view code image
double area(double radius) {
return 3.14 * radius * radius;
}
double volume(double radius) {
Search WWH ::




Custom Search