Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example calculates approximate dimensions of a sphere, given its radius.:

Code Block
bgColor#ffcccc
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
bgColor#ffcccc
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
bgColor#ccccff
private static final int BUFSIZE = 512;

// ...

public void exampleFunction(int nbytes) {
  int nblocks = 1 + (nbytes - 1) / BUFSIZE;
  // ...
}

...